Skip to content

[Source manager] Improve the performance of findBufferContainingLocInternal #68971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

DougGregor
Copy link
Member

This function was performing a linear scan through the set of known buffers to find the buffer containing a given source location. This linear scan can show up in hot loops, and the number of buffers in a program is increasing due to macros, so this has become a performance problem.

Replace the linear scan with a logarithmic lookup into a sorted vector of the buffer IDs, with a one-element most-recently-used cache so that repeated lookups in the same buffer require constant time.

This mirrors what we already do with source files in a module. Unfortunately, we cannot reuse that code because there is no link from buffers to source files. We should look to consolidate this in the future.

Fixes rdar://116184248.

…ternal()

This function was performing a linear scan through the set of known
buffers to find the buffer containing a given source location. This
linear scan can show up in hot loops, and the number of buffers in a
program is increasing due to macros, so this has become a performance
problem.

Replace the linear scan with a logarithmic lookup into a sorted vector
of the buffer IDs, with a one-element most-recently-used cache so that
repeated lookups in the same buffer require constant time.

This mirrors what we already do with source files in a module.
Unfortunately, we cannot reuse that code because there is no link from
buffers to source files. We should look to consolidate this in the
future.

Fixes rdar://116184248.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test compiler performance

@DougGregor
Copy link
Member Author

Infrastructure failure in trying to post the performance results. Digging in, this didn't make a difference in aggregate, I suspect because none of the test projects there have a huge number of Swift files in a single target.

@DougGregor DougGregor merged commit 2e289f0 into swiftlang:main Oct 5, 2023
@DougGregor DougGregor deleted the faster-find-buffer-containing-loc branch October 5, 2023 14:02
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.

2 participants