Skip to content

perf: Remove unused length computation in List.myers_difference - #15753

Merged
josevalim merged 1 commit into
elixir-lang:mainfrom
tomciopp:perf/myers-diff
Aug 16, 2026
Merged

perf: Remove unused length computation in List.myers_difference#15753
josevalim merged 1 commit into
elixir-lang:mainfrom
tomciopp:perf/myers-diff

Conversation

@tomciopp

Copy link
Copy Markdown
Contributor

myers_difference_with_diff_script/3 computed
length(list1) + length(list2) and threaded it through find_script/4,
which never read it: the recursion is bounded by each_diagonal/5
finding the endpoint, which the Myers algorithm guarantees within
n + m iterations. Two full list traversals of dead work per call,
also paid by String.myers_difference/2 and ExUnit's diff output.

Assisted by: Claude Fable.

@tomciopp

Copy link
Copy Markdown
Contributor Author
Mix.install([{:benchee, "~> 1.4"}])

tag = System.get_env("TAG") || raise "set TAG, e.g. TAG=main or TAG=p03"
result_file = fn t -> Path.join(__DIR__, "p03_myers.#{t}.benchee") end
baselines = Path.wildcard(Path.join(__DIR__, "p03_myers.*.benchee")) -- [result_file.(tag)]

identical = Enum.to_list(1..1000)
small_edit = List.replace_at(identical, 500, :changed)
disjoint_a = Enum.to_list(1..200)
disjoint_b = Enum.to_list(1001..1200)

str_a = String.duplicate("abcdefghij", 100)
str_b = String.replace(str_a, "abcdefghij", "abcdeXghij", global: false)

Benchee.run(
  %{
    "identical 1000" => fn -> List.myers_difference(identical, identical) end,
    "one edit 1000" => fn -> List.myers_difference(identical, small_edit) end,
    "disjoint 200 (control)" => fn -> List.myers_difference(disjoint_a, disjoint_b) end,
    "String.myers_difference" => fn -> String.myers_difference(str_a, str_b) end
  },
  warmup: 1,
  time: 3,
  memory_time: 1,
  save: [path: result_file.(tag), tag: tag],
  load: baselines
)

@josevalim
josevalim merged commit 99b6970 into elixir-lang:main Aug 16, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants