Repository navigation
Cache Utils.underscore/camelize to cut allocations (#125) - #129
Closed
tungleduyxyz wants to merge 1 commit into
Closed
tungleduyxyz wants to merge 1 commit into
tungleduyxyz wants to merge 1 commit into
Conversation
) Utils.underscore (3 gsub! + tr!) ran twice for every JSON key of every response, and camelize ran for every attribute on to_hash (and therefore on == and hash). The set of attribute names is small and finite, so: * Memoize underscore and camelize in a thread-safe, size-capped cache (copy-on-write frozen Hash behind a Mutex: lock-free reads, no new runtime dependency). Results are now frozen. * Skip the regexes for names that are already underscored. * Add Utils.underscore_symbols, which caches the attribute name and setter symbols together, and use it in Resource so that instantiate_record_from_json no longer underscores each key twice nor builds a setter string per key. * Add specs (edge cases, idempotency, thread safety, from_json round trip) and benchmark/utils_benchmark.rb. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Contributor
Author
|
Closing as a duplicate of #127, which covers the same fix for #125 (plus the ReDoS fix) and already has green CI. The remaining ideas from this PR (caching the attribute/setter symbols together, covering |
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.
What
Utils.underscore(3gsub!+tr!) ran twice for every JSON key of every response, andcamelizeran for every attribute into_hash(hence also in==/hash). The set of names is small and finite, so this caches the conversions.underscoreandcamelizein a thread-safe, size-capped cache (Utils::Cache, 4096 entries; past the cap, values are just computed). It is a frozenHashswapped copy-on-write under aMutex: reads are lock-free and there is no new runtime dependency. Results are now frozen; no caller in this gem mutates them (the only otherUtilsusage isdemodulize).-or::skip the regexes (on a cache miss).underscoreininstantiate_record_from_json, and addUtils.underscore_symbols(name), which caches[:attr_name, :attr_name=]together, so no setter string /to_symis allocated per key.Resource#initialize(hash)uses it too.String/Symbolinputs are cached; anything else is computed as before.to_hash) is not done:camelizeis now a hash lookup, which seemed enough.No behaviour change
underscore/camelize(String and Symbol, upper/lower, 652 inputs includingaccountCBA,::,-, empty, non-ASCII, random names): 0 mismatches.utils_spec.rb(edge cases, idempotency which justifies dropping the second call, frozen/cached results, input not aliased, thread safety, cache cap) and afrom_json/to_hashround-trip inresource_spec.rb.Benchmark
benchmark/utils_benchmark.rb(benchmark-ips + allocation counts): 87 real attribute names and a ~156 KB account-timeline-like payload (synthetic, built from the real model attribute names). master (5d66ece) vs. this branch, same machine (Apple Silicon, JDK 21):underscore(87 keys)camelize :lower(87 keys)from_json(timeline)to_hash(timeline)Allocated objects per iteration (CRuby; JRuby does not expose the counter):
underscore1065 → 0,camelize1837 → 0,from_json220,206 → 81,231,to_hash131,961 → 1,017.Still open from #125
Possible overlap with #126 (
instantiate_record_from_jsonsimplification): the change there is 3 lines (name, setter = Utils.underscore_symbols name, lookup by symbol,send(setter, value)), easy to rebase either way.🤖 Generated with Claude Code