Skip to content

perf(rust): eliminate context cloning and merge duplicated evaluation paths#112

Merged
aepfli merged 2 commits intomainfrom
perf/eval-owned-context
Feb 22, 2026
Merged

perf(rust): eliminate context cloning and merge duplicated evaluation paths#112
aepfli merged 2 commits intomainfrom
perf/eval-owned-context

Conversation

@aepfli
Copy link
Copy Markdown
Contributor

@aepfli aepfli commented Feb 22, 2026

Summary

  • Take owned Value instead of &Value in all evaluate methods, avoiding a deep clone of the context on every evaluation. This is the biggest win for large contexts (1000+ attributes)
  • Merge duplicated evaluation methods: evaluate_flag_internal + evaluate_flag_internal_pre_enriched collapsed into single evaluate_flag_core (-151 lines)
  • Add merge_metadata_flag_set_only helper to avoid HashMap::new() allocation on flag-not-found path
  • enrich_context now destructures owned Value instead of cloning the context map

Net: -148 lines (162 added, 310 removed)

Motivation

The evaluation hot path cloned the context Value on every call:

  • Regular path: enrich_context called obj.clone() to deep-copy the entire context map
  • Pre-enriched path: context.clone() passed to evaluate_owned even though nothing needed modification

Since evaluate_ownedArc::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_enriched methods (~130 lines each) were a maintenance hazard — any bug fix had to be applied in both. Merged into a single evaluate_flag_core with a needs_enrichment: bool parameter.

Test plan

  • All 134 unit tests pass
  • All 36 integration tests pass
  • All 39 gherkin scenarios pass
  • All 6 metadata merging tests pass
  • cargo clippy -- -D warnings clean
  • WASM target builds successfully
  • Run benchmarks to measure improvement on large contexts

🤖 Generated with Claude Code

aepfli and others added 2 commits February 22, 2026 09:34
… 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>
@aepfli aepfli merged commit 9770d4f into main Feb 22, 2026
17 checks passed
@aepfli aepfli deleted the perf/eval-owned-context branch February 22, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant