Performance: simplify instantiate_record_from_json (hot path when parsing every Kill Bill response) and add an isolated benchmark
@tungleduyxyz đang làm issue này rồi.
Từ ngày 4/11/2026.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Summary
Resource.instantiate_record_from_json turns each parsed JSON object into a model instance. It runs for every object of every Kill Bill response Kaui receives, recursively for nested objects. JFR profiling of Kaui on JRuby shows it as a large, hot method. The JRuby performance team recommends making it flatter (fewer blocks capturing surrounding variables) and smaller (the JVM optimizes small methods better), and adding an isolated benchmark of it together with Utils.underscore.
This complements killbill/killbill-client-ruby#125 (caching underscore/camelize). #125 removes the string-conversion cost; this issue is about the per-record and per-key work around it.
Where
resource.rb#L143-L180 at master (5d66ece, v4.0.11), called from from_json (#L122-L140) right after JSON.parse.
What it does today:
| When | Work | Cost |
|---|---|---|
| per record | resource_class.ancestors.select { |a| !@@attribute_names[a.name].nil? } |
walks and allocates the ancestor list for every record, although it only depends on the class |
| per key | name = Utils.underscore name |
3 regex gsub! + tr! (see #125) |
| per key | kb_ancestors.each { ... @@attribute_names[ancestor.name][name.to_sym] ... } |
a block, ancestor.name and to_sym lookups per key, to find has_one/has_many metadata |
| per key, nested | value.each { ... instantiate_record_from_json(type, val) ... } into a new array |
nested blocks capturing newValue, type and more |
| per key | record.send("#{Utils.underscore name}=", value) rescue nil |
underscores the name a second time, allocates the setter string, and uses rescue nil as control flow |
About rescue nil: when Kill Bill returns a field the model doesn't declare, the setter call raises NoMethodError and the exception is swallowed. Exceptions are expensive on the JVM (backtrace capture), so if this happens on a common path it costs real time. Not verified yet how often it happens with current Kill Bill (0.25.x) payloads; the benchmark below should measure it.
Proposed changes
- Per-class lookup table, built once and cached (thread-safe):
json_key => [setter_symbol, cardinality, type](ornilfor unknown keys). Each key then costs a single hash lookup: noancestorswalk, nounderscore, no string interpolation. - Unknown keys: look them up in that table (or use
respond_to?once per class/key) instead of raising and rescuingNoMethodError. Keep the current lenient behaviour (unknown fields are ignored). - Split into small methods: e.g.
assign_attribute,build_many,build_one, with no blocks capturing outer locals in the hot loop (plaineachover the hash and arrays). - Keep the public behaviour identical, including inheritance of
has_one/has_manydeclarations through ancestors, and nestedResources.
Benchmark (requested by the perf team)
Add a benchmark script, e.g. benchmark/from_json.rb, using benchmark-ips and allocation counts, that runs from_json on real Kill Bill payloads:
- an account with balance
- an invoice list
- a payment list
- the
AccountTimelineof a large account (the heaviest Kaui page)
Realistic payloads can be captured from our perf environment, which has 500 seeded accounts, including accounts with about 300 invoices and payments each. Run on JRuby 10 and CRuby, before and after.
Acceptance criteria
- Existing specs pass; new specs for nested
has_one/has_many, inherited attribute metadata, and unknown fields being ignored - Benchmark script committed, with before/after results (iterations/s and allocated objects) on JRuby 10 and CRuby
- A measurement of how often the
rescue nilpath is hit on the real payloads above - Follow-up JFR profile of Kaui shows
instantiate_record_from_jsonshrinking accordingly
Related
- killbill/killbill-client-ruby#125: cache
Utils.underscore/camelize(do first, or together) - killbill/killbill-admin-ui-standalone#136: json gem 3.x evaluation (faster JSON parsing)
- killbill/killbill-cloud#349 (invokedynamic) and killbill/killbill-admin-ui-standalone#137 (GC comparison and JFR recordings): the rest of the same profiling round
- Load-test harness and perf notes: killbill-admin-ui-standalone, branch
performance-test,perf/
- Ngôn ngữ chính
- Ruby
- Star
- 26
- Fork
- 27
- Merge trung bình
- 4 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 4
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
-
Docker images: plain X.Y.Z tag (e.g. 7.2.3) is never updated after the initial release buildĐang mởneeds verification
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 13 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
palladius/rails8-app-on-gcp#244 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
MAC addresses issuesĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
rezoleo/lea5#583 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 3 ngày