Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Symbol#name to minimize memory allocation #405

Conversation

skryukov
Copy link
Contributor

@skryukov skryukov commented Jan 2, 2025

Ruby 3.0 introduced the Symbol#name method that acts as Symbol#to_s but returns a frozen string, we can utilize this method to reduce number of objects Alba creates during serialization.

BEFORE (no YJIT, no Oj):

IPS:

Comparison:
               panko:      692.4 i/s
alba_with_transformation:      276.5 i/s - 2.50x  slower
                alba:      240.7 i/s - 2.88x  slower
         alba_inline:       75.1 i/s - 9.21x  slower

MEMORY:

Comparison:
               panko:     259178 allocated
alba_with_transformation:     650141 allocated - 2.51x more
                alba:     826201 allocated - 3.19x more
         alba_inline:    2711921 allocated - 10.46x more

AFTER (no YJIT, no Oj):

IPS:

Comparison:
               panko:      668.5 i/s
alba_with_transformation:      276.8 i/s - 2.42x  slower
                alba:      264.6 i/s - 2.53x  slower
         alba_inline:       74.4 i/s - 8.99x  slower

MEMORY:

Comparison:
               panko:     259178 allocated
alba_with_transformation:     650141 allocated - 2.51x more
                alba:     650201 allocated - 2.51x more
         alba_inline:    2535921 allocated - 9.78x more

@skryukov skryukov force-pushed the use-symbol-name-for-regularize-key branch from 7e2477c to 184a86c Compare January 2, 2025 18:49
@skryukov
Copy link
Contributor Author

skryukov commented Jan 2, 2025

One thing I noticed is that we do test for UserResource.new(@user).serialize(root_key: true), but this line raises an error with Alba.symbolize_keys!.

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.83%. Comparing base (22eb21f) to head (d2edf21).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #405   +/-   ##
=======================================
  Coverage   99.83%   99.83%           
=======================================
  Files          14       14           
  Lines         599      600    +1     
  Branches      153      154    +1     
=======================================
+ Hits          598      599    +1     
  Misses          1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@skryukov skryukov force-pushed the use-symbol-name-for-regularize-key branch from 184a86c to d2edf21 Compare January 2, 2025 18:52
Copy link
Owner

@okuramasafumi okuramasafumi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow I think I knew name method, but did not know it makes such a big difference, thank you!

@okuramasafumi okuramasafumi merged commit d565832 into okuramasafumi:main Jan 4, 2025
68 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