Skip to content

Commit 635423e

Browse files
committed
constants: remove package
The constant expression `min(math.MaxUint32, math.MaxInt)` can just be used directly instead (and is better self-documenting).
1 parent 273e266 commit 635423e

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

internal/arenaskl/arena.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package arenaskl
1919

2020
import (
21+
"math"
2122
"sync/atomic"
2223
"unsafe"
2324

2425
"github.com/cockroachdb/errors"
25-
"github.com/cockroachdb/pebble/internal/constants"
2626
"github.com/cockroachdb/pebble/internal/invariants"
2727
)
2828

@@ -40,14 +40,16 @@ var (
4040
ErrArenaFull = errors.New("allocation failed because arena is full")
4141
)
4242

43+
const MaxArenaSize = min(math.MaxUint32, math.MaxInt)
44+
4345
// NewArena allocates a new arena using the specified buffer as the backing
4446
// store.
4547
func NewArena(buf []byte) *Arena {
46-
if len(buf) > constants.MaxUint32OrInt {
48+
if len(buf) > MaxArenaSize {
4749
if invariants.Enabled {
4850
panic(errors.AssertionFailedf("attempting to create arena of size %d", len(buf)))
4951
}
50-
buf = buf[:constants.MaxUint32OrInt]
52+
buf = buf[:MaxArenaSize]
5153
}
5254
a := &Arena{
5355
buf: buf,
@@ -61,10 +63,10 @@ func NewArena(buf []byte) *Arena {
6163
// Size returns the number of bytes allocated by the arena.
6264
func (a *Arena) Size() uint32 {
6365
s := a.n.Load()
64-
if s > constants.MaxUint32OrInt {
66+
if s > MaxArenaSize {
6567
// The last failed allocation can push the size higher than len(a.buf).
6668
// Saturate at the maximum representable offset.
67-
return constants.MaxUint32OrInt
69+
return MaxArenaSize
6870
}
6971
return uint32(s)
7072
}

internal/arenaskl/arena_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"math"
2222
"testing"
2323

24-
"github.com/cockroachdb/pebble/internal/constants"
2524
"github.com/stretchr/testify/require"
2625
)
2726

@@ -32,7 +31,7 @@ func newArena(n uint32) *Arena {
3231
// TestArenaSizeOverflow tests that large allocations do not cause Arena's
3332
// internal size accounting to overflow and produce incorrect results.
3433
func TestArenaSizeOverflow(t *testing.T) {
35-
a := newArena(constants.MaxUint32OrInt)
34+
a := newArena(MaxArenaSize)
3635

3736
// Allocating under the limit throws no error.
3837
offset, err := a.alloc(math.MaxUint16, 1, 0)
@@ -44,10 +43,10 @@ func TestArenaSizeOverflow(t *testing.T) {
4443
// overflow if 32-bit arithmetic was used. It shouldn't.
4544
_, err = a.alloc(math.MaxUint32, 1, 0)
4645
require.Equal(t, ErrArenaFull, err)
47-
require.Equal(t, uint32(constants.MaxUint32OrInt), a.Size())
46+
require.Equal(t, uint32(MaxArenaSize), a.Size())
4847

4948
// Continuing to allocate continues to throw an error.
5049
_, err = a.alloc(math.MaxUint16, 1, 0)
5150
require.Equal(t, ErrArenaFull, err)
52-
require.Equal(t, uint32(constants.MaxUint32OrInt), a.Size())
51+
require.Equal(t, uint32(MaxArenaSize), a.Size())
5352
}

internal/batchskl/skl.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ import (
6363

6464
"github.com/cockroachdb/errors"
6565
"github.com/cockroachdb/pebble/internal/base"
66-
"github.com/cockroachdb/pebble/internal/constants"
6766
)
6867

6968
const (
7069
maxHeight = 20
7170
maxNodeSize = uint64(unsafe.Sizeof(node{}))
7271
linksSize = uint64(unsafe.Sizeof(links{}))
73-
maxNodesSize = constants.MaxUint32OrInt
72+
maxNodesSize = min(math.MaxUint32, math.MaxInt)
7473
)
7574

7675
var (

internal/constants/constants.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

internal/constants/constants_test.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

open.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/cockroachdb/pebble/internal/arenaskl"
2424
"github.com/cockroachdb/pebble/internal/base"
2525
"github.com/cockroachdb/pebble/internal/cache"
26-
"github.com/cockroachdb/pebble/internal/constants"
2726
"github.com/cockroachdb/pebble/internal/invariants"
2827
"github.com/cockroachdb/pebble/internal/keyspan"
2928
"github.com/cockroachdb/pebble/internal/manifest"
@@ -49,7 +48,7 @@ const (
4948
//
5049
// On 32-bit systems, slices are naturally limited to MaxInt (just short of
5150
// 2GB).
52-
maxBatchSize = constants.MaxUint32OrInt
51+
maxBatchSize = min(math.MaxUint32, math.MaxInt)
5352

5453
// The max memtable size is limited by the uint32 offsets stored in
5554
// internal/arenaskl.node, DeferredBatchOp, and flushableBatchEntry.
@@ -59,7 +58,7 @@ const (
5958
//
6059
// On 32-bit systems, slices are naturally limited to MaxInt (just short of
6160
// 2GB).
62-
maxMemTableSize = constants.MaxUint32OrInt
61+
maxMemTableSize = min(math.MaxUint32, math.MaxInt)
6362
)
6463

6564
// FileCacheSize can be used to determine the file

0 commit comments

Comments
 (0)