Skip to content

Commit 07fec78

Browse files
authored
Grow memory geometrically. (#316)
1 parent da4638c commit 07fec78

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

internal/alloc/alloc_unix.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ func (m *mmappedMemory) Reallocate(size uint64) []byte {
4040
com := uint64(len(m.buf))
4141
res := uint64(cap(m.buf))
4242
if com < size && size <= res {
43-
// Round up to the page size.
43+
// Grow geometrically, round up to the page size.
4444
rnd := uint64(unix.Getpagesize() - 1)
45-
new := (size + rnd) &^ rnd
45+
new := com + com>>3
46+
new = min(max(size, new), res)
47+
new = (new + rnd) &^ rnd
4648

4749
// Commit additional memory up to new bytes.
4850
err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE)

internal/alloc/alloc_windows.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ func (m *virtualMemory) Reallocate(size uint64) []byte {
4747
com := uint64(len(m.buf))
4848
res := uint64(cap(m.buf))
4949
if com < size && size <= res {
50-
// Round up to the page size.
50+
// Grow geometrically, round up to the page size.
5151
rnd := uint64(windows.Getpagesize() - 1)
52-
new := (size + rnd) &^ rnd
52+
new := com + com>>3
53+
new = min(max(size, new), res)
54+
new = (new + rnd) &^ rnd
5355

5456
// Commit additional memory up to new bytes.
5557
_, err := windows.VirtualAlloc(m.addr, uintptr(new), windows.MEM_COMMIT, windows.PAGE_READWRITE)

0 commit comments

Comments
 (0)