Skip to content

Add string_fastpath option to save on serializing simple strings#1041

Merged
petergoldstein merged 1 commit into
petergoldstein:mainfrom
byroot:string-bypass
May 16, 2025
Merged

Add string_fastpath option to save on serializing simple strings#1041
petergoldstein merged 1 commit into
petergoldstein:mainfrom
byroot:string-bypass

Conversation

@byroot

@byroot byroot commented May 9, 2025

Copy link
Copy Markdown
Contributor

Going through Marshal.load/dump (or other serializers) when the value is already a string is quite costly for not a whole lot of value.

The only real benefit is that it automatically preserve the string encoding, but we can assume the overwhelming majority of strings are either UTF-8 or BINARY, so we can encode that in the bitflags.

For strings that have a different encoding, or objects that inherit from String we can continue to use the serializer.

Note that only the #store path checks for the option. #retrieve always handle this new format. This is so that you can seemlessly upgrade without having to flush your caches. You can first upgrade dalli and then later turn on the option.

It would be nice to make this option a default at some point though.

@danmayer @nickamorim (Ref: rails/rails#54996)

@danmayer

Copy link
Copy Markdown
Collaborator

from some discussion we think this looks good and we will likely go this way vs trying to have the client taking raw true to handle this sort of bypass. We will likely pull it into our internal fork first and can roll it out and verify it there.

@nickamorim nickamorim left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM - thanks @byroot

Going through `Marshal.load/dump` (or other serializers) when the
value is already a string is quite costly for not a whole lot
of value.

The only real benefit is that it automatically preserve the string
encoding, but we can assume the overwhelming majority of strings
are either UTF-8 or BINARY, so we can encode that in the bitflags.

For strings that have a different encoding, or objects that inherit
from `String` we can continue to use the serializer.

Note that only the `#store` path checks for the option.
`#retrieve` always handle this new format. This is so that you can
seemlessly upgrade without having to flush your caches. You can
first upgrade dalli and then later turn on the option.

It would be nice to make this option a default at some point
though.
@byroot

byroot commented May 13, 2025

Copy link
Copy Markdown
Contributor Author

I simplified the code further. Rubocop is still complaining that the method is 12 lines long instead of 10, but I refuse to comply.

@danmayer

Copy link
Copy Markdown
Collaborator

yeah don't worry about the rubocop warning we want to loosen up the rules for this gem as they have been frustrating while refactoring and making any significant changes.

@petergoldstein petergoldstein self-requested a review May 16, 2025 01:11

@petergoldstein petergoldstein left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This looks good.

@petergoldstein petergoldstein merged commit bfbd05a into petergoldstein:main May 16, 2025
40 of 45 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.

4 participants