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

How to clear an IntervalHeap for re-use? #155

Open
mastef opened this issue Sep 18, 2024 · 1 comment
Open

How to clear an IntervalHeap for re-use? #155

mastef opened this issue Sep 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@mastef
Copy link

mastef commented Sep 18, 2024

It would be nice to be able to clear an IntervalHeap for re-use, to reduce array allocs and to avoid GC.

Currently ( if I understood correctly ) it can be only cleared with running either while (!iHeap.IsEmpty) iHeap.DeleteMin(); or while (!iHeap.IsEmpty) iHeap.DeleteMax();, which seems less than optimal performance wise.

A iHeap.Clear() method could Array.Clear(heap, 0, size); and then set size = 0

@mastef mastef added the enhancement New feature or request label Sep 18, 2024
@mastef
Copy link
Author

mastef commented Sep 22, 2024

I've been testing now with this, and it seems to work nicely. It reduced a lot of GC by allowing to re-use interval heaps without having to create a new one:

public void Clear()
{
	stamp++;
	
	for (int i = 0; i < size; i++)
	{
		heap[i].first = default;
		heap[i].firsthandle = null;
		heap[i].last = default;
		heap[i].lasthandle = null;
	}
	
	size = 0;
}

( Using DeleteMin() in a loop was way too slow. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant