Skip to content

Conversation

@artur-abd
Copy link

@artur-abd artur-abd commented Nov 26, 2025

Improve reuse distance using splay tree instead of linked list. Splay tree have asymptotic O(log n) amortizated time for all operations instead of O(n / const) for linked list with skip list.
Perfomance improve achives in avarage 10-40% based on different benchmarks.

Fixes #7731

@artur-abd artur-abd changed the title i#7731: Replace linked list with splay tree i#7731: Optimize reuse distance collection using splay tree Nov 26, 2025
@artur-abd artur-abd marked this pull request as draft November 27, 2025 05:35
@artur-abd artur-abd force-pushed the i7731-optimize-reuse-distance branch 2 times, most recently from 4b6bffb to 3961c95 Compare November 27, 2025 13:24
Improve reuse distance using splay tree instead of linked list. Splay tree have asymptotic O(log n) amortizated time for all operations instead of O(n / const) for linked list with skip list.
Perfomance improve achives in avarage 10-40% based on different benchmarks.

Fixes DynamoRIO#7731
@artur-abd artur-abd force-pushed the i7731-optimize-reuse-distance branch from 3961c95 to 1b8aa74 Compare November 27, 2025 13:26
@artur-abd artur-abd marked this pull request as ready for review November 27, 2025 13:37
@derekbruening
Copy link
Contributor

Thank you for contributing. Please mark for review when ready. Also, please don't force-push a shared branch: see https://dynamorio.org/page_code_reviews.html about squash-and-merge, where the final merge commit message comes from, and how force pushes break the review process. Use separate incremental commits with commit messages that describe what each one is doing (and not just repeating the merge message).

Fixes description for line_ref_splay_t and it's methods

Fixes: DynamoRIO#7731
@artur-abd
Copy link
Author

artur-abd commented Dec 1, 2025

Thanks for your comments. PR is ready for review.
Tests for ci-windows / vs2022-64 (pull_request) is failed due to a timeout in other modules. Self triggered tests passed.

@abhinav92003 abhinav92003 self-requested a review December 2, 2025 19:47
// The head of the list is the most recently accessed cache line.
// The earlier a cache line was accessed last time, the deeper that cache line
// is in the list.
// We use a splay to keep track of the cache line reuse distance.
Copy link
Contributor

Choose a reason for hiding this comment

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

We use a splay to keep

splay tree

// The earlier a cache line was accessed last time, the deeper that cache line
// is in the list.
// We use a splay to keep track of the cache line reuse distance.
// The lefmost node of the splay tree is the most recently accessed cache line.
Copy link
Contributor

Choose a reason for hiding this comment

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

The lefmost node of the splay tree is the most recently accessed cache line.

I thought splay trees have the most recently accessed node closer to the root, not to the left.

: head_(NULL)
, gate_(NULL)
, tail_(NULL)
// The splay tree is a binary search tree that using splay operatio for balancing, it
Copy link
Contributor

Choose a reason for hiding this comment

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

The splay tree is a binary search tree that using splay operatio for balancing, it
allow to delete and insert an element at any position in O(log n) amortizated time.

Reword for grammar:

The splay tree is a binary search tree that uses the splay operation for balancing, which
allows deleting and inserting an element at any position in O(log n) amortized time.

return root;
}

// Find previos element of ref in the splay tree.
Copy link
Contributor

Choose a reason for hiding this comment

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

previos

previous

and also in other comments.

return nullptr;
// the last visited node
line_ref_node_t *last = nullptr;
// Walk up by tree wihle did not found a node to the left of the current
Copy link
Contributor

Choose a reason for hiding this comment

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

wihle did not found

while we do not find

, tail_(NULL)
// The splay tree is a binary search tree that using splay operatio for balancing, it
// allow to delete and insert an element at any position in O(log n) amortizated time.
struct line_ref_splay_t {
Copy link
Contributor

Choose a reason for hiding this comment

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

Partially done with reviewing the splay tree functions here. A couple high level comments:

  • I think it'll be useful to move this implementation to ext/drcontainers
  • We also need a dedicated set of unit tests for the splay tree impl. It is complex enough to warrant separate test coverage.

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.

drcachecim/reuse_distance Optimize perfomance

3 participants