From 7deb605f08b4d239ca887d489a5b9d6e3f6439e1 Mon Sep 17 00:00:00 2001 From: Chandler King Date: Mon, 26 Oct 2026 18:36:44 -0400 Subject: [PATCH 1/2] perf: Key registry proto caches on version_id so a refresh does not rebuild objects The registry_proto_cache decorators keyed on id(registry_proto) as well as version_id. Every registry refresh parses a new proto object, so each refresh missed the cache and rebuilt every object from proto even when the registry had not changed. For a registry with 14 feature services that rebuild costs about 35 ms, most of it typeguard checks in Field.__init__. Key on version_id alone, falling back to the object identity only when a registry does not set a version_id. Also cache get_feature_service per version, which get_online_features calls on every request. Refs #7072 Co-Authored-By: Claude Fable 5.1 Signed-off-by: Chandler King --- .../infra/registry/proto_registry_utils.py | 13 +++-- .../registry/test_proto_registry_utils.py | 55 +++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/sdk/python/feast/infra/registry/proto_registry_utils.py b/sdk/python/feast/infra/registry/proto_registry_utils.py index a4c47740942..2f22321e3fd 100644 --- a/sdk/python/feast/infra/registry/proto_registry_utils.py +++ b/sdk/python/feast/infra/registry/proto_registry_utils.py @@ -29,6 +29,11 @@ from feast.stream_feature_view import StreamFeatureView +def _registry_proto_key(registry_proto: RegistryProto): + """Return the registry's version_id, or the object identity when it has none.""" + return registry_proto.version_id or id(registry_proto) + + def registry_proto_cache(func): cache_key = None cache_value = None @@ -37,7 +42,7 @@ def registry_proto_cache(func): def wrapper(registry_proto: RegistryProto, project: str): nonlocal cache_key, cache_value - key = tuple([id(registry_proto), registry_proto.version_id, project]) + key = (_registry_proto_key(registry_proto), project) if key == cache_key: return cache_value @@ -66,8 +71,7 @@ def wrapper( # Snapshot the filter so caller mutations cannot change a cached key. tags_key = frozenset(tags.items()) if tags is not None else None key = ( - id(registry_proto), - registry_proto.version_id, + _registry_proto_key(registry_proto), project, tags_key, kwargs_key, @@ -91,7 +95,7 @@ def registry_proto_cache_name(func): def wrapper(registry_proto: RegistryProto, name: str, project: str): nonlocal cache_proto_key, cache - proto_key = (id(registry_proto), registry_proto.version_id) + proto_key = _registry_proto_key(registry_proto) if proto_key != cache_proto_key: cache = {} @@ -119,6 +123,7 @@ def get_project_metadata( return None +@registry_proto_cache_name def get_feature_service( registry_proto: RegistryProto, name: str, project: str ) -> FeatureService: diff --git a/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py b/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py index fd0f2486b57..2957d1ea554 100644 --- a/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py +++ b/sdk/python/tests/unit/infra/registry/test_proto_registry_utils.py @@ -81,3 +81,58 @@ def test_list_saved_datasets_uses_spec_tags(self): ) assert len(result) == 1 assert result[0].name == "test_dataset" + + +def _registry_with_entity(version_id: str) -> RegistryProto: + registry = RegistryProto(version_id=version_id) + entity = Entity(name="driver", value_type=ValueType.INT64).to_proto() + entity.spec.project = "test_project" + registry.entities.append(entity) + return registry + + +def test_reparsed_registry_with_same_version_reuses_cache() -> None: + first_parse = _registry_with_entity("v1") + second_parse = _registry_with_entity("v1") + assert first_parse is not second_parse + + with patch.object(Entity, "from_proto", wraps=Entity.from_proto) as from_proto: + list_entities(first_parse, "test_project", None) + list_entities(second_parse, "test_project", None) + from_proto.assert_called_once() + + list_entities(_registry_with_entity("v2"), "test_project", None) + assert from_proto.call_count == 2 + + +def test_registry_without_version_id_is_cached_per_object() -> None: + first = _registry_with_entity("") + second = _registry_with_entity("") + + with patch.object(Entity, "from_proto", wraps=Entity.from_proto) as from_proto: + list_entities(first, "test_project", None) + list_entities(second, "test_project", None) + assert from_proto.call_count == 2 + + +def test_get_feature_service_is_cached_per_version() -> None: + from feast.feature_service import FeatureService + from feast.infra.registry.proto_registry_utils import get_feature_service + + def registry(version_id: str) -> RegistryProto: + proto = RegistryProto(version_id=version_id) + service = FeatureService(name="svc", features=[]).to_proto() + service.spec.project = "test_project" + proto.feature_services.append(service) + return proto + + with patch.object( + FeatureService, "from_proto", wraps=FeatureService.from_proto + ) as from_proto: + first = get_feature_service(registry("v1"), "svc", "test_project") + second = get_feature_service(registry("v1"), "svc", "test_project") + assert first is second + from_proto.assert_called_once() + + get_feature_service(registry("v2"), "svc", "test_project") + assert from_proto.call_count == 2 From a74e7830f4d1968a6e035a4342b84b2c6a0ea2a9 Mon Sep 17 00:00:00 2001 From: Chandler King Date: Mon, 26 Oct 2026 18:49:47 -0400 Subject: [PATCH 2/2] perf: Stamp SQL and Snowflake registry protos with a content-hash version_id SqlRegistry.proto() and SnowflakeRegistry.proto() never set version_id, so the version-keyed caches fell back to object identity and every cache refresh still rebuilt every object from proto. Set version_id to the sha1 of the deterministic serialization, which is stable across refreshes and changes on any apply or delete. On a 421 KB registry proto the hash costs 0.2 ms. Refs #7072 Co-Authored-By: Claude Fable 5.1 Signed-off-by: Chandler King --- sdk/python/feast/infra/registry/snowflake.py | 2 ++ sdk/python/feast/infra/registry/sql.py | 2 ++ .../unit/infra/registry/test_sql_registry.py | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/sdk/python/feast/infra/registry/snowflake.py b/sdk/python/feast/infra/registry/snowflake.py index 71ca51112ee..ec0a11e5f83 100644 --- a/sdk/python/feast/infra/registry/snowflake.py +++ b/sdk/python/feast/infra/registry/snowflake.py @@ -1,3 +1,4 @@ +import hashlib import logging import os import uuid @@ -1367,6 +1368,7 @@ def process_project(project: Project): if last_updated_timestamps: r.last_updated.FromDatetime(max(last_updated_timestamps)) + r.version_id = hashlib.sha1(r.SerializeToString(deterministic=True)).hexdigest() return r def _get_last_updated_metadata(self, project: str): diff --git a/sdk/python/feast/infra/registry/sql.py b/sdk/python/feast/infra/registry/sql.py index 771a3890886..36693656495 100644 --- a/sdk/python/feast/infra/registry/sql.py +++ b/sdk/python/feast/infra/registry/sql.py @@ -1,3 +1,4 @@ +import hashlib import logging import uuid from concurrent.futures import ThreadPoolExecutor @@ -1570,6 +1571,7 @@ def process_project(project: Project): if last_updated_timestamps: r.last_updated.FromDatetime(max(last_updated_timestamps)) + r.version_id = hashlib.sha1(r.SerializeToString(deterministic=True)).hexdigest() return r def commit(self): diff --git a/sdk/python/tests/unit/infra/registry/test_sql_registry.py b/sdk/python/tests/unit/infra/registry/test_sql_registry.py index e39aaa247e6..8b634ecc517 100644 --- a/sdk/python/tests/unit/infra/registry/test_sql_registry.py +++ b/sdk/python/tests/unit/infra/registry/test_sql_registry.py @@ -731,3 +731,39 @@ def test_schema_mode_invalid_value_rejected(self): path="sqlite:///dummy.db", schema_mode="bogus", ) + + +def test_proto_version_id_is_a_content_hash(shared_sqlite_db_path): + from unittest.mock import patch + + from feast.feature_service import FeatureService + + config = SqlRegistryConfig( + registry_type="sql", + path=f"sqlite:///{shared_sqlite_db_path}", + purge_feast_metadata=False, + ) + registry = SqlRegistry(config, "project_hash", None) + registry.apply_feature_service( + FeatureService(name="svc", features=[]), "project_hash" + ) + + first = registry.proto() + second = registry.proto() + assert first.version_id + assert first.version_id == second.version_id + assert first is not second + + with patch.object( + FeatureService, "from_proto", wraps=FeatureService.from_proto + ) as from_proto: + registry.refresh("project_hash") + a = registry.get_feature_service("svc", "project_hash", allow_cache=True) + registry.refresh("project_hash") + b = registry.get_feature_service("svc", "project_hash", allow_cache=True) + assert a is b + from_proto.assert_called_once() + + registry.apply_entity(Entity(name="customer", join_keys=["cid"]), "project_hash") + assert registry.proto().version_id != first.version_id + registry.teardown()