perf: Lookups and vectorization optimizations (P4, P8, P11) #159
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
Performance optimizations for lookup operations and vectorization patterns.
Changes
P8: vapply instead of sapply for null check
Location: vector extraction in
R/cansim_vectors.Rsapply(x, is.null)withvapply(x, is.null, logical(1))Reverted Changes
P4/P11: Environment hash table for parent lookup - ❌ REVERTED
The original plan proposed replacing named vector lookup with environment hash tables for O(1) lookup. Benchmarking revealed this is actually slower in R.
Benchmark Results
P4/P11: Environment hash table - ❌ 1.6% regression (REVERTED)
Test methodology: Isolated the
add_hierarchy()function pattern and benchmarked named vector vs environment hash table approaches.Test data: Simulated hierarchy metadata
Benchmark code:
Results:
Analysis: In R, named vector indexing
parent_lookup[key]is highly optimized (vectorized C code), while environment creation andmget()have overhead that outweighs the theoretical O(1) lookup benefit:forloop is slow in Rmget()overhead includes argument parsing, ifnotfound handling, and list creationThe theoretical O(n) vs O(1) analysis doesn't account for R's internal optimizations and constant factors.
Deferred Optimizations
P9 (French string constants): Not implemented
intToUtf8()is already very fast (O(1) character conversion)Summary
Test Plan
devtools::check()with no errors/warnings🤖 Generated with Claude Code