diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a2116c..9041bfe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -205,7 +205,6 @@ jobs: - '3.3' - '3.4' - '4.0' - - mingw - mswin - ucrt diff --git a/CHANGELOG.md b/CHANGELOG.md index 12f5003..05b692b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,16 @@ `diff` and `sdiff` build their default results without traversal events. [#230][pull-230] +- Clément Joubert ([@czj][gh-user-czj]) made `Diff::LCS::Change` and + `Diff::LCS::ContextChange` plain frozen classes with the API they had as + `Data` classes, so building one allocates a single object: on line diffs + with YJIT, `sdiff` runs more than twice as fast and `traverse_balanced` + 1.7 times as fast. They are no longer `Data` instances, `to_s`, `pp` and + `ContextChange#inspect` drop the `data` prefix, keyword names must be + Symbols, a subclass overriding `initialize` with keyword parameters can no + longer be built positionally, and a `Marshal` or YAML dump made with 2.0.x + no longer loads. [#235][pull-235] + ## 2.0.0 / 2026-02-01 This release has significant **breaking changes**. @@ -591,6 +601,7 @@ This release has significant **breaking changes**. [pull-230]: https://github.com/halostatue/diff-lcs/pull/230 [pull-231]: https://github.com/halostatue/diff-lcs/pull/231 [pull-234]: https://github.com/halostatue/diff-lcs/pull/234 +[pull-235]: https://github.com/halostatue/diff-lcs/pull/235 [rspec-expectations#200]: https://github.com/rspec/rspec-expectations/pull/200 [rspec-expectations#219]: https://github.com/rspec/rspec-expectations/issues/219 [rspec-expectations#238]: https://github.com/rspec/rspec-expectations/issues/238 diff --git a/Manifest.txt b/Manifest.txt index 16a31b2..f059299 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -20,6 +20,7 @@ lib/diff/lcs/array.rb lib/diff/lcs/block.rb lib/diff/lcs/callbacks.rb lib/diff/lcs/change.rb +lib/diff/lcs/frozen_value.rb lib/diff/lcs/hunk.rb lib/diff/lcs/internals.rb lib/diff/lcs/ldiff.rb @@ -113,6 +114,7 @@ test/fixtures/old-chef2 test/test_block.rb test/test_change.rb test/test_diff.rb +test/test_frozen_value.rb test/test_helper.rb test/test_hunk.rb test/test_issues.rb diff --git a/lib/diff/lcs/change.rb b/lib/diff/lcs/change.rb index 737d677..01e83a6 100644 --- a/lib/diff/lcs/change.rb +++ b/lib/diff/lcs/change.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true -Diff::LCS::Change = Data.define(:action, :position, :element) # :nodoc: -Diff::LCS::ContextChange = Data.define(:action, :old_position, :old_element, :new_position, :new_element) # :nodoc: +require "diff/lcs/frozen_value" # Represents a simplistic (non-contextual) change. Represents the removal or addition of # an element from either the old or the new sequenced enumerable. class Diff::LCS::Change + include Diff::LCS::FrozenValue + # The only actions valid for changes are '+' (add), '-' (delete), '=' (no change), '!' # (changed), '<' (tail changes from first sequence), or '>' (tail changes from second # sequence). The last two ('<>') are only found with Diff::LCS::diff and @@ -14,23 +15,37 @@ class Diff::LCS::Change def self.valid_action?(action) = VALID_ACTIONS.include?(action) - ## + MEMBERS = %i[action position element].freeze + private_constant :MEMBERS + + # Returns the member names of a Change, in the order of its positional arguments. + def self.members = MEMBERS.dup + # Returns the action this Change represents. - # :attr_reader: action + attr_reader :action - ## # Returns the position of the Change. - # :attr_reader: position + attr_reader :position - ## # Returns the sequence element of the Change. - # :attr_reader: element + attr_reader :element + + # :call-seq: + # new(action, position, element) -> change + # new(action:, position:, element:) -> change + # + # Returns a new frozen Change. `action` must be one of VALID_ACTIONS and `position` + # must be an Integer. + def initialize(action = NO_ARGUMENT, position = NO_ARGUMENT, element = NO_ARGUMENT) + action, position, element = arguments_to_values(action, position, element) if NO_ARGUMENT.equal?(element) - def initialize(action:, position:, element:) fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action) fail "Invalid Position Type" unless position.is_a?(Integer) - super + @action = action + @position = position + @element = element + freeze end def inspect(*_args) = "#<#{self.class}: #{to_a.inspect}>" @@ -41,6 +56,12 @@ def to_a = [action, position, element] def deconstruct_keys(_) = {action:, position:, element:} + protected def values # :nodoc: + [@action, @position, @element] + end + + private def member_names = MEMBERS + def self.from_a(arr) case arr in [action, [old_position, old_element], [new_position, new_element]] @@ -84,32 +105,51 @@ def finished_b? = action == "<" # Represents a contextual change. Contains the position and values of the elements in the # old and the new sequenced enumerable values as well as the action taken. class Diff::LCS::ContextChange - ## + include Diff::LCS::FrozenValue + + MEMBERS = %i[action old_position old_element new_position new_element].freeze + private_constant :MEMBERS + + # Returns the member names of a ContextChange, in the order of its positional arguments. + def self.members = MEMBERS.dup + # Returns the action this Change represents. - # :attr_reader: action + attr_reader :action - ## # Returns the old position being changed. - # :attr_reader: old_position - - ## - # Returns the new position being changed. - # :attr_reader: new_position + attr_reader :old_position - ## # Returns the old element being changed. - # :attr_reader: old_element + attr_reader :old_element + + # Returns the new position being changed. + attr_reader :new_position - ## # Returns the new element being changed. - # :attr_reader: new_element + attr_reader :new_element + + # :call-seq: + # new(action, old_position, old_element, new_position, new_element) -> context_change + # new(action:, old_position:, old_element:, new_position:, new_element:) -> context_change + # + # Returns a new frozen ContextChange. `action` must be one of Change::VALID_ACTIONS, + # and each position must be an Integer or `nil`. + def initialize(action = NO_ARGUMENT, old_position = NO_ARGUMENT, old_element = NO_ARGUMENT, new_position = NO_ARGUMENT, new_element = NO_ARGUMENT) + if NO_ARGUMENT.equal?(new_element) + action, old_position, old_element, new_position, new_element = + arguments_to_values(action, old_position, old_element, new_position, new_element) + end - def initialize(action:, old_position:, old_element:, new_position:, new_element:) fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action) fail "Invalid (Old) Position Type" unless old_position.nil? || old_position.is_a?(Integer) fail "Invalid (New) Position Type" unless new_position.nil? || new_position.is_a?(Integer) - super + @action = action + @old_position = old_position + @old_element = old_element + @new_position = new_position + @new_element = new_element + freeze end def to_a = [action, [old_position, old_element], [new_position, new_element]] @@ -118,20 +158,22 @@ def to_a = [action, [old_position, old_element], [new_position, new_element]] def deconstruct_keys(_) = {action:, old_position:, old_element:, new_position:, new_element:} + protected def values # :nodoc: + [@action, @old_position, @old_element, @new_position, @new_element] + end + + private def member_names = MEMBERS + def self.from_a(arr) = Diff::LCS::Change.from_a(arr) # Simplifies a context change for use in some diff callbacks. '<' actions are converted # to '-' and '>' actions are converted to '+'. def self.simplify(event) case event.action - when "-" - event.with(new_element: nil) - when "<" - event.with(action: "-", new_element: nil) - when "+" - event.with(old_element: nil) - when ">" - event.with(action: "+", old_element: nil) + when "-", "<" + event.class.new("-", event.old_position, event.old_element, event.new_position, nil) + when "+", ">" + event.class.new("+", event.old_position, nil, event.new_position, event.new_element) else event end diff --git a/lib/diff/lcs/frozen_value.rb b/lib/diff/lcs/frozen_value.rb new file mode 100644 index 0000000..1c90042 --- /dev/null +++ b/lib/diff/lcs/frozen_value.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module Diff; end unless defined? Diff +module Diff::LCS; end unless defined? Diff::LCS + +# The Data API (keyword or positional construction, `members`, `to_h`, `with`, `eql?`, +# `hash`, frozen instances) for Diff::LCS::Change and Diff::LCS::ContextChange. They are +# built for each element of a traversal, and a Data subclass validating in `initialize` +# allocates three more objects per instance than a plain class (ruby-core#22437). +# +# An including class defines a class-level `members` returning a new Array, like Data, a +# private `member_names` returning the same names without a copy, `values`, +# `deconstruct_keys`, readers, and an `initialize` taking one positional argument per +# member, each defaulting to NO_ARGUMENT, which calls `arguments_to_values` when its last +# argument is NO_ARGUMENT, then freezes. +module Diff::LCS::FrozenValue # :nodoc: + NO_ARGUMENT = Object.new.freeze + private_constant :NO_ARGUMENT + + def self.included(base) + base.singleton_class.alias_method :[], :new + end + + def members = self.class.members + + def to_h(&) = deconstruct_keys(nil).to_h(&) + + def with(**updates) + return self if updates.empty? + + self.class.new(**to_h.merge!(updates)) + end + + def eql?(other) = self.class == other.class && values.eql?(other.values) + + def hash = [self.class, values].hash + + def inspect = "#<#{self.class} #{to_h.map { |member, value| "#{member}=#{value.inspect}" }.join(", ")}>" + + alias_method :to_s, :inspect + + # Psych only calls `encode_with` and `init_with` when they are public. + def encode_with(coder) = to_h.each { |member, value| coder[member.to_s] = value } + + # Like `marshal_load`, goes through `initialize`, so loaded values are validated and + # frozen. + def init_with(coder) = initialize(**coder.map.transform_keys { _1.is_a?(String) ? _1.to_sym : _1 }) + + private + + # `allocate` must stay defined: Psych calls it before `init_with`, and TruffleRuby's + # Marshal before `marshal_load`. + def marshal_load(values) = initialize(*values) + + def marshal_dump = values + + def initialize_copy(*) = super.freeze + + def arguments_to_values(*arguments) + arguments.pop while NO_ARGUMENT.equal?(arguments.last) + keywords = arguments.first if arguments.size == 1 && arguments.first.is_a?(Hash) + + # Fewer positional arguments than members: the ones left out are always missing. + missing = keywords ? member_names.reject { keywords.key?(_1) } : member_names.drop(arguments.size) + raise_keywords_error("missing", missing) if missing.any? + raise_keywords_error("unknown", keywords.keys - member_names) if keywords.size > member_names.size + + keywords.values_at(*member_names) + end + + def raise_keywords_error(kind, keywords) + raise ArgumentError, "#{kind} keyword#{"s" if keywords.size > 1}: #{keywords.map(&:inspect).join(", ")}" + end +end diff --git a/test/test_change.rb b/test/test_change.rb index 6f51ae2..54a6cb1 100644 --- a/test/test_change.rb +++ b/test/test_change.rb @@ -215,20 +215,24 @@ def test_spaceship_element_precedence end def test_simplify - simplify("-", 5, "old", 10, "new") => {action:, new_element:} - assert_equal action, "-" - assert_nil new_element + assert_equal change("-", 5, "old", 10, nil), simplify("-", 5, "old", 10, "new") + assert_equal change("-", 5, "old", 10, nil), simplify("<", 5, "old", 10, "new") + assert_equal change("+", 5, nil, 10, "new"), simplify("+", 5, "old", 10, "new") + assert_equal change("+", 5, nil, 10, "new"), simplify(">", 5, "old", 10, "new") + end + + def test_simplify_returns_unchanged_and_changed_events_as_they_are + %w[= !].each do |action| + event = change(action, 5, "old", 10, "new") - simplify("<", 5, "old", 10, "new") => {action:, new_element:} - assert_equal action, "-" - assert_nil new_element + assert_same event, Diff::LCS::ContextChange.simplify(event) + end + end - simplify("+", 5, "old", 10, "new") => {action:, old_element:} - assert_equal action, "+" - assert_nil old_element + def test_simplify_keeps_the_class_of_the_event + subclass = Class.new(Diff::LCS::ContextChange) - simplify(">", 5, "old", 10, "new") => {action:, old_element:} - assert_equal action, "+" - assert_nil old_element + assert_instance_of subclass, Diff::LCS::ContextChange.simplify(subclass.new("<", 5, "old", 10, "new")) + assert_instance_of subclass, Diff::LCS::ContextChange.simplify(subclass.new(">", 5, "old", 10, "new")) end end diff --git a/test/test_frozen_value.rb b/test/test_frozen_value.rb new file mode 100644 index 0000000..a097f08 --- /dev/null +++ b/test/test_frozen_value.rb @@ -0,0 +1,155 @@ +# frozen_string_literal: true + +require "test_helper" +require "yaml" + +# Diff::LCS::Change and ContextChange keep the API they had as Data classes, checked +# against Data classes with the same members. +class TestFrozenValue < Minitest::Test + CHANGE = Diff::LCS::Change + CONTEXT_CHANGE = Diff::LCS::ContextChange + DATA_BY_CLASS = { + CHANGE => Data.define(*CHANGE.members), + CONTEXT_CHANGE => Data.define(*CONTEXT_CHANGE.members) + }.freeze + + def context_change = CONTEXT_CHANGE.new("!", 1, "old", 2, "new") + + # The members (`to_h`) of the value built by `operation` on `klass`, or the message of + # the ArgumentError it raised. + def outcome(klass, &operation) + operation.call(klass).to_h + rescue ArgumentError => exception + exception.message + end + + def assert_outcome_matches_data(klass, &operation) + assert_equal outcome(DATA_BY_CLASS.fetch(klass), &operation), outcome(klass, &operation) + end + + def test_construction_matches_data + assert_outcome_matches_data(CHANGE) { _1.new("+", 0, "a") } + assert_outcome_matches_data(CHANGE) { _1["+", 0, "a"] } + assert_outcome_matches_data(CHANGE) { _1.new(action: "+", position: 0, element: "a") } + assert_outcome_matches_data(CHANGE) { _1[element: "a", position: 0, action: "+"] } + assert_outcome_matches_data(CHANGE) { _1.new("+", 0) } + assert_outcome_matches_data(CHANGE) { _1.new } + assert_outcome_matches_data(CHANGE) { _1.new(action: "+", position: 0, element: "a", extra: 1) } + assert_outcome_matches_data(CONTEXT_CHANGE) { _1.new("!", 1, "old", 2, "new") } + assert_outcome_matches_data(CONTEXT_CHANGE) { _1.new(action: "+", old_position: 0, new_element: "a") } + assert_raises(ArgumentError) { CHANGE.new("+", 0, "a", "b") } + end + + def test_with_matches_data + [ + [CHANGE, ["+", 0, "a"], {}], + [CHANGE, ["+", 0, "a"], {position: 1}], + [CHANGE, ["+", 0, "a"], {element: nil, action: "-"}], + [CHANGE, ["+", 0, "a"], {extra: 1}], + [CONTEXT_CHANGE, ["<", 1, "old", 2, "new"], {action: "-", new_element: nil}], + [CONTEXT_CHANGE, ["<", 1, "old", 2, "new"], {position: 3}] + ].each do |klass, values, updates| + assert_outcome_matches_data(klass) { _1.new(*values).with(**updates) } + end + + assert_outcome_matches_data(CHANGE) { _1.new("+", 0, "a").with(3) } + end + + def test_with_validates_and_returns_self_without_changes + change = context_change + + assert_same change, change.with + assert_raises_with_message RuntimeError, "Invalid Change Action '~'" do + change.with(action: "~") + end + end + + def test_keyword_construction_validates + assert_raises_with_message RuntimeError, "Invalid Position Type" do + CHANGE.new(action: "+", position: "0", element: "a") + end + end + + def test_members_and_to_h + assert_equal %i[action position element], CHANGE.members + assert_equal %i[action old_position old_element new_position new_element], context_change.members + CHANGE.members << :extra + context_change.members << :extra + assert_equal %i[action position element], CHANGE.members + refute_respond_to context_change, :marshal_dump + assert_equal({"action" => "+", "position" => 0, "element" => "a"}, + CHANGE.new("+", 0, "a").to_h { |member, value| [member.to_s, value] }) + end + + def test_to_h_returns_a_hash_of_its_own + change = CHANGE.new("+", 0, "a") + change.to_h[:position] = 1 + + assert_equal({action: "+", position: 0, element: "a"}, change.to_h) + end + + # Telling these apart from a Hash given as the last member would take a keyword + # parameter, which allocates on every positional call. + def test_a_hash_as_only_or_last_argument_is_not_rejected_like_data + assert_equal CHANGE.new("+", 0, "a"), CHANGE.new({action: "+", position: 0, element: "a"}) + assert_equal({element: "a"}, CHANGE.new("+", 0, element: "a").element) + end + + def test_value_equality_as_hash_keys + counts = Hash.new(0) + 2.times { counts[CONTEXT_CHANGE.new("!", 1, "old", 2, "new")] += 1 } + counts[CONTEXT_CHANGE.new("!", 1, "old", 2, "other")] += 1 + + assert_equal [2, 1], counts.values + refute CHANGE.new("+", 1, 1).eql?(CHANGE.new("+", 1, 1.0)) + refute CHANGE.new("+", 1, 1).eql?(CONTEXT_CHANGE.new("+", 1, 1, 1, 1)) + end + + def test_instances_and_their_copies_are_frozen + change = CHANGE.new("+", 0, "a") + + assert_predicate change, :frozen? + assert_predicate change.dup, :frozen? + assert_predicate change.clone(freeze: false), :frozen? + assert_raises(FrozenError) { change.instance_variable_set(:@position, 1) } + assert_raises(FrozenError) { change.send(:marshal_load, ["-", 1, "b"]) } + end + + def test_marshal_round_trip + copy = Marshal.load(Marshal.dump(context_change)) + + assert_equal context_change, copy + assert_predicate copy, :frozen? + end + + def test_yaml_round_trip + copy = YAML.unsafe_load(YAML.dump(context_change)) + + assert_equal context_change, copy + assert_predicate copy, :frozen? + assert_raises_with_message RuntimeError, "Invalid Change Action '~'" do + YAML.unsafe_load(YAML.dump(CHANGE.new("+", 0, "a")).sub('"+"', '"~"')) + end + end + + def test_yaml_load_requires_exactly_the_members + dump = YAML.dump(CHANGE.new("+", 0, "a")) + + assert_raises_with_message ArgumentError, "missing keyword: :element" do + YAML.unsafe_load(dump.sub(/^element: .*\n/, "")) + end + assert_raises_with_message ArgumentError, "unknown keyword: :extra" do + YAML.unsafe_load("#{dump}extra: 1\n") + end + assert_raises_with_message ArgumentError, "unknown keyword: 1" do + YAML.unsafe_load("#{dump}1: 2\n") + end + end + + def test_inspect_and_to_s + assert_equal '#', context_change.to_s + assert_equal '#', CHANGE.new("+", 0, "a").inspect + assert_equal '#', CHANGE.new("+", 0, "a").to_s + end +end