Conversation
osv gem does not build cleanly, so it should be skipped
Goal: cut allocations and copies in the hot paths so the Rust extension runs as fast as possible without resorting to unsafe shortcuts that break GC safety. Writer changes - Take RArray/RString directly instead of having magnus deserialize rows into Vec<Vec<String>> on every call (eliminates ~10K transient String + 1K Vec allocations for a 1000-row generate_lines). - Use csv::Writer::write_byte_record so each row hits csv crate's bulk-copy fast path. - For generate_lines, write straight into a pre-sized Ruby string via rb_str_cat (RStringWriter) — saves the final Vec<u8> -> RString copy. - For generate_line, keep the Vec<u8> + enc_str_new path so MRI can use an embedded RString for the typically-small output. Reader changes - parse_csv accepts RString and reads bytes via as_slice — no String::from_utf8 round-trip on the input. - Drive csv-core directly so we bypass csv crate's internal BufReader copy of the in-memory input. - Preserve the input string's encoding on every produced field instead of always returning BINARY, so non-ASCII inputs round-trip correctly. Build profile - Add a release profile with fat LTO and codegen-units = 1. Benchmark harness - Use a more representative dataset (mix of plain hex, quoted/escaped fields, and empty values) and add coverage for generate_line and the streaming Reader.each path so the new code paths are measured. Results on Ruby 4.0.3 + YJIT, 1000-row dataset: generate_lines 1.36k -> 6.64k i/s (~4.9x) generate_line ? -> 1.63M i/s parse 1.80k -> 2.10k i/s (~1.17x; mostly Ruby alloc cost)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal: cut allocations and copies in the hot paths so the Rust extension
runs as fast as possible without resorting to unsafe shortcuts that
break GC safety.
Writer changes
rows into Vec<Vec> on every call (eliminates ~10K transient
String + 1K Vec allocations for a 1000-row generate_lines).
bulk-copy fast path.
rb_str_cat (RStringWriter) — saves the final Vec -> RString copy.
an embedded RString for the typically-small output.
Reader changes
String::from_utf8 round-trip on the input.
copy of the in-memory input.
of always returning BINARY, so non-ASCII inputs round-trip correctly.
Build profile
Benchmark harness
fields, and empty values) and add coverage for generate_line and the
streaming Reader.each path so the new code paths are measured.
Results on Ruby 4.0.3 + YJIT, 1000-row dataset:
generate_lines 1.36k -> 6.64k i/s (~4.9x)
generate_line ? -> 1.63M i/s
parse 1.80k -> 2.10k i/s (~1.17x; mostly Ruby alloc cost)