Skip to content

Build changes as frozen classes: sdiff 2.4x faster on lines, 3.5x fewer objects - #235

Merged
halostatue merged 2 commits into
halostatue:mainfrom
czj:frozen-change-classes
Nov 17, 2026
Merged

halostatue merged 2 commits into
halostatue:mainfrom
czj:frozen-change-classes

Conversation

@czj

@czj czj commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Following up on #230: Diff::LCS::Change and Diff::LCS::ContextChange are now plain classes frozen in initialize, with the API they had as Data. On line diffs with YJIT, sdiff and traverse_balanced run 1.6 to 2.4× faster (1.5 to 1.6× without JIT) and allocate 2.4 to 3.5× fewer objects. diff and patch! are unchanged, and outputs are identical.

  • Same API, still immutable. Positional or keyword construction and [], members, to_h, with, eql? and hash. No writers, copies stay frozen, and Marshal and YAML loads go through initialize; a YAML document with missing or unknown members raises like keyword construction. A test checks construction, with and their ArgumentError messages against Data.define classes with the same members.
  • Nothing extra on the hot path. Each constructor defaults its parameters to a private sentinel, so keywords and missing arguments are only handled when the last member wasn't passed positionally, and a call-seq keeps the sentinel out of the RDoc. ContextChange.new is 3.5× faster with YJIT and allocates 1 object instead of 4. ContextChange.simplify builds its event directly, 1 object instead of 6.
  • What you'd give up. is_a?(Data) is false, to_s and ContextChange#inspect lose the data prefix, pp prints like inspect, and Marshal or 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, in new and with, a subclass overriding initialize with keyword parameters can't be built positionally anymore, and mixing positional and keyword arguments (Change.new("+", 0, element: "a")) is no longer rejected.
  • Off the hot path, Data stays faster for keyword construction (1.4×), hash (3×), eql? (1.6×) and with (1.3×), which the library no longer calls.
  • Block stays a Data. 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/lib history (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.

Workload YJIT, Data → frozen No JIT Objects
sdiff, lines 138 → 58 ms 232 → 147 ms 1.07M → 309k
traverse_balanced + SDiffCallbacks, lines 127 → 79 ms 251 → 170 ms 1.33M → 562k
sdiff, words 1,099 → 879 ms 3.30 → 3.12 s 2.59M → 768k
diff, lines 34 → 37 ms 106 → 105 ms 72k → 61k
patch!, lines 2.6 → 2.6 ms 6.3 → 7.2 ms 4.4k → 4.4k
ContextChange.new × 1M 358 → 101 ms 417 → 147 ms 4M → 1M
ContextChange.simplify × 1M 439 → 96 ms 548 → 173 ms 6M → 1M

AI-assisted; prompted, reviewed and verified by me.

czj added a commit to czj/diff-lcs that referenced this pull request Nov 10, 2026
Signed-off-by: Clément Joubert <czj@users.noreply.github.com>
Assisted-by: Claude Opus 5.5 [Claude Code]
@coveralls

coveralls commented Nov 10, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 95.163% (+0.4%) from 94.778% — czj:frozen-change-classes into halostatue:main

@czj

czj commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

A note on the two red Ruby mingw jobs: they fail in setup-ruby before any test runs, and they fail the same way on main. MSYS2 deprecated mingw64, so ruby/setup-msys2-gcc#41 froze its toolchain and msys2-mingw64.7z is no longer in the latest release (a 404). ruby/setup-ruby#969 removes the mingw build, so it's probably time to drop it from the matrix. Every other job is green.

@czj
czj force-pushed the frozen-change-classes branch from 7a42f52 to 8afd6c8 Compare November 10, 2026 08:50
@halostatue

Copy link
Copy Markdown
Owner

You are correct about mingw. I'm dealing with a different repo right now, so please feel free to remove mingw from the matrix.

Comment thread CHANGELOG.md Resolved
@halostatue

Copy link
Copy Markdown
Owner

LGTM. Once you remove mingw (separate commit, chore: Remove unsupported mingw builds from test matrix) then I think I can merge this.

@czj

czj commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Done in 496b58f, thanks! Closing #238, which did the same on its own.

@czj
czj force-pushed the frozen-change-classes branch 2 times, most recently from 7ca8501 to 22886ae Compare November 11, 2026 16:37
@czj

czj commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

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 (!ruby/data:Diff::LCS::Change) can't load anymore, since Psych binds Data#initialize to it, and a new dump came back through allocate, unfrozen and unvalidated. FrozenValue now has encode_with and init_with, so a YAML load goes through initialize like a Marshal load does (with a test), and the CHANGELOG now says that Marshal and YAML dumps made with 2.0.x no longer load. That also settles your musing on CHANGELOG.md, so the feat: commit now carries a Breaking-Change trailer, as CONTRIBUTING asks. It's still one feat: commit plus the chore: one, and I've updated the description to match.

@halostatue

Copy link
Copy Markdown
Owner

I might want to put a warning at the top of the CHANGELOG bringing the breaking change more visible.

The # :nodoc: presence did not (or should not have) prevented the classes from being documented in RDoc, just the extra constant definition because of the way that Data definitions and then extensions work. I might still keep this minor, because I looked at RSpec (the main consumer of diff-lcs) and there's nothing that looks like it does Marshal or YAML serde of either of these.

I'll worry about that as prep for release.

@czj

czj commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

You're right about :nodoc:: it only covered the Data.define constants, and both classes are in the RDoc. Checking what this PR publishes there sent me back over the whole change, and I found three things I want fixed before the release. A YAML document with missing members loads with nil in their place instead of raising, so my "validated" above was only half true. new shows its NO_ARGUMENT defaults in the RDoc. And ContextChange.simplify still goes through with, 6 objects per event where 1 is enough. None of it changes what you reviewed here, so I'm leaving this PR as you approved it and will open a small follow-up as soon as it's merged: the branch is ready and tested. The CHANGELOG warning and the version are yours for release prep, and thanks for checking RSpec.

@halostatue

Copy link
Copy Markdown
Owner

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.

czj added 2 commits November 12, 2026 15:47
`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]
@czj
czj force-pushed the frozen-change-classes branch from 22886ae to 06e5616 Compare November 12, 2026 16:03
@czj

czj commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, done: the three fixes are folded into the feat: commit (9a51e7e), so it's still one feat: commit plus the chore: one. A YAML document with missing or unknown members now raises the same ArgumentError as keyword construction, new gets a call-seq and NO_ARGUMENT is private, and ContextChange.simplify builds its event directly, 1 object instead of 6.

Another review of the folded commit found two more small differences with Data, now fixed: members returns a new Array, and marshal_dump is private. Two others are now listed in the description and the CHANGELOG: keyword names must be Symbols, and a subclass overriding initialize with keywords can't be built positionally.

I also corrected the description, the commit message and the CHANGELOG entry: Data classes have allocate too, Change#inspect never had the data prefix (pp did), and traverse_balanced is 1.7× faster with YJIT, not more than twice. The numbers are remeasured. Saturday is perfectly fine, no rush :)

@czj czj changed the title Build changes as frozen classes: sdiff 2.7x faster on lines, 3.5x fewer objects Build changes as frozen classes: sdiff 2.4x faster on lines, 3.5x fewer objects Nov 12, 2026
@halostatue
halostatue merged commit 87f629c into halostatue:main Nov 17, 2026
48 checks passed
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.

3 participants