Skip to content
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

Preventing resize overuse in SortVisibleSpans #1646

Merged

Conversation

colincornaby
Copy link
Contributor

Adding an if check to resize the buffers only when there is a risk of an overflow.

Every pass through is resizing the scratch buffers in SortVisibleSpans. Resize is completely deallocating and reallocating the buffers. This is resulting in a ~5% performance drag - so this change results in a ~5% performance improvement on the main loop.

It’s possible this is a result of 78c0b48 which moved this storage over to std. I don’t know if the Cyan array implementation would have handled this.

I don’t see any reason this would create issues - there doesn’t seem to be code that relies on the size of the vectors. The vectors only need to be correctly sized to fit all the data required.

Copy link
Member

@Hoikas Hoikas left a comment

Choose a reason for hiding this comment

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

Good catch! hsTArray::SetCount() would only reallocate if more elements were requested. It never deallocated. Cyan's code was more like using std::vector::reserve() instead of resize(), therefore. Like reserve(), SetCount() also does not initialize the newly created elements. Unfortunately, I'm uncertain if assigning to an index that hasn't been initialized is defined behavior. It works fine on release builds in MSVC, but it might assert in debug mode. So, this might be the best fix. Maybe @zrax can chime in here?

@colincornaby
Copy link
Contributor Author

I tried resize as an alternative when working on this - and it caused index out of range failures on macOS. I'd also be curious if there was a better way to do this.

This is also not the only code that was refactored to use resize. There are other uses even in this function. The use here was by far the highest contributors in the performance graph. I can do an analysis of the remaining uses.

@Hoikas
Copy link
Member

Hoikas commented Dec 31, 2024

I tried resize as an alternative when working on this - and it caused index out of range failures on macOS.

Just to make sure - do you mean that you tried reserve() (NOT resize())? If so, that would confirm my suspicion that this PR is probably the best fix.

@colincornaby
Copy link
Contributor Author

Yes, sorry, I tried reserve and it produced out of bounds errors.

Copy link
Member

@Hoikas Hoikas left a comment

Choose a reason for hiding this comment

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

Now that we have Linux CI working, it would be nice if this branch were rebased so we can get the green checkmark and merge it.

Adding an if check to resize the buffers only when there is a risk of an overflow.

Every pass through is resizing the scratch buffers in SortVisibleSpans. Resize is completely deallocating and reallocating the buffers. This is resulting in a ~5% performance drag - so this change results in a ~5% performance improvement on the main loop.

It’s possible this is a result of 78c0b48 which moved this storage over to std. I don’t know if the Cyan array implementation would have handled this.

I don’t see any reason this would create issues - there doesn’t seem to be code that relies on the size of the vectors. The vectors only need to be correctly sized to fit all the data required.
@colincornaby colincornaby force-pushed the Prevent-Sort-Visible-Spans-Reallocations branch from fc2aad9 to 14a636e Compare January 1, 2025 22:50
@colincornaby
Copy link
Contributor Author

I've rebased this branch.

Upon further review of the performance data - I think this actually produces closer to a 10% speed up on the main render loop. So this should be a pretty good improvement.

@Hoikas Hoikas merged commit 7308ccf into H-uru:master Jan 2, 2025
17 checks passed
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