mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-11 21:49:13 +00:00
Simplify mmap.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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(""))
|
||||
|
||||
@@ -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})
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user