From 019246d1bec261a7e8695b18d12cd79db898b716 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 26 Apr 2024 16:25:45 +0100 Subject: [PATCH] Simplify mmap. --- internal/util/mmap.go | 19 +++++-------------- internal/util/mmap_other.go | 6 +----- internal/util/module.go | 6 +++--- sqlite.go | 2 +- vfs/lock_test.go | 2 +- vfs/tests/mptest/mptest_test.go | 22 +++++++++++----------- vfs/tests/speedtest1/speedtest1_test.go | 4 ++-- vfs/vfs.go | 9 +++------ vfs/vfs_test.go | 4 ++-- 9 files changed, 29 insertions(+), 45 deletions(-) diff --git a/internal/util/mmap.go b/internal/util/mmap.go index d0f14a2..26f30be 100644 --- a/internal/util/mmap.go +++ b/internal/util/mmap.go @@ -12,22 +12,13 @@ import ( "golang.org/x/sys/unix" ) +func withMmappedAllocator(ctx context.Context) context.Context { + return experimental.WithMemoryAllocator(ctx, + experimental.MemoryAllocatorFunc(mmappedAllocator)) +} + type mmapState struct { regions []*MappedRegion - enabled bool -} - -func (s *mmapState) init(ctx context.Context, enabled bool) context.Context { - if s.enabled = enabled; enabled { - return experimental.WithMemoryAllocator(ctx, - experimental.MemoryAllocatorFunc(mmappedAllocator)) - } - return ctx -} - -func CanMapFiles(ctx context.Context) bool { - s := ctx.Value(moduleKey{}).(*moduleState) - return s.mmapState.enabled } func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *MappedRegion { diff --git a/internal/util/mmap_other.go b/internal/util/mmap_other.go index 05c44cf..89631e0 100644 --- a/internal/util/mmap_other.go +++ b/internal/util/mmap_other.go @@ -6,10 +6,6 @@ import "context" type mmapState struct{} -func (s *mmapState) init(ctx context.Context, _ bool) context.Context { +func withMmappedAllocator(ctx context.Context) context.Context { return ctx } - -func CanMapFiles(ctx context.Context) bool { - return false -} diff --git a/internal/util/module.go b/internal/util/module.go index 0db93bb..f5ee4bd 100644 --- a/internal/util/module.go +++ b/internal/util/module.go @@ -12,10 +12,10 @@ type moduleState struct { mmapState } -func NewContext(ctx context.Context, mappableMemory bool) context.Context { +func NewContext(ctx context.Context) context.Context { state := new(moduleState) - ctx = context.WithValue(ctx, moduleKey{}, state) + ctx = withMmappedAllocator(ctx) ctx = experimental.WithCloseNotifier(ctx, state) - ctx = state.mmapState.init(ctx, mappableMemory) + ctx = context.WithValue(ctx, moduleKey{}, state) return ctx } diff --git a/sqlite.go b/sqlite.go index a1fdd2c..a446ec0 100644 --- a/sqlite.go +++ b/sqlite.go @@ -85,7 +85,7 @@ func instantiateSQLite() (sqlt *sqlite, err error) { } sqlt = new(sqlite) - sqlt.ctx = util.NewContext(context.Background(), vfs.SupportsSharedMemory) + sqlt.ctx = util.NewContext(context.Background()) sqlt.mod, err = instance.runtime.InstantiateModule(sqlt.ctx, instance.compiled, wazero.NewModuleConfig().WithName("")) diff --git a/vfs/lock_test.go b/vfs/lock_test.go index 30abdfe..7b482f5 100644 --- a/vfs/lock_test.go +++ b/vfs/lock_test.go @@ -35,7 +35,7 @@ func Test_vfsLock(t *testing.T) { pOutput = 32 ) mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) - ctx := util.NewContext(context.TODO(), false) + ctx := util.NewContext(context.TODO()) vfsFileRegister(ctx, mod, pFile1, &vfsFile{File: file1}) vfsFileRegister(ctx, mod, pFile2, &vfsFile{File: file2}) diff --git a/vfs/tests/mptest/mptest_test.go b/vfs/tests/mptest/mptest_test.go index 33919aa..78e2515 100644 --- a/vfs/tests/mptest/mptest_test.go +++ b/vfs/tests/mptest/mptest_test.go @@ -96,7 +96,7 @@ func system(ctx context.Context, mod api.Module, ptr uint32) uint32 { cfg := config(ctx).WithArgs(args...) go func() { - ctx := util.NewContext(ctx, true) + ctx := util.NewContext(ctx) mod, _ := rt.InstantiateModule(ctx, module, cfg) mod.Close(ctx) }() @@ -104,7 +104,7 @@ func system(ctx context.Context, mod api.Module, ptr uint32) uint32 { } func Test_config01(t *testing.T) { - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "config01.test") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -122,7 +122,7 @@ func Test_config02(t *testing.T) { t.Skip("skipping in CI") } - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "config02.test") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -140,7 +140,7 @@ func Test_crash01(t *testing.T) { t.Skip("skipping in CI") } - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "crash01.test") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -155,7 +155,7 @@ func Test_multiwrite01(t *testing.T) { t.Skip("skipping in short mode") } - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "multiwrite01.test") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -167,7 +167,7 @@ func Test_multiwrite01(t *testing.T) { func Test_config01_memory(t *testing.T) { memdb.Delete("test.db") - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) cfg := config(ctx).WithArgs("mptest", "/test.db", "config01.test", "--vfs", "memdb") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -183,7 +183,7 @@ func Test_multiwrite01_memory(t *testing.T) { } memdb.Delete("test.db") - ctx := util.NewContext(newContext(t), false) + ctx := util.NewContext(newContext(t)) cfg := config(ctx).WithArgs("mptest", "/test.db", "multiwrite01.test", "--vfs", "memdb") mod, err := rt.InstantiateModule(ctx, module, cfg) @@ -204,7 +204,7 @@ func Test_crash01_wal(t *testing.T) { t.Skip("skipping without shared memory") } - ctx := util.NewContext(newContext(t), true) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "crash01.test", "--journalmode", "wal") @@ -223,7 +223,7 @@ func Test_multiwrite01_wal(t *testing.T) { t.Skip("skipping without shared memory") } - ctx := util.NewContext(newContext(t), true) + ctx := util.NewContext(newContext(t)) name := filepath.Join(t.TempDir(), "test.db") cfg := config(ctx).WithArgs("mptest", name, "multiwrite01.test", "--journalmode", "wal") @@ -242,7 +242,7 @@ func Test_crash01_adiantum(t *testing.T) { t.Skip("skipping in CI") } - ctx := util.NewContext(newContext(t), true) + ctx := util.NewContext(newContext(t)) name := "file:" + filepath.Join(t.TempDir(), "test.db") + "?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" cfg := config(ctx).WithArgs("mptest", name, "crash01.test", @@ -265,7 +265,7 @@ func Test_crash01_adiantum_wal(t *testing.T) { t.Skip("skipping without shared memory") } - ctx := util.NewContext(newContext(t), true) + ctx := util.NewContext(newContext(t)) name := "file:" + filepath.Join(t.TempDir(), "test.db") + "?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" cfg := config(ctx).WithArgs("mptest", name, "crash01.test", diff --git a/vfs/tests/speedtest1/speedtest1_test.go b/vfs/tests/speedtest1/speedtest1_test.go index 72d718f..f181a77 100644 --- a/vfs/tests/speedtest1/speedtest1_test.go +++ b/vfs/tests/speedtest1/speedtest1_test.go @@ -85,7 +85,7 @@ func initFlags() { func Benchmark_speedtest1(b *testing.B) { output.Reset() - ctx := util.NewContext(context.Background(), true) + ctx := util.NewContext(context.Background()) name := filepath.Join(b.TempDir(), "test.db") args := append(options, "--size", strconv.Itoa(b.N), name) cfg := wazero.NewModuleConfig(). @@ -103,7 +103,7 @@ func Benchmark_speedtest1(b *testing.B) { func Benchmark_adiantum(b *testing.B) { output.Reset() - ctx := util.NewContext(context.Background(), true) + ctx := util.NewContext(context.Background()) name := "file:" + filepath.Join(b.TempDir(), "test.db") + "?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" args := append(options, "--vfs", "adiantum", "--size", strconv.Itoa(b.N), name) diff --git a/vfs/vfs.go b/vfs/vfs.go index c37ec22..503a35c 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -161,12 +161,9 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile uint32, fla if pOutFlags != 0 { util.WriteUint32(mod, pOutFlags, uint32(flags)) } - if pOutVFS != 0 && util.CanMapFiles(ctx) { - if f, ok := file.(FileSharedMemory); ok { - if f.SharedMemory() != nil { - util.WriteUint32(mod, pOutVFS, 1) - } - } + if f, ok := file.(FileSharedMemory); ok && flags&OPEN_MAIN_DB != 0 && + pOutVFS != 0 && f.SharedMemory() != nil { + util.WriteUint32(mod, pOutVFS, 1) } vfsFileRegister(ctx, mod, pFile, file) return _OK diff --git a/vfs/vfs_test.go b/vfs/vfs_test.go index 49511ab..701dc6c 100644 --- a/vfs/vfs_test.go +++ b/vfs/vfs_test.go @@ -209,7 +209,7 @@ func Test_vfsAccess(t *testing.T) { func Test_vfsFile(t *testing.T) { mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) - ctx := util.NewContext(context.TODO(), false) + ctx := util.NewContext(context.TODO()) // Open a temporary file. rc := vfsOpen(ctx, mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0, 0) @@ -281,7 +281,7 @@ func Test_vfsFile(t *testing.T) { func Test_vfsFile_psow(t *testing.T) { mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize)) - ctx := util.NewContext(context.TODO(), false) + ctx := util.NewContext(context.TODO()) // Open a temporary file. rc := vfsOpen(ctx, mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0, 0)