perf: cache deserialized feature definitions in FeatureEvaluator - #1
Draft
elliotmjackson wants to merge 1 commit into
Draft
perf: cache deserialized feature definitions in FeatureEvaluator#1elliotmjackson wants to merge 1 commit into
elliotmjackson wants to merge 1 commit into
Conversation
Deserializing a feature definition dominates evaluation cost. Profiling a caller that evaluates ~50 features per request put 44% of process CPU in evaluateFeature, three quarters of it inside Gson.fromJson — more than every database query on the endpoint combined. The parsed Feature depends only on the feature JSON, never on the context or attributes, so it is re-derived for nothing. Callers that build a GrowthBook per evaluation — the normal shape when the context carries per-request attributes — pay it on every single call. Cache the parsed definition, keyed by feature name and scoped to one features document identified by reference. A new features object (an API refresh, or setFeatures) replaces the map wholesale. Reference identity is deliberate: JsonObject.equals walks the whole tree and would cost as much as the parse it avoids. The cache is static because a FeatureEvaluator is created per GrowthBook, so a per-instance cache would never be reused by those callers. Entries are safe to share: Feature exposes only final fields and nothing in evaluation mutates a feature or its rules. Measured on the calling service, API pinned to 2 cores to remove load-generator contention: 104 -> 220 rps at 100% success, median latency 17ms -> 9.9ms. Roughly double the throughput per core. The existing conformance suite passes unchanged (132 tests), plus 4 new tests covering cache scoping and invalidation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deserializing a feature definition dominates evaluation cost. Profiling a caller that evaluates ~50 features per request put 44% of process CPU in evaluateFeature, three quarters of it inside Gson.fromJson — more than every database query on the endpoint combined.
The parsed Feature depends only on the feature JSON, never on the context or attributes, so it is re-derived for nothing. Callers that build a GrowthBook per evaluation — the normal shape when the context carries per-request attributes — pay it on every single call.
Cache the parsed definition, keyed by feature name and scoped to one features document identified by reference. A new features object (an API refresh, or setFeatures) replaces the map wholesale. Reference identity is deliberate: JsonObject.equals walks the whole tree and would cost as much as the parse it avoids. The cache is static because a FeatureEvaluator is created per GrowthBook, so a per-instance cache would never be reused by those callers.
Entries are safe to share: Feature exposes only final fields and nothing in evaluation mutates a feature or its rules.
Measured on the calling service, API pinned to 2 cores to remove load-generator contention: 104 -> 220 rps at 100% success, median latency 17ms -> 9.9ms. Roughly double the throughput per core.
The existing conformance suite passes unchanged (132 tests), plus 4 new tests covering cache scoping and invalidation.