-
Notifications
You must be signed in to change notification settings - Fork 279
Replace bits-and-blooms/bitset with roaring bitmaps #2386
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
base: main
Are you sure you want to change the base?
Changes from all commits
b0717ba
b699cc1
b6dead0
feaff58
405f3d0
84c5813
fa35957
f9c537f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ import ( | |
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/bits-and-blooms/bitset" | ||
| "github.com/RoaringBitmap/roaring/v2" | ||
| "github.com/edsrzf/mmap-go" | ||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/attribute" | ||
|
|
@@ -117,7 +117,7 @@ func (c *Cache) ExportToDiff(ctx context.Context, out *os.File) (*header.DiffMet | |
| } | ||
|
|
||
| if c.mmap == nil { | ||
| return header.NewDiffMetadata(c.blockSize, bitset.New(0)), nil | ||
| return header.NewDiffMetadata(c.blockSize, roaring.New()), nil | ||
| } | ||
|
|
||
| f, err := os.Open(c.filePath) | ||
|
|
@@ -136,7 +136,7 @@ func (c *Cache) ExportToDiff(ctx context.Context, out *os.File) (*header.DiffMet | |
| logger.L().Warn(ctx, "error syncing file", zap.Error(err)) | ||
| } | ||
|
|
||
| diffMetadata := header.NewDiffMetadata(c.blockSize, c.dirty.BitSet()) | ||
| diffMetadata := header.NewDiffMetadata(c.blockSize, c.dirty.Bitmap()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0/5 nit: Do we need to clone under the RLock here, or (since we are holding |
||
|
|
||
| dst := int(out.Fd()) | ||
| var writeOffset int64 | ||
|
|
@@ -194,7 +194,7 @@ func (c *Cache) ExportToDiff(ctx context.Context, out *os.File) (*header.DiffMet | |
| telemetry.SetAttributes(ctx, | ||
| attribute.Int64("copy_ms", time.Since(copyStart).Milliseconds()), | ||
| attribute.Int64("total_size_bytes", c.size), | ||
| attribute.Int64("dirty_size_bytes", int64(diffMetadata.Dirty.Count())*c.blockSize), | ||
| attribute.Int64("dirty_size_bytes", int64(diffMetadata.Dirty.GetCardinality())*c.blockSize), | ||
| attribute.Int64("total_ranges", totalRanges), | ||
| ) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,21 @@ import ( | |
| "iter" | ||
| "sync" | ||
|
|
||
| "github.com/bits-and-blooms/bitset" | ||
| "github.com/RoaringBitmap/roaring/v2" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/storage/header" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/utils" | ||
| ) | ||
|
|
||
| type Tracker struct { | ||
| b *bitset.BitSet | ||
| b *roaring.Bitmap | ||
| mu sync.RWMutex | ||
|
|
||
| blockSize int64 | ||
| } | ||
|
|
||
| func NewTracker(blockSize int64) *Tracker { | ||
| return &Tracker{ | ||
| // The bitset resizes automatically based on the maximum set bit. | ||
| b: bitset.New(0), | ||
| b: roaring.New(), | ||
| blockSize: blockSize, | ||
| } | ||
| } | ||
|
|
@@ -29,27 +27,21 @@ func (t *Tracker) Has(off int64) bool { | |
| t.mu.RLock() | ||
| defer t.mu.RUnlock() | ||
|
|
||
| return t.b.Test(uint(header.BlockIdx(off, t.blockSize))) | ||
| return t.b.Contains(uint32(header.BlockIdx(off, t.blockSize))) | ||
| } | ||
|
|
||
| func (t *Tracker) Add(off int64) { | ||
| t.mu.Lock() | ||
| defer t.mu.Unlock() | ||
|
|
||
| t.b.Set(uint(header.BlockIdx(off, t.blockSize))) | ||
| t.b.Add(uint32(header.BlockIdx(off, t.blockSize))) | ||
| } | ||
|
|
||
| func (t *Tracker) Reset() { | ||
| t.mu.Lock() | ||
| defer t.mu.Unlock() | ||
|
|
||
| t.b.ClearAll() | ||
| } | ||
|
|
||
| // BitSet returns the bitset. | ||
| // This is not safe to use concurrently. | ||
| func (t *Tracker) BitSet() *bitset.BitSet { | ||
| return t.b | ||
| t.b.Clear() | ||
| } | ||
|
|
||
| func (t *Tracker) BlockSize() int64 { | ||
|
|
@@ -70,11 +62,11 @@ func (t *Tracker) Offsets() iter.Seq[int64] { | |
| t.mu.RLock() | ||
| defer t.mu.RUnlock() | ||
|
|
||
| return bitsetOffsets(t.b.Clone(), t.BlockSize()) | ||
| } | ||
| snapshot := t.b.Clone() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to clone here? |
||
|
|
||
| func bitsetOffsets(b *bitset.BitSet, blockSize int64) iter.Seq[int64] { | ||
| return utils.TransformTo(b.EachSet(), func(idx uint) int64 { | ||
| return header.BlockOffset(int64(idx), blockSize) | ||
| }) | ||
| return func(yield func(int64) bool) { | ||
| snapshot.Iterate(func(idx uint32) bool { | ||
| return yield(header.BlockOffset(int64(idx), t.blockSize)) | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import ( | |
| "fmt" | ||
| "runtime" | ||
|
|
||
| "github.com/bits-and-blooms/bitset" | ||
| "github.com/RoaringBitmap/roaring/v2" | ||
| "github.com/firecracker-microvm/firecracker-go-sdk" | ||
| "github.com/go-openapi/strfmt" | ||
|
|
||
|
|
@@ -451,8 +451,8 @@ func (c *apiClient) memoryInfo(ctx context.Context, blockSize int64) (*header.Di | |
| } | ||
|
|
||
| return &header.DiffMetadata{ | ||
| Dirty: bitset.From(res.Payload.Resident), | ||
| Empty: bitset.From(res.Payload.Empty), | ||
| Dirty: roaring.FromDense(res.Payload.Resident, false), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you explain why with |
||
| Empty: roaring.FromDense(res.Payload.Empty, false), | ||
| BlockSize: blockSize, | ||
| }, nil | ||
| } | ||
|
|
@@ -468,8 +468,8 @@ func (c *apiClient) dirtyMemory(ctx context.Context, blockSize int64) (*header.D | |
| } | ||
|
|
||
| return &header.DiffMetadata{ | ||
| Dirty: bitset.From(res.Payload.Bitmap), | ||
| Empty: bitset.New(0), | ||
| Dirty: roaring.FromDense(res.Payload.Bitmap, false), | ||
| Empty: roaring.New(), | ||
| BlockSize: blockSize, | ||
| }, nil | ||
| } | ||
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.
unrelated to the change, right?