Skip to content

Speed up do_classes_matches?#649

Merged
philss merged 1 commit into
philss:mainfrom
preciz:speed_up_do_classes_matches
Dec 17, 2025
Merged

Speed up do_classes_matches?#649
philss merged 1 commit into
philss:mainfrom
preciz:speed_up_do_classes_matches

Conversation

@preciz
Copy link
Copy Markdown
Contributor

@preciz preciz commented Dec 17, 2025

Introduced a guard check using String.contains?/2. If the class attribute does not contain the target class substring, we return false immediately, bypassing the expensive String.split/2 operation.

There is an average 1.5x speed up.

Bench script:

Mix.install([
  {:benchee, "~> 1.0"},
  {:floki, path: "."}
])

read_file = fn name ->
  __ENV__.file
  |> Path.dirname()
  |> Path.join(name)
  |> File.read!()
  |> Floki.parse_document!()
end

# Use big.html for a realistic large DOM
doc = read_file.("big.html")

IO.puts "Document parsed. Running benchmark..."

Benchee.run(
  %{
    "find .mw-redirect (common)" => fn -> Floki.find(doc, ".mw-redirect") end,
    "find .reference (frequent)" => fn -> Floki.find(doc, ".reference") end,
    "find .nonexistent (not found)" => fn -> Floki.find(doc, ".nonexistent12345") end,
    "find .citation (partial of .citation-comment)" => fn -> Floki.find(doc, ".citation") end
  },
  time: 5,
  memory_time: 2
)

Copilot AI review requested due to automatic review settings December 17, 2025 10:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a performance optimization for CSS class matching by adding a guard check using String.contains?/2 before performing the more expensive String.split/2 operation. If the class attribute doesn't contain the target class substring, the function returns false immediately, avoiding unnecessary string splitting and enumeration.

  • Adds String.contains?/2 guard check before String.split/2 in single-class matching
  • Maintains correctness by still performing full validation when substring is present
  • Targets the do_classes_matches?/2 function for single-class selectors

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@philss philss merged commit 2ee362f into philss:main Dec 17, 2025
12 checks passed
@philss
Copy link
Copy Markdown
Owner

philss commented Dec 17, 2025

@preciz good call. Thank you!

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