-
Notifications
You must be signed in to change notification settings - Fork 250
Support 1.23 iterator in a backward-compatible way #475
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
+445
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| //go:build go1.23 | ||
| // +build go1.23 | ||
|
|
||
| package roaring | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "testing" | ||
| ) | ||
|
|
||
| func BenchmarkIterator123(b *testing.B) { | ||
| bm := NewBitmap() | ||
| domain := 100000000 | ||
| count := 10000 | ||
| for j := 0; j < count; j++ { | ||
| v := uint32(rand.Intn(domain)) | ||
| bm.Add(v) | ||
| } | ||
| i := IntIterator{} | ||
| expectedCardinality := bm.GetCardinality() | ||
| counter := uint64(0) | ||
|
|
||
| b.Run("simple iteration with alloc", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| i := bm.Iterator() | ||
| for i.HasNext() { | ||
| i.Next() | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| b.Run("simple iteration", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| i.Initialize(bm) | ||
| for i.HasNext() { | ||
| i.Next() | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| b.Run("values iteration", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| Values(bm)(func(_ uint32) bool { | ||
| counter++ | ||
| return true | ||
| }) | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| b.Run("reverse iteration with alloc", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| ir := bm.ReverseIterator() | ||
| for ir.HasNext() { | ||
| ir.Next() | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| ir := IntReverseIterator{} | ||
|
|
||
| b.Run("reverse iteration", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| ir.Initialize(bm) | ||
| for ir.HasNext() { | ||
| ir.Next() | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| b.Run("backward iteration", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| Backward(bm)(func(_ uint32) bool { | ||
| counter++ | ||
| return true | ||
| }) | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
|
|
||
| b.Run("many iteration with alloc", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| buf := make([]uint32, 1024) | ||
| im := bm.ManyIterator() | ||
| for n := im.NextMany(buf); n != 0; n = im.NextMany(buf) { | ||
| counter += uint64(n) | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| im := ManyIntIterator{} | ||
| buf := make([]uint32, 1024) | ||
|
|
||
| b.Run("many iteration", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| im.Initialize(bm) | ||
| for n := im.NextMany(buf); n != 0; n = im.NextMany(buf) { | ||
| counter += uint64(n) | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
|
|
||
| b.Run("values iteration 1.23", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| for range Values(bm) { | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
|
|
||
| b.Run("backward iteration 1.23", func(b *testing.B) { | ||
| for n := 0; n < b.N; n++ { | ||
| counter = 0 | ||
| for range Backward(bm) { | ||
| counter++ | ||
| } | ||
| } | ||
| b.StopTimer() | ||
| }) | ||
| if counter != expectedCardinality { | ||
| b.Fatalf("Cardinalities don't match: %d, %d", counter, expectedCardinality) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package roaring | ||
|
|
||
| func Values(b *Bitmap) func(func(uint32) bool) { | ||
| return func(yield func(uint32) bool) { | ||
| it := b.Iterator() | ||
| for it.HasNext() { | ||
| if !yield(it.Next()) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func Backward(b *Bitmap) func(func(uint32) bool) { | ||
| return func(yield func(uint32) bool) { | ||
| it := b.ReverseIterator() | ||
| for it.HasNext() { | ||
| if !yield(it.Next()) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| //go:build go1.23 | ||
| // +build go1.23 | ||
|
|
||
| package roaring | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestBackwardCount123(t *testing.T) { | ||
| array := []int{2, 63, 64, 65, 4095, 4096, 4097, 4159, 4160, 4161, 5000, 20000, 66666} | ||
| for _, testSize := range array { | ||
| b := New() | ||
| for i := uint32(0); i < uint32(testSize); i++ { | ||
| b.Add(i) | ||
| } | ||
|
|
||
| count := 0 | ||
| for range Values(b) { | ||
| count++ | ||
| } | ||
|
|
||
| assert.Equal(t, testSize, count) | ||
| } | ||
| } | ||
|
|
||
| func TestBackward123(t *testing.T) { | ||
| t.Run("#1", func(t *testing.T) { | ||
| values := []uint32{0, 2, 15, 16, 31, 32, 33, 9999, MaxUint16, MaxUint32} | ||
| b := New() | ||
| for n := 0; n < len(values); n++ { | ||
| b.Add(values[n]) | ||
| } | ||
| n := len(values) - 1 | ||
| for val := range Backward(b) { | ||
| assert.EqualValues(t, val, values[n]) | ||
| n-- | ||
| } | ||
| }) | ||
|
|
||
| t.Run("#2", func(t *testing.T) { | ||
| b := New() | ||
|
|
||
| count := 0 | ||
| for range Backward(b) { | ||
| count++ | ||
| } | ||
|
|
||
| assert.Equal(t, 0, count) | ||
| }) | ||
|
|
||
| t.Run("#3", func(t *testing.T) { | ||
| b := New() | ||
| b.AddInt(0) | ||
|
|
||
| // only one value zero | ||
| for val := range Backward(b) { | ||
| assert.EqualValues(t, 0, val) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("#4", func(t *testing.T) { | ||
| b := New() | ||
| b.AddInt(9999) | ||
|
|
||
| // only one value 9999 | ||
| for val := range Backward(b) { | ||
| assert.EqualValues(t, 9999, val) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("#5", func(t *testing.T) { | ||
| b := New() | ||
| b.AddInt(MaxUint16) | ||
|
|
||
| // only one value MaxUint16 | ||
| for val := range Backward(b) { | ||
| assert.EqualValues(t, MaxUint16, val) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("#6", func(t *testing.T) { | ||
| b := New() | ||
| b.AddInt(MaxUint32) | ||
|
|
||
| // only one value MaxUint32 | ||
| for val := range Backward(b) { | ||
| assert.EqualValues(t, MaxUint32, val) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestValues123(t *testing.T) { | ||
| b := New() | ||
|
|
||
| testSize := 5000 | ||
| for i := 0; i < testSize; i++ { | ||
| b.AddInt(i) | ||
| } | ||
|
|
||
| n := 0 | ||
| for val := range Values(b) { | ||
| assert.Equal(t, uint32(n), val) | ||
| n++ | ||
|
|
||
| } | ||
|
|
||
| assert.Equal(t, testSize, n) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An int might be a signed 32-bit integer, so MaxUint32 can't be represented.
But when it works, you might be allocating 512 MB of memory. Please don't do this as part of our routine tests.