Repository navigation
Build changes as frozen classes: sdiff 2.4x faster on lines, 3.5x fewer objects - #235
Conversation
Signed-off-by: Clément Joubert <czj@users.noreply.github.com> Assisted-by: Claude Opus 5.5 [Claude Code]
|
A note on the two red |
7a42f52 to
8afd6c8
Compare
|
You are correct about mingw. I'm dealing with a different repo right now, so please feel free to remove mingw from the matrix. |
|
LGTM. Once you remove |
7ca8501 to
22886ae
Compare
|
Before you merge: I was wrong in my reply on CHANGELOG.md, a closer review of my own change found that YAML didn't round-trip the same. A dump made with 2.0.x ( |
|
I might want to put a warning at the top of the CHANGELOG bringing the breaking change more visible. The I'll worry about that as prep for release. |
|
You're right about |
|
Go ahead and add the changes to this PR; I won't get to merging it until Saturday because of other stuff and life in general. |
`Diff::LCS::Change` and `Diff::LCS::ContextChange` are built for each element of a traversal, and a `Data` subclass that validates in `initialize` allocates three more objects per instance than a plain class: `Data.new` turns positional arguments into keywords before calling `initialize`, which calls `super` with them (ruby-core#22437). `ContextChange.new` goes from 347 to 38 ns with YJIT, and from 4 objects to 1. Both are now plain classes frozen in `initialize`, with the API they had as `Data`, in `Diff::LCS::FrozenValue`: positional or keyword construction and `[]`, `members`, `to_h`, `with`, `eql?` and `hash`, copies and `Marshal` and YAML loads that go through `initialize` and stay frozen, and the same `ArgumentError` messages for missing or unknown keywords, which a test checks against `Data.define` classes with the same members. A YAML document with missing or unknown members raises that error too. Each constructor defaults its parameters to a private sentinel, so the keyword and missing-argument checks only run when the last member was not passed positionally, and a `call-seq` keeps the sentinel out of the RDoc. `ContextChange.simplify` builds the simplified event positionally instead of through `with`, 1 object per event instead of 6. `with`, which the library no longer calls, takes one more object than with `Data`. `Diff::LCS::Block`, built once per hunk, stays a `Data` class. Visible differences: `is_a?(Data)` is false, `to_s`, `pp` and `ContextChange#inspect` drop the `data` prefix, a `Marshal` or YAML dump made with 2.0.x no longer loads, keyword names must be Symbols (in `new` and `with`), and a subclass overriding `initialize` with keyword parameters can no longer be built positionally. Breaking-Change: change objects are no longer Data instances, and Marshal or YAML dumps made with 2.0.x no longer load Signed-off-by: Clément Joubert <czj@users.noreply.github.com> Assisted-by: Claude Opus 5.5 [Claude Code]
MSYS2 deprecated mingw64, so ruby/setup-msys2-gcc no longer ships msys2-mingw64.7z in its latest release and setup-ruby fails with a 404 before any test runs. ruby/setup-ruby#969 removes the mingw build. The ucrt and mswin builds keep Windows covered. Signed-off-by: Clément Joubert <czj@users.noreply.github.com> Assisted-by: Claude Opus 5.5 [Claude Code]
22886ae to
06e5616
Compare
|
Thanks, done: the three fixes are folded into the Another review of the folded commit found two more small differences with I also corrected the description, the commit message and the CHANGELOG entry: |
Following up on #230:
Diff::LCS::ChangeandDiff::LCS::ContextChangeare now plain classes frozen ininitialize, with the API they had asData. On line diffs with YJIT,sdiffandtraverse_balancedrun 1.6 to 2.4× faster (1.5 to 1.6× without JIT) and allocate 2.4 to 3.5× fewer objects.diffandpatch!are unchanged, and outputs are identical.[],members,to_h,with,eql?andhash. No writers, copies stay frozen, andMarshaland YAML loads go throughinitialize; a YAML document with missing or unknown members raises like keyword construction. A test checks construction,withand theirArgumentErrormessages againstData.defineclasses with the same members.call-seqkeeps the sentinel out of the RDoc.ContextChange.newis 3.5× faster with YJIT and allocates 1 object instead of 4.ContextChange.simplifybuilds its event directly, 1 object instead of 6.is_a?(Data)is false,to_sandContextChange#inspectlose thedataprefix,ppprints likeinspect, andMarshalor YAML dumps don't cross versions: those made with 2.0.0 won't load, and those made with this change won't load in 2.0.0. Keyword names must be Symbols, innewandwith, a subclass overridinginitializewith keyword parameters can't be built positionally anymore, and mixing positional and keyword arguments (Change.new("+", 0, element: "a")) is no longer rejected.Datastays faster for keyword construction (1.4×),hash(3×),eql?(1.6×) andwith(1.3×), which the library no longer calls.Blockstays aData. It's built once per hunk, so there's nothing to gain there.Benchmarks
Workload: before/after revisions of 700 Ruby files from rails/rails'
activerecord/libhistory (150 of them also diffed by words), Ruby 4.0.9, arm64-darwin, best of 5; objects =GC.stat(:total_allocated_objects)delta for one run. Outputs compared by value (to_a) between both builds.Data→ frozensdiff, linestraverse_balanced+SDiffCallbacks, linessdiff, wordsdiff, linespatch!, linesContextChange.new× 1MContextChange.simplify× 1MAI-assisted; prompted, reviewed and verified by me.