ruby / json

JSON implementation for Ruby
https://ruby.github.io/json
Other
704 stars 333 forks source link

JSON.dump: write directly into the provided IO #686

Closed byroot closed 2 days ago

byroot commented 2 weeks ago

Ref: https://github.com/ruby/json/issues/524

Fix: https://github.com/ruby/json/pull/538 Fix: https://github.com/ruby/json/issues/524

Rather than to buffer everything in memory.

Unfortunately Ruby doesn't provide an API to write into and IO without first allocating a string, which is a bit wasteful.

FYI: @headius

headius commented 3 days ago

Java side is implemented atop this PR in #538.

headius commented 3 days ago

We need some benchmarks!

byroot commented 2 days ago

I cherry picked you commits.

Now TruffleRuby head seem to crash for some reason, but I'm having trouble understanding Truffle crash reports, so I'm going to assume it's just a bug in Truffle. FYI: @eregon

Also the TruffleRuby::Generator is still buffering everything in a string before flushing, like other implementations used to.

eregon commented 2 days ago

Re the TruffleRuby crash, @andrykonchin can you look at that?

headius commented 2 days ago

@byroot Thanks! I have some optimizations I'll do in a separate PR now.

andrykonchin commented 1 day ago

@eregon The issue is fixed in https://github.com/oracle/truffleruby/commit/567aa2763b79c54e68615e66d8d4221cadb871d4.

byroot commented 1 day ago

Urk, I didn't realize rb_gc_mark_locations was using rb_gc_mark_maybe under the hood. I probably should use a loop instead.

eregon commented 1 day ago

Yes, I've never seen rb_gc_mark_locations() before, I think a simple loop would be better/safer and as fast or faster.