Repository navigation
Simplify instantiate_record_from_json with a cached per-class lookup table - #131
Merged
Merged
Conversation
…table Resolve each JSON key once per class into [setter, cardinality, type] (copy-on-write cache, cleared when attributes are declared) instead of walking ancestors, underscoring twice and rescuing NoMethodError for every key of every record. Unknown keys are cached and skipped without raising. Split the method into smaller pieces. Add specs for nested has_one/has_many, inherited metadata, unknown fields and cache invalidation, plus benchmark/from_json.rb (benchmark-ips). Refs #126 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
([A-Z]+)([A-Z][a-z]) backtracks quadratically on long runs of capital letters. Use an equivalent lookaround form (fuzzed against the original on 300k random strings, identical output) that runs in linear time. Add specs for Utils.underscore, including a long-input timing check. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
4 tasks
Resolve conflicts with #127 (cached Utils.underscore/camelize): - resource.rb: keep this branch's lookup-table rewrite of instantiate_record_from_json; it already underscores each key once. - utils.rb: take master's version; both branches fixed the same ReDoS in underscore with equivalent regexes. - utils_spec.rb: take master's specs and add this branch's extra underscore cases and the no-mutation check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019weYmj1um68VaKJYHC646E
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.
Addresses #126 (partially; see "Not covered" below).
What
Resource.instantiate_record_from_jsonnow resolves each JSON key once per class into[setter, cardinality, type]and caches it, instead of per key/record doing anancestorswalk, aUtils.underscorecall, string interpolation andrescue nilfor unknown fields (a raisedNoMethodErrorper unknown key).has_one,has_many,attributeandcreate_alias.NoMethodError.instantiate_record_from_json,assign_json_attribute,build_manyand lookup helpers.has_one/has_manyand leniency: a setter that raises is still swallowed (rescue StandardError, free unless raised).Rebased on #127 (cached
Utils.underscore/camelize) via a merge ofmaster. This PR's earlierUtils.underscoreReDoS fix is superseded by the equivalent one in #127, soutils.rbis no longer part of the diff; this PR only adds a few extraUtils.underscorecases toutils_spec.rb.Tests
New
spec/killbill_client/instantiate_record_from_json_spec.rb(14 examples): nested has_one/has_many, inherited metadata, no leak to parent, unknown fields ignored without exceptions, shape mismatches, nulls, cache invalidation, threads, and a realAccountTimeline. All but the no-exceptions example also pass on the old code.Full unit suite after merging
master: 107 examples, 0 failures on CRuby 3.3.6. (Before the merge: 68 examples, 0 failures on CRuby 2.6.10 and JRuby 9.4.9.)Benchmark
New
benchmark/from_json.rb(benchmark-ips, allocation and exception counts,Utils.underscore). UsePAYLOAD_DIRfor real captures; otherwise it generates synthetic payloads.Synthetic payloads, CRuby 3.3.6, current
master(with #127's caches) vs this PR:Earlier numbers, measured before #127 was merged
Iterations/s:
CRuby allocations for the timeline: ~2.0M -> ~171k objects per parse.