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

Implement a freeze: parser option #447

Merged
merged 1 commit into from Oct 20, 2020
Merged

Conversation

casperisfine
Copy link

If set to true all parsed objects will be immediately frozen, and strings will be deduplicated if the Ruby implementation allows it.

This option is useful if you plan to keep the parsed objects around, and want to reduce their memory footprint, and you know they contain duplicated string. It's common in says I18n data, or some database like configuration files (e.g. tax rates etc).

A similar option was added to YAML/Pysch: ruby/psych#414

MessagePack is also about to have it: msgpack/msgpack-ruby#194 (at least the principle was accepted).

Right now this option incur a small overhead because it has to call String#-@ through rb_funcall, but hopefully Ruby 3.0 should soon expose rb_fstring_new(const char *ptr, long len) (likely under another name) which should allow to reduce allocations by directly looking up existing strings without allocating a new one.

NB: I presume that a java implementation is needed, I'm not too well versed into Java by I can try my hand at it. However I'd rather have your feedback on the MRI and "pure" implementations first.

@casperisfine casperisfine force-pushed the global-freeze-option branch 2 times, most recently from de7693a to 94e4064 Compare September 14, 2020 11:07
@casperisfine
Copy link
Author

cc @marcandre @tenderlove could I interest one of you into this feature?

@casperisfine
Copy link
Author

I presume that a java implementation is needed

I went ahead and implemented it anyway. It wasn't that complicated actually.

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

👍 Looks like a very useful addition to me 👍

@casperisfine
Copy link
Author

Thanks @marcandre.

cc @hsbt is this something you could consider merging?

@casperisfine casperisfine force-pushed the global-freeze-option branch 2 times, most recently from b0255ee to 3a56c08 Compare September 15, 2020 12:48
If set to true all parsed objects will be
immediately frozen, and strings will be
deduplicated if the Ruby implementation
allows it.
@casperisfine
Copy link
Author

It should be good to go now. CI is green.

@casperisfine
Copy link
Author

@hsbt anything else you'd like changed?

This was referenced Mar 15, 2021
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.

None yet

4 participants