Skip to content

Simplify instantiate_record_from_json with a cached per-class lookup table - #131

Merged
tungleduyxyz merged 3 commits into
masterfrom
issue_126_simplify_instantiate_record
Nov 18, 2026
Merged

tungleduyxyz merged 3 commits into
masterfrom
issue_126_simplify_instantiate_record

Conversation

@tungleduyxyz

@tungleduyxyz tungleduyxyz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #126 (partially; see "Not covered" below).

What

Resource.instantiate_record_from_json now resolves each JSON key once per class into [setter, cardinality, type] and caches it, instead of per key/record doing an ancestors walk, a Utils.underscore call, string interpolation and rescue nil for unknown fields (a raised NoMethodError per unknown key).

  • Copy-on-write cache: readers never lock, writers are serialized. Cleared on has_one, has_many, attribute and create_alias.
  • Unknown keys are cached and skipped without raising NoMethodError.
  • Method split into instantiate_record_from_json, assign_json_attribute, build_many and lookup helpers.
  • Behaviour is unchanged, including inheritance of has_one/has_many and leniency: a setter that raises is still swallowed (rescue StandardError, free unless raised).

Rebased on #127 (cached Utils.underscore/camelize) via a merge of master. This PR's earlier Utils.underscore ReDoS fix is superseded by the equivalent one in #127, so utils.rb is no longer part of the diff; this PR only adds a few extra Utils.underscore cases to utils_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 real AccountTimeline. 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). Use PAYLOAD_DIR for real captures; otherwise it generates synthetic payloads.

Synthetic payloads, CRuby 3.3.6, current master (with #127's caches) vs this PR:

Payload master (i/s) this PR (i/s) Speedup Allocations master → PR Exceptions master → PR
Account 27.2k 54.4k 2.0x 77 → 36 2 → 0
Invoice list x50 97.2 214 2.2x 20.3k → 8.5k 700 → 0
Payment list x50 142 318 2.2x 15.1k → 5.9k 700 → 0
AccountTimeline (300 invoices/payments) 8.6 19.2 2.2x 219k → 89k 8,704 → 0
Earlier numbers, measured before #127 was merged

Iterations/s:

Payload CRuby 2.6.10 before after JRuby 9.4 before after
Account 9.9k 77k 18.2k 53.9k
Invoice list x50 31.8 273 60.6 187
Payment list x50 46.3 364 85.6 252
AccountTimeline (300 invoices/payments) 3.0 22.8 5.8 17.0

CRuby allocations for the timeline: ~2.0M -> ~171k objects per parse.

…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>
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
@tungleduyxyz
tungleduyxyz merged commit 06f1c2b into master Nov 18, 2026
14 checks passed
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.

2 participants