perf(python): add host-side optimizations for pre-evaluation, context filtering, and index-based eval#72
Merged
Merged
Conversation
…uation
Reduce targeting flag evaluation latency for large contexts by avoiding
unnecessary data transfer across the WASM boundary.
Rust side:
- Walk compiled targeting trees to extract referenced context keys
(e.g. {"var": "email"} -> "email") during update_state()
- Return per-flag requiredContextKeys and flagIndices in UpdateStateResponse
- Add evaluate_by_index(u32, ctx_ptr, ctx_len) WASM export for O(1) flag
lookup by numeric index instead of string key HashMap lookup
- Add evaluate_flag_pre_enriched() that skips context enrichment when
$flagd is already present (host-side enrichment)
Java side:
- Cache requiredContextKeys and flagIndices from updateState() response
- EvaluationContextSerializer.serializeFiltered() serializes only the
context keys a targeting rule references, plus $flagd enrichment
- evaluateFlag(EvaluationContext) uses filtered serialization + index-based
eval when available, falls back to full serialization gracefully
- evaluateByIndex() calls the new WASM export with O(1) Vec lookup
JMH results (1000+ attribute LayeredEvaluationContext):
- Targeting flags: ~12.8 µs (down from ~167 µs) — 13x improvement
- vs old json-logic-java: 32-34x faster (409 µs -> 12.8 µs)
- Static/disabled flags: ~0.02 µs (pre-evaluated cache, unchanged)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… filtering, and index-based eval Normalize the Python PyO3 bindings to support the same 3 host-side optimizations as Java and Go: 1. Pre-evaluated cache: after update_state(), cache results for static/disabled flags and return them directly without calling into the Rust evaluator. 2. Context key filtering: store requiredContextKeys per flag and build a filtered context containing only the keys referenced by the targeting rule, plus $flagd enrichment and targetingKey. 3. Index-based evaluation: store flagIndices and call evaluate_flag_by_index() when both index and required keys are available, using O(1) Vec lookup instead of HashMap. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The WASM tests use a thread-local singleton evaluator. Parallel test execution causes race conditions where one test's update_state overwrites another test's state, producing intermittent failures like test_wasm_evaluate_by_index getting "Static" instead of "TargetingMatch". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Gherkin tests (cucumber) don't support --test-threads flag. Run lib and integration tests with --test-threads=1 for WASM singleton safety, and gherkin tests separately without the flag. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
$flagdenrichment andtargetingKeyevaluate_flag_by_index()with O(1) Vec lookup when both index and required keys are availableBenchmark Highlights
vs native
json-logic-utils(used by current flagd Python provider):Changes
python/src/lib.rsFlagEvaluatorstruct, optimized evaluation pipelinepython/tests/test_optimizations.pyTest plan
cargo fmtandcargo clippy -- -D warningsclean🤖 Generated with Claude Code