From 7bab8bb949140814dcc37d1d50836bd88dedea64 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 15 Apr 2024 23:01:20 +0100 Subject: [PATCH] wazero 1.7.1. --- go.mod | 2 +- go.sum | 4 ++-- internal/util/alloc.go | 28 +++++++++------------------- internal/util/mmap.go | 3 ++- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index e05ce31..2ac4362 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21 require ( github.com/ncruces/julianday v1.0.0 github.com/psanford/httpreadat v0.1.0 - github.com/tetratelabs/wazero v1.7.1-0.20240410111357-a0fbb185447f + github.com/tetratelabs/wazero v1.7.1 golang.org/x/crypto v0.22.0 golang.org/x/sync v0.7.0 golang.org/x/sys v0.19.0 diff --git a/go.sum b/go.sum index 596c223..942146f 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g= github.com/psanford/httpreadat v0.1.0 h1:VleW1HS2zO7/4c7c7zNl33fO6oYACSagjJIyMIwZLUE= github.com/psanford/httpreadat v0.1.0/go.mod h1:Zg7P+TlBm3bYbyHTKv/EdtSJZn3qwbPwpfZ/I9GKCRE= -github.com/tetratelabs/wazero v1.7.1-0.20240410111357-a0fbb185447f h1:xJ6F/f7fM1OvnPFSn7Ggf9icswSXoYOYLZbu7aJVQbA= -github.com/tetratelabs/wazero v1.7.1-0.20240410111357-a0fbb185447f/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y= +github.com/tetratelabs/wazero v1.7.1 h1:QtSfd6KLc41DIMpDYlJdoMc6k7QTN246DM2+n2Y/Dx8= +github.com/tetratelabs/wazero v1.7.1/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y= golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= diff --git a/internal/util/alloc.go b/internal/util/alloc.go index 8e3e83f..6bea744 100644 --- a/internal/util/alloc.go +++ b/internal/util/alloc.go @@ -9,7 +9,7 @@ import ( "golang.org/x/sys/unix" ) -func mmappedAllocator(min, cap, max uint64) experimental.MemoryBuffer { +func mmappedAllocator(cap, max uint64) experimental.LinearMemory { // Round up to the page size. rnd := uint64(unix.Getpagesize() - 1) max = (max + rnd) &^ rnd @@ -32,28 +32,17 @@ func mmappedAllocator(min, cap, max uint64) experimental.MemoryBuffer { unix.Munmap(b) panic(err) } - return &mmappedBuffer{ - buf: b[:cap], - cur: min, - } + return &mmappedMemory{buf: b[:cap]} } // The slice covers the entire mmapped memory: // - len(buf) is the already committed memory, -// - cap(buf) is the reserved address space, -// - cur is the already requested size. -type mmappedBuffer struct { +// - cap(buf) is the reserved address space. +type mmappedMemory struct { buf []byte - cur uint64 } -func (m *mmappedBuffer) Buffer() []byte { - // Limit capacity because bytes beyond len(m.buf) - // have not yet been committed. - return m.buf[:m.cur:len(m.buf)] -} - -func (m *mmappedBuffer) Grow(size uint64) []byte { +func (m *mmappedMemory) Reallocate(size uint64) []byte { if com := uint64(len(m.buf)); com < size { // Round up to the page size. rnd := uint64(unix.Getpagesize() - 1) @@ -68,11 +57,12 @@ func (m *mmappedBuffer) Grow(size uint64) []byte { // Update commited memory. m.buf = m.buf[:new] } - m.cur = size - return m.Buffer() + // Limit returned capacity because bytes beyond + // len(m.buf) have not yet been committed. + return m.buf[:size:len(m.buf)] } -func (m *mmappedBuffer) Free() { +func (m *mmappedMemory) Free() { err := unix.Munmap(m.buf[:cap(m.buf)]) if err != nil { panic(err) diff --git a/internal/util/mmap.go b/internal/util/mmap.go index eda2790..94476a1 100644 --- a/internal/util/mmap.go +++ b/internal/util/mmap.go @@ -19,7 +19,8 @@ type mmapState struct { func (s *mmapState) init(ctx context.Context, enabled bool) context.Context { if s.enabled = enabled; enabled { - return experimental.WithMemoryAllocator(ctx, mmappedAllocator) + return experimental.WithMemoryAllocator(ctx, + experimental.MemoryAllocatorFunc(mmappedAllocator)) } return ctx }