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

Handle integers bigger than 64bit #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

casperisfine
Copy link

The capacity of handling arbitrary size integers is quite important to be able to replace the stdlib library.

Unfortunately RapidJSON isn't very flexible here.

For generating JSON it's quite easy to fallback to Integer#to_s.

However for parsing, the only escape hatch is to parse all integers ourselves, which significantly impact performance.

It could be a bit better by parsing in place, but all Ruby functions expect null terminated C-strings, so it wouldn't work well.

Ruby number parsing is also not as fast at RapidJSON's.

So for this to be performant, RapidJSON would need a flag that allows to only parse over-sized integers ourself, and not all numbers.

But that would require a new feature upstream.

(opening as a draft for comments, but I don't think it's mergeable in this state)

The capacity of handling arbitrary size integers is quite important
to be able to replace the stdlib library.

Unfortunately RapidJSON isn't very flexible here.

For generating JSON it's quite easy to fallback to `Integer#to_s`.

However for parsing, the only escape hatch is to parse all
integers ourselves, which significantly impact performance.

It could be a bit better by parsing in place, but all Ruby functions
expect null terminated C-strings, so it wouldn't work well.

Ruby number parsing is also not as fast at RapidJSON's.

So for this to be performant, RapidJSON would need a flag that
allows to only parse over-sized integers ourself, and not all numbers.

But that would require a new feature upstream.
@casperisfine
Copy link
Author

So Tencent/rapidjson#2155 would allow to really keep the overhead down in the vast majority of cases where the number fits int64_t / uint64_t or is a Float.

No idea if it has any chance to be accepted upstream though.

casperisfine pushed a commit to casperisfine/rapidjson-ruby that referenced this pull request May 17, 2023
BigNum are expected to be rare, so calling `Integer#to_s` while not
super fast is fast enough and simpler than checking bounds.

Ref: jhawthorn#4
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