Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Add Iterate for allocation-free iteration #10

Closed

Conversation

kevinconaway
Copy link

@kevinconaway kevinconaway commented May 30, 2021

This commit adds the Iterate method to the bitmap that accepts a callback function for each value in the bitmap. If the callback function returns false, the iteration is halted

This pattern allows for iterating the bitmap with 0 allocations:

name        time/op
Iterator-8  1.35ms ± 4%
Iterate-8    374µs ± 5%

name        alloc/op
Iterator-8  4.65MB ± 0%
Iterate-8    0.00B

name        allocs/op
Iterator-8    30.0 ± 0%
Iterate-8     0.00

This commit also fixes the existing BenchmarkIterate function to correctly benchmark the iterator


This change is Reviewable

This commit adds the `Iterate` method to the bitmap that accepts a callback function for each value in the bitmap.  If the callback function returns false, the iteration is halted

This pattern allows for iterating the bitmap with 0 allocations:

```
name        time/op
Iterator-8  1.35ms ± 4%
Iterate-8    374µs ± 5%

name        alloc/op
Iterator-8  4.65MB ± 0%
Iterate-8    0.00B

name        allocs/op
Iterator-8    30.0 ± 0%
Iterate-8     0.00
```

This commit also fixes the existing `BenchmarkIterate` function to correctly benchmark the iterator
@CLAassistant
Copy link

CLAassistant commented May 30, 2021

CLA assistant check
All committers have signed the CLA.

@kevinconaway
Copy link
Author

@ahsanbarkati thoughts here?

Copy link
Contributor

@ahsanbarkati ahsanbarkati left a comment

Choose a reason for hiding this comment

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

@kevinconaway The change looks good to me. We also want to make the current type based iterator to be able to run without allocations. The plan is to keep a couple of pointers (for the key and the index in the container) as the iterator state.

// Iterate iterates over the bitmap, calling the given callback with each value in the bitmap. If the callback returns
// false, the iteration is halted.
// The iteration results are undefined if the bitmap is modified (e.g., with Set or Remove).
// There is no guarantee as to what order the values will be iterated
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? The iteration will be in sorted order, right?

@ahsanbarkati
Copy link
Contributor

This change is no more needed. An iterator has been already implemented which does iteration without allocations and keeps the Next() API. Please try it out.
#19

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants