Skip to content

Performance improvements in class_methods.rb#32

Open
guille-sage wants to merge 1 commit intomasterfrom
performance-2
Open

Performance improvements in class_methods.rb#32
guille-sage wants to merge 1 commit intomasterfrom
performance-2

Conversation

@guille-sage
Copy link
Contributor

Makes a series of performance improvements to the methods defined by #attr_accessor_type. In most cases this will make the initial loading when defining a class a bit slower but make parsing and dumping faster.

  • Use a shared ValueHelper singleton that is shared by all closures
  • Remove calls to #get_attribute: use the cka closure directly
  • Pre-compute getter and setter symbols (:"@name" and :"@name=")
  • Define smaller methods, changing the logic depending on the type and other things known at definition time. This makes the runtime checks leaner. For example, the old code generated methods that had to check on every run whether the attribute was one_of. This is solved by checking before defining the method.

Already showing decent improvements in the benchmarks:

bench median before (s) median after (s)
deep_100_deserialize 0.0460s 0.0367s
deep_100_serialize 0.0260s 0.0176s
deep_10_deserialize 0.0048s 0.0035s
deep_10_serialize 0.0027s 0.0018s
deep_500_deserialize 0.2439s 0.1897s
deep_500_serialize 0.1412s 0.1004s
flat_large_deserialize 0.5162s 0.4539s
flat_large_serialize 0.1532s 0.1169s
flat_medium_deserialize 0.0485s 0.0662s
flat_medium_serialize 0.0158s 0.0150s
flat_small_deserialize 0.0054s 0.0062s
flat_small_serialize 0.0015s 0.0015s
hash_deserialize 0.0461s 0.0258s
hash_serialize 0.0350s 0.0231s
micro_from_hash 0.4109s 0.2670s
micro_from_json 0.7753s 0.6003s
micro_to_hash 0.3552s 0.2282s
micro_to_json 0.3935s 0.2758s
nested_large_deserialize 0.7513s 0.6480s
nested_large_serialize 0.4686s 0.2864s
nested_medium_deserialize 0.0723s 0.0552s
nested_medium_serialize 0.0375s 0.0239s
nested_small_deserialize 0.0083s 0.0067s
nested_small_serialize 0.0041s 0.0027s

Makes a series of performance improvements to the methods defined by
`#attr_accessor_type`. In most cases this will make the initial loading
when defining a class a bit slower but make parsing and dumping faster.

- Use a shared ValueHelper singleton that is shared by all closures
- Remove calls to #get_attribute: use the cka closure directly
- Pre-compute getter and setter symbols (:"@name" and :"@name=")
- Define smaller methods, changing the logic depending on the type and
  other things known at definition time. This makes the runtime checks
  leaner. For example, the old code generated methods that had to check
  on every run whether the attribute was `one_of`. This is solved by
  checking before defining the method.
Base automatically changed from performance-1 to master February 16, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant