-
Notifications
You must be signed in to change notification settings - Fork 275
Reduce memory allocations by using bitset instead of sync.map #2284
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
Draft
djeebus
wants to merge
8
commits into
main
Choose a base branch
from
improve-dirty-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+521
−11
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f39bc3b
reduce memory allocations by using bitset instead of sync.map
djeebus 70694a8
add a benchmark
djeebus 856b620
chore: auto-commit generated changes
github-actions[bot] 0722353
bring back old behavior, add tests to verify
djeebus 3d1c463
chore: auto-commit generated changes
github-actions[bot] 6a120a7
linting and fixes
djeebus 8134a74
add some more unlikely edge cases, fix benchmarks
djeebus d52f1fb
Merge
ValentaTomas 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
Some comments aren't visible on the classic Files Changed page.
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
177 changes: 177 additions & 0 deletions
177
packages/orchestrator/pkg/sandbox/block/cache_bench_test.go
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,177 @@ | ||
| package block | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/storage/header" | ||
| ) | ||
|
|
||
| // BenchmarkDirtyTracking benchmarks the dirty tracking operations | ||
| // which were changed from sync.Map to bitset.BitSet. | ||
| func BenchmarkDirtyTracking(b *testing.B) { | ||
| const ( | ||
| blockSize = int64(header.PageSize) // 4KB blocks | ||
| cacheSize = 1024 * 1024 * 1024 // 1GB cache = 262144 blocks | ||
| ) | ||
|
|
||
| tmpFile := b.TempDir() + "/bench_cache" | ||
|
|
||
| cache, err := NewCache(cacheSize, blockSize, tmpFile, false) | ||
| if err != nil { | ||
| b.Fatalf("failed to create cache: %v", err) | ||
| } | ||
| defer cache.Close() | ||
|
|
||
| // Simulate write data | ||
| data := make([]byte, blockSize) | ||
|
|
||
| b.Run("SetIsCached", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := range b.N { | ||
| // Simulate marking a block as cached | ||
| off := int64(i%262144) * blockSize | ||
| cache.setIsCached(off, blockSize) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("IsCached_Hit", func(b *testing.B) { | ||
| // Pre-populate some blocks as cached | ||
| for i := range int64(1000) { | ||
| cache.setIsCached(i*blockSize, blockSize) | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := range b.N { | ||
| off := int64(i%1000) * blockSize | ||
| cache.isCached(off, blockSize) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("IsCached_Miss", func(b *testing.B) { | ||
| // Use a fresh cache to ensure we're measuring actual misses, | ||
| // not hits from blocks populated by previous sub-benchmarks | ||
| missCache, err := NewCache(cacheSize, blockSize, b.TempDir()+"/bench_cache_miss", false) | ||
| if err != nil { | ||
| b.Fatalf("failed to create miss cache: %v", err) | ||
| } | ||
| defer missCache.Close() | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := range b.N { | ||
| off := int64(i%262144) * blockSize | ||
| missCache.isCached(off, blockSize) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("WriteAt_SingleBlock", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := range b.N { | ||
| off := int64(i%262144) * blockSize | ||
| cache.WriteAt(data, off) | ||
| } | ||
| }) | ||
|
|
||
| b.Run("WriteAt_MultiBlock", func(b *testing.B) { | ||
| // Write spanning 4 blocks | ||
| multiBlockData := make([]byte, blockSize*4) | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for i := range b.N { | ||
djeebus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| off := int64(i%65536) * blockSize * 4 | ||
| cache.WriteAt(multiBlockData, off) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // BenchmarkDirtySortedKeys benchmarks the dirtySortedKeys operation | ||
| // used during export. | ||
| func BenchmarkDirtySortedKeys(b *testing.B) { | ||
| const ( | ||
| blockSize = int64(header.PageSize) | ||
| cacheSize = 1024 * 1024 * 1024 // 1GB | ||
| ) | ||
|
|
||
| tmpFile := b.TempDir() + "/bench_cache" | ||
|
|
||
| cache, err := NewCache(cacheSize, blockSize, tmpFile, false) | ||
| if err != nil { | ||
| b.Fatalf("failed to create cache: %v", err) | ||
| } | ||
| defer cache.Close() | ||
|
|
||
| // Mark 10% of blocks as dirty (26214 blocks) | ||
| for i := range int64(26214) { | ||
| cache.setIsCached(i*blockSize, blockSize) | ||
| } | ||
|
|
||
| b.Run("DirtySortedKeys_10pct", func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| keys := cache.dirtySortedKeys() | ||
| _ = keys | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // BenchmarkCacheCreation benchmarks cache creation overhead. | ||
| func BenchmarkCacheCreation(b *testing.B) { | ||
| const ( | ||
| blockSize = int64(header.PageSize) | ||
| ) | ||
|
|
||
| sizes := []int64{ | ||
| 1 * 1024 * 1024, // 1MB | ||
| 100 * 1024 * 1024, // 100MB | ||
| 1024 * 1024 * 1024, // 1GB | ||
| 10 * 1024 * 1024 * 1024, // 10GB | ||
| } | ||
|
|
||
| for _, size := range sizes { | ||
| name := formatSize(size) | ||
| b.Run(name, func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| tmpFile := b.TempDir() + "/bench_cache" | ||
| cache, err := NewCache(size, blockSize, tmpFile, false) | ||
| if err != nil { | ||
| b.Fatalf("failed to create cache: %v", err) | ||
| } | ||
| cache.Close() | ||
| os.RemoveAll(tmpFile) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func formatSize(bytes int64) string { | ||
| const ( | ||
| KB = 1024 | ||
| MB = 1024 * KB | ||
| GB = 1024 * MB | ||
| ) | ||
|
|
||
| switch { | ||
| case bytes >= GB: | ||
| return fmt.Sprintf("%dGB", bytes/GB) | ||
| case bytes >= MB: | ||
| return fmt.Sprintf("%dMB", bytes/MB) | ||
| default: | ||
| return fmt.Sprintf("%dKB", bytes/KB) | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.