Skip to content

Commit 3b3f8d9

Browse files
authored
refactor(header): replace IsEmptyBlock with fast IsZero short-circuit (#2547)
`IsEmptyBlock` was the per-block hot path of `DiffMetadataBuilder.Process` — one call per block during the rootfs scan in `DirectProvider.exportToDiff`. It paid a full SIMD `memcmp` (4 KiB or, on hugepages, 2 MiB) before it could short-circuit, and required two static zero buffers (`EmptyBlock`, `EmptyHugePage`) to compare against. Replace it with a small `IsZero` helper that uses QEMU's `buffer_is_zero` trick: sample three bytes (first, last, middle) so most non-zero blocks reject from a single cache line. The fallback uses `bytes.Equal` on `b` shifted by one (`b[1:] == b[:n-1]`), which dispatches to the runtime's SIMD `memequal` on amd64/arm64 — same speed as the old comparison against a static buffer when the short-circuit doesn't fire. `DiffMetadataBuilder.Process` calls `IsZero` directly (the size validation `IsEmptyBlock` did was unreachable in practice). `EmptyBlock` is removed; `EmptyHugePage` stays — `build.go` uses it as the literal zero buffer for `uuid.Nil` reads in `Build.ReadAt`. Pre-factored from #2546 so it can land on its own.
1 parent b2208ff commit 3b3f8d9

3 files changed

Lines changed: 42 additions & 20 deletions

File tree

packages/shared/pkg/storage/header/diff.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package header
22

33
import (
44
"bytes"
5-
"fmt"
65

76
"go.opentelemetry.io/otel"
87
)
@@ -15,21 +14,24 @@ const (
1514

1615
var tracer = otel.Tracer("github.com/e2b-dev/infra/packages/shared/pkg/storage/header")
1716

18-
var (
19-
EmptyHugePage = make([]byte, HugepageSize)
20-
EmptyBlock = make([]byte, RootfsBlockSize)
21-
)
17+
var EmptyHugePage = make([]byte, HugepageSize)
2218

23-
func IsEmptyBlock(block []byte, blockSize int64) (bool, error) {
24-
var emptyBuf []byte
25-
switch blockSize {
26-
case HugepageSize:
27-
emptyBuf = EmptyHugePage
28-
case RootfsBlockSize:
29-
emptyBuf = EmptyBlock
30-
default:
31-
return false, fmt.Errorf("block size not supported: %d", blockSize)
19+
// IsZero reports whether b is all-zero. Samples first/middle/last byte to
20+
// reject most non-zero buffers from one cache line, then falls back to
21+
// bytes.Equal(b[:n-1], b[1:]): true exactly when every adjacent pair of
22+
// bytes is equal, i.e. all bytes equal b[0] (which the sample already
23+
// proved is zero).
24+
func IsZero(b []byte) bool {
25+
n := len(b)
26+
if n == 0 {
27+
return true
28+
}
29+
if b[0]|b[n-1]|b[n/2] != 0 {
30+
return false
31+
}
32+
if n <= 3 {
33+
return true
3234
}
3335

34-
return bytes.Equal(block, emptyBuf), nil
36+
return bytes.Equal(b[:n-1], b[1:])
3537
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package header
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsZero(t *testing.T) {
10+
t.Parallel()
11+
12+
assert.True(t, IsZero(make([]byte, RootfsBlockSize)))
13+
assert.False(t, IsZero([]byte{0, 1, 0}), "middle byte is sampled")
14+
15+
// Non-zero byte buried where the 3-sample short-circuit cannot see it
16+
// must still be caught by the SIMD fallback.
17+
buf := make([]byte, RootfsBlockSize)
18+
buf[100] = 0xFF
19+
assert.False(t, IsZero(buf))
20+
21+
buf = make([]byte, RootfsBlockSize)
22+
buf[RootfsBlockSize-2] = 0xFF
23+
assert.False(t, IsZero(buf), "non-zero just before the last sampled byte")
24+
}

packages/shared/pkg/storage/header/metadata.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ func NewDiffMetadataBuilder(blockSize int64) *DiffMetadataBuilder {
132132
func (b *DiffMetadataBuilder) Process(ctx context.Context, block []byte, out io.Writer, offset int64) error {
133133
blockIdx := BlockIdx(offset, b.blockSize)
134134

135-
isEmpty, err := IsEmptyBlock(block, b.blockSize)
136-
if err != nil {
137-
return fmt.Errorf("error checking empty block: %w", err)
138-
}
139-
if isEmpty {
135+
if IsZero(block) {
140136
b.empty.Add(uint32(blockIdx))
141137

142138
return nil

0 commit comments

Comments
 (0)