Skip to content

Commit

Permalink
LibWeb: Elide boundary checks when constructing find in page ranges
Browse files Browse the repository at this point in the history
Previously, unnecessary boundary checks were being done when
constructing the range objects used to represent find in page matches.
These checks are no longer performed leading to a significant speedup
when performing find in page queries on pages containing a lot of text.

(cherry picked from commit e76ad9492e0992f9a6d821240bea2b62f27a03c8)
  • Loading branch information
tcl3 authored and nico committed Nov 1, 2024
1 parent 7d472b5 commit 716bf5c
Showing 1 changed file with 1 addition and 4 deletions.
5 changes: 1 addition & 4 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5219,20 +5219,17 @@ Vector<JS::Handle<DOM::Range>> Document::find_matching_text(String const& query,
for (; i < text_block.positions.size() - 1 && match_index.value() > text_block.positions[i + 1].start_offset; ++i)
match_start_position = &text_block.positions[i + 1];

auto range = create_range();
auto start_position = match_index.value() - match_start_position->start_offset;
auto& start_dom_node = match_start_position->dom_node;
(void)range->set_start(start_dom_node, start_position);

auto* match_end_position = match_start_position;
for (; i < text_block.positions.size() - 1 && (match_index.value() + query.bytes_as_string_view().length() > text_block.positions[i + 1].start_offset); ++i)
match_end_position = &text_block.positions[i + 1];

auto& end_dom_node = match_end_position->dom_node;
auto end_position = match_index.value() + query.bytes_as_string_view().length() - match_end_position->start_offset;
(void)range->set_end(end_dom_node, end_position);

matches.append(range);
matches.append(Range::create(start_dom_node, start_position, end_dom_node, end_position));
match_start_position = match_end_position;
offset = match_index.value() + query.bytes_as_string_view().length() + 1;
if (offset >= text.bytes_as_string_view().length())
Expand Down

0 comments on commit 716bf5c

Please sign in to comment.