Commit a9eb593c authored by Adam Ettenberger's avatar Adam Ettenberger Committed by Commit Bot

Performance improvement for AXPosition<>::AsTreePosition

While investigating opportunities where AXRange<>::{begin|end}() can
be improved, I found that AsTreePosition was one of the higher cost
methods.

This is because it's called many times in order to create an
AXRange<>::Iterator. Both AXRange<>::AsForwardRange and the Iterator
constructor via AXPosition<>::AsLeafTextPosition call AsTreePosition.
The reason it's expensive is because of AXPosition<>::MaxTextOffset.

For the scenario I was measuring, this CL reduced the overall amount
of time spent in AXRange::begin by ~30-35%, and the overall amount of
time spent in AXPosition<>::AsTreePosition by ~50%.

I crafted a document where there are many inline nodes that form
sentences with various levels of nesting, and used Narrator with
UIA enabled to navigate 5 sentences.

I used "UI for ETW" to capture perf traces, and WPA to view the data.

Before :
  * Weight for callees of AXRange<>::begin : 7889
  * Weight for callees of AXPosition<>::AsTreePosition : 20418

After :
  * Weight for callees of AXRange<>::begin : 5246
  * Weight for callees of AXPosition<>::AsTreePosition : 10298

Bug: 928948
Change-Id: I24be1be302b9e22bc85412f091b5f464274f1dfb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1824064
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarKurt Catti-Schmidt <kschmi@microsoft.com>
Reviewed-by: default avatarNektarios Paisios <nektar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702075}
parent 99916154
...@@ -707,11 +707,14 @@ class AXPosition { ...@@ -707,11 +707,14 @@ class AXPosition {
AXPositionInstance copy = Clone(); AXPositionInstance copy = Clone();
DCHECK(copy); DCHECK(copy);
DCHECK_GE(copy->text_offset_, 0); DCHECK_GE(copy->text_offset_, 0);
if (!copy->AnchorChildCount() && if (!copy->AnchorChildCount()) {
copy->text_offset_ != copy->MaxTextOffset()) { const int max_text_offset = copy->MaxTextOffset();
copy->child_index_ = BEFORE_TEXT; copy->child_index_ =
} else { (max_text_offset != 0 && copy->text_offset_ != max_text_offset)
copy->child_index_ = 0; ? BEFORE_TEXT
: 0;
copy->kind_ = AXPositionKind::TREE_POSITION;
return copy;
} }
// Blink doesn't always remove all deleted whitespace at the end of a // Blink doesn't always remove all deleted whitespace at the end of a
...@@ -720,23 +723,22 @@ class AXPosition { ...@@ -720,23 +723,22 @@ class AXPosition {
// last child that we can reach with the current text offset and ignore any // last child that we can reach with the current text offset and ignore any
// remaining children. // remaining children.
int current_offset = 0; int current_offset = 0;
for (int i = 0; i < copy->AnchorChildCount(); ++i) { int child_index = 0;
AXPositionInstance child = copy->CreateChildPositionAt(i); for (; child_index < copy->AnchorChildCount(); ++child_index) {
AXPositionInstance child = copy->CreateChildPositionAt(child_index);
DCHECK(child); DCHECK(child);
int child_length = child->MaxTextOffsetInParent(); int child_length = child->MaxTextOffsetInParent();
if (copy->text_offset_ >= current_offset && if (copy->text_offset_ >= current_offset &&
(copy->text_offset_ < (current_offset + child_length) || (copy->text_offset_ < (current_offset + child_length) ||
(copy->affinity_ == ax::mojom::TextAffinity::kUpstream && (copy->affinity_ == ax::mojom::TextAffinity::kUpstream &&
copy->text_offset_ == (current_offset + child_length)))) { copy->text_offset_ == (current_offset + child_length)))) {
copy->child_index_ = i;
break; break;
} }
current_offset += child_length; current_offset += child_length;
} }
if (current_offset >= copy->MaxTextOffset())
copy->child_index_ = copy->AnchorChildCount();
copy->child_index_ = child_index;
copy->kind_ = AXPositionKind::TREE_POSITION; copy->kind_ = AXPositionKind::TREE_POSITION;
return copy; return copy;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment