Skip to content

Commit 019246d

Browse files
committed
Simplify mmap.
1 parent fa259bd commit 019246d

File tree

9 files changed

+29
-45
lines changed

9 files changed

+29
-45
lines changed

internal/util/mmap.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,13 @@ import (
1212
"golang.org/x/sys/unix"
1313
)
1414

15-
type mmapState struct {
16-
regions []*MappedRegion
17-
enabled bool
18-
}
19-
20-
func (s *mmapState) init(ctx context.Context, enabled bool) context.Context {
21-
if s.enabled = enabled; enabled {
22-
return experimental.WithMemoryAllocator(ctx,
23-
experimental.MemoryAllocatorFunc(mmappedAllocator))
24-
}
25-
return ctx
15+
func withMmappedAllocator(ctx context.Context) context.Context {
16+
return experimental.WithMemoryAllocator(ctx,
17+
experimental.MemoryAllocatorFunc(mmappedAllocator))
2618
}
2719

28-
func CanMapFiles(ctx context.Context) bool {
29-
s := ctx.Value(moduleKey{}).(*moduleState)
30-
return s.mmapState.enabled
20+
type mmapState struct {
21+
regions []*MappedRegion
3122
}
3223

3324
func (s *mmapState) new(ctx context.Context, mod api.Module, size int32) *MappedRegion {

internal/util/mmap_other.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ import "context"
66

77
type mmapState struct{}
88

9-
func (s *mmapState) init(ctx context.Context, _ bool) context.Context {
9+
func withMmappedAllocator(ctx context.Context) context.Context {
1010
return ctx
1111
}
12-
13-
func CanMapFiles(ctx context.Context) bool {
14-
return false
15-
}

internal/util/module.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ type moduleState struct {
1212
mmapState
1313
}
1414

15-
func NewContext(ctx context.Context, mappableMemory bool) context.Context {
15+
func NewContext(ctx context.Context) context.Context {
1616
state := new(moduleState)
17-
ctx = context.WithValue(ctx, moduleKey{}, state)
17+
ctx = withMmappedAllocator(ctx)
1818
ctx = experimental.WithCloseNotifier(ctx, state)
19-
ctx = state.mmapState.init(ctx, mappableMemory)
19+
ctx = context.WithValue(ctx, moduleKey{}, state)
2020
return ctx
2121
}

sqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func instantiateSQLite() (sqlt *sqlite, err error) {
8585
}
8686

8787
sqlt = new(sqlite)
88-
sqlt.ctx = util.NewContext(context.Background(), vfs.SupportsSharedMemory)
88+
sqlt.ctx = util.NewContext(context.Background())
8989

9090
sqlt.mod, err = instance.runtime.InstantiateModule(sqlt.ctx,
9191
instance.compiled, wazero.NewModuleConfig().WithName(""))

vfs/lock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Test_vfsLock(t *testing.T) {
3535
pOutput = 32
3636
)
3737
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
38-
ctx := util.NewContext(context.TODO(), false)
38+
ctx := util.NewContext(context.TODO())
3939

4040
vfsFileRegister(ctx, mod, pFile1, &vfsFile{File: file1})
4141
vfsFileRegister(ctx, mod, pFile2, &vfsFile{File: file2})

vfs/tests/mptest/mptest_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ func system(ctx context.Context, mod api.Module, ptr uint32) uint32 {
9696

9797
cfg := config(ctx).WithArgs(args...)
9898
go func() {
99-
ctx := util.NewContext(ctx, true)
99+
ctx := util.NewContext(ctx)
100100
mod, _ := rt.InstantiateModule(ctx, module, cfg)
101101
mod.Close(ctx)
102102
}()
103103
return 0
104104
}
105105

106106
func Test_config01(t *testing.T) {
107-
ctx := util.NewContext(newContext(t), false)
107+
ctx := util.NewContext(newContext(t))
108108
name := filepath.Join(t.TempDir(), "test.db")
109109
cfg := config(ctx).WithArgs("mptest", name, "config01.test")
110110
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -122,7 +122,7 @@ func Test_config02(t *testing.T) {
122122
t.Skip("skipping in CI")
123123
}
124124

125-
ctx := util.NewContext(newContext(t), false)
125+
ctx := util.NewContext(newContext(t))
126126
name := filepath.Join(t.TempDir(), "test.db")
127127
cfg := config(ctx).WithArgs("mptest", name, "config02.test")
128128
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -140,7 +140,7 @@ func Test_crash01(t *testing.T) {
140140
t.Skip("skipping in CI")
141141
}
142142

143-
ctx := util.NewContext(newContext(t), false)
143+
ctx := util.NewContext(newContext(t))
144144
name := filepath.Join(t.TempDir(), "test.db")
145145
cfg := config(ctx).WithArgs("mptest", name, "crash01.test")
146146
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -155,7 +155,7 @@ func Test_multiwrite01(t *testing.T) {
155155
t.Skip("skipping in short mode")
156156
}
157157

158-
ctx := util.NewContext(newContext(t), false)
158+
ctx := util.NewContext(newContext(t))
159159
name := filepath.Join(t.TempDir(), "test.db")
160160
cfg := config(ctx).WithArgs("mptest", name, "multiwrite01.test")
161161
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -167,7 +167,7 @@ func Test_multiwrite01(t *testing.T) {
167167

168168
func Test_config01_memory(t *testing.T) {
169169
memdb.Delete("test.db")
170-
ctx := util.NewContext(newContext(t), false)
170+
ctx := util.NewContext(newContext(t))
171171
cfg := config(ctx).WithArgs("mptest", "/test.db", "config01.test",
172172
"--vfs", "memdb")
173173
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -183,7 +183,7 @@ func Test_multiwrite01_memory(t *testing.T) {
183183
}
184184

185185
memdb.Delete("test.db")
186-
ctx := util.NewContext(newContext(t), false)
186+
ctx := util.NewContext(newContext(t))
187187
cfg := config(ctx).WithArgs("mptest", "/test.db", "multiwrite01.test",
188188
"--vfs", "memdb")
189189
mod, err := rt.InstantiateModule(ctx, module, cfg)
@@ -204,7 +204,7 @@ func Test_crash01_wal(t *testing.T) {
204204
t.Skip("skipping without shared memory")
205205
}
206206

207-
ctx := util.NewContext(newContext(t), true)
207+
ctx := util.NewContext(newContext(t))
208208
name := filepath.Join(t.TempDir(), "test.db")
209209
cfg := config(ctx).WithArgs("mptest", name, "crash01.test",
210210
"--journalmode", "wal")
@@ -223,7 +223,7 @@ func Test_multiwrite01_wal(t *testing.T) {
223223
t.Skip("skipping without shared memory")
224224
}
225225

226-
ctx := util.NewContext(newContext(t), true)
226+
ctx := util.NewContext(newContext(t))
227227
name := filepath.Join(t.TempDir(), "test.db")
228228
cfg := config(ctx).WithArgs("mptest", name, "multiwrite01.test",
229229
"--journalmode", "wal")
@@ -242,7 +242,7 @@ func Test_crash01_adiantum(t *testing.T) {
242242
t.Skip("skipping in CI")
243243
}
244244

245-
ctx := util.NewContext(newContext(t), true)
245+
ctx := util.NewContext(newContext(t))
246246
name := "file:" + filepath.Join(t.TempDir(), "test.db") +
247247
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
248248
cfg := config(ctx).WithArgs("mptest", name, "crash01.test",
@@ -265,7 +265,7 @@ func Test_crash01_adiantum_wal(t *testing.T) {
265265
t.Skip("skipping without shared memory")
266266
}
267267

268-
ctx := util.NewContext(newContext(t), true)
268+
ctx := util.NewContext(newContext(t))
269269
name := "file:" + filepath.Join(t.TempDir(), "test.db") +
270270
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
271271
cfg := config(ctx).WithArgs("mptest", name, "crash01.test",

vfs/tests/speedtest1/speedtest1_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func initFlags() {
8585

8686
func Benchmark_speedtest1(b *testing.B) {
8787
output.Reset()
88-
ctx := util.NewContext(context.Background(), true)
88+
ctx := util.NewContext(context.Background())
8989
name := filepath.Join(b.TempDir(), "test.db")
9090
args := append(options, "--size", strconv.Itoa(b.N), name)
9191
cfg := wazero.NewModuleConfig().
@@ -103,7 +103,7 @@ func Benchmark_speedtest1(b *testing.B) {
103103

104104
func Benchmark_adiantum(b *testing.B) {
105105
output.Reset()
106-
ctx := util.NewContext(context.Background(), true)
106+
ctx := util.NewContext(context.Background())
107107
name := "file:" + filepath.Join(b.TempDir(), "test.db") +
108108
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
109109
args := append(options, "--vfs", "adiantum", "--size", strconv.Itoa(b.N), name)

vfs/vfs.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,9 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile uint32, fla
161161
if pOutFlags != 0 {
162162
util.WriteUint32(mod, pOutFlags, uint32(flags))
163163
}
164-
if pOutVFS != 0 && util.CanMapFiles(ctx) {
165-
if f, ok := file.(FileSharedMemory); ok {
166-
if f.SharedMemory() != nil {
167-
util.WriteUint32(mod, pOutVFS, 1)
168-
}
169-
}
164+
if f, ok := file.(FileSharedMemory); ok && flags&OPEN_MAIN_DB != 0 &&
165+
pOutVFS != 0 && f.SharedMemory() != nil {
166+
util.WriteUint32(mod, pOutVFS, 1)
170167
}
171168
vfsFileRegister(ctx, mod, pFile, file)
172169
return _OK

vfs/vfs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func Test_vfsAccess(t *testing.T) {
209209

210210
func Test_vfsFile(t *testing.T) {
211211
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
212-
ctx := util.NewContext(context.TODO(), false)
212+
ctx := util.NewContext(context.TODO())
213213

214214
// Open a temporary file.
215215
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) {
281281

282282
func Test_vfsFile_psow(t *testing.T) {
283283
mod := wazerotest.NewModule(wazerotest.NewMemory(wazerotest.PageSize))
284-
ctx := util.NewContext(context.TODO(), false)
284+
ctx := util.NewContext(context.TODO())
285285

286286
// Open a temporary file.
287287
rc := vfsOpen(ctx, mod, 0, 0, 4, OPEN_CREATE|OPEN_EXCLUSIVE|OPEN_READWRITE|OPEN_DELETEONCLOSE, 0, 0)

0 commit comments

Comments
 (0)