perf(rust): eliminate context cloning and merge duplicated evaluation paths#112
Merged
perf(rust): eliminate context cloning and merge duplicated evaluation paths#112
Conversation
… paths - Take owned Value instead of &Value in all evaluate methods, avoiding deep clone of context on every evaluation (biggest win for large contexts) - Merge evaluate_flag_internal + evaluate_flag_internal_pre_enriched into single evaluate_flag_core method (-151 lines of duplicated code) - Merge evaluate_with_type_check + evaluate_with_type_check_pre_enriched - Add merge_metadata_flag_set_only to avoid HashMap::new() allocation on flag-not-found path - enrich_context now destructures owned Value instead of cloning Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Valueinstead of&Valuein all evaluate methods, avoiding a deep clone of the context on every evaluation. This is the biggest win for large contexts (1000+ attributes)evaluate_flag_internal+evaluate_flag_internal_pre_enrichedcollapsed into singleevaluate_flag_core(-151 lines)merge_metadata_flag_set_onlyhelper to avoidHashMap::new()allocation on flag-not-found pathenrich_contextnow destructures ownedValueinstead of cloning the context mapNet: -148 lines (162 added, 310 removed)
Motivation
The evaluation hot path cloned the context
Valueon every call:enrich_contextcalledobj.clone()to deep-copy the entire context mapcontext.clone()passed toevaluate_ownedeven though nothing needed modificationSince
evaluate_owned→Arc::new(data)takes ownership anyway, passing owned values from the start eliminates the clone entirely. The WASM callers already own the parsed context, so this is free.The two nearly-identical
evaluate_flag_internal/evaluate_flag_internal_pre_enrichedmethods (~130 lines each) were a maintenance hazard — any bug fix had to be applied in both. Merged into a singleevaluate_flag_corewith aneeds_enrichment: boolparameter.Test plan
cargo clippy -- -D warningsclean🤖 Generated with Claude Code