Skip to content

Commit 01cd0e0

Browse files
committed
memfs: Remove memfs.WithoutMutex
Ensure memfs is thread-safe at all times. In case the single thread performance is more important (probably outside of go-git) that can be fixed with future optimisation. In the context of go-git, this should not be a concern as there are other bottlenecks (e.g. sha1cd). Signed-off-by: Paulo Gomes <[email protected]>
1 parent a989507 commit 01cd0e0

File tree

5 files changed

+9
-62
lines changed

5 files changed

+9
-62
lines changed

memfs/memory.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"sort"
1212
"strings"
13+
"sync"
1314
"syscall"
1415

1516
"github.com/go-git/go-billy/v6"
@@ -23,23 +24,19 @@ const separator = filepath.Separator
2324
type Memory struct {
2425
s *storage
2526

26-
m mutex
27+
m sync.RWMutex
2728
}
2829

2930
// New returns a new Memory filesystem.
3031
func New(opts ...Option) billy.Filesystem {
31-
o := &options{
32-
// Enable thread-safety by default.
33-
newMutex: newMutex,
34-
}
35-
32+
o := &options{}
3633
for _, opt := range opts {
3734
opt(o)
3835
}
3936

4037
fs := &Memory{
41-
s: newStorage(o.newMutex),
42-
m: o.newMutex(),
38+
s: newStorage(),
39+
m: sync.RWMutex{},
4340
}
4441
_, err := fs.s.New("/", 0755|os.ModeDir, 0)
4542
if err != nil {

memfs/memory_option.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,4 @@ package memfs
33
type Option func(*options)
44

55
type options struct {
6-
newMutex func() mutex
7-
}
8-
9-
// WithoutMutex disables thread safety. This is a temporary option
10-
// for users to opt-out from the default setting.
11-
func WithoutMutex() Option {
12-
return func(o *options) {
13-
o.newMutex = newNoOpMutex
14-
}
156
}

memfs/mutex.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

memfs/storage.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"github.com/go-git/go-billy/v6"
@@ -15,16 +16,14 @@ type storage struct {
1516
files map[string]*file
1617
children map[string]map[string]*file
1718

18-
mf mutex
19-
mc mutex
19+
mf sync.RWMutex
20+
mc sync.RWMutex
2021
}
2122

22-
func newStorage(newMutex func() mutex) *storage {
23+
func newStorage() *storage {
2324
return &storage{
2425
files: make(map[string]*file, 0),
2526
children: make(map[string]map[string]*file, 0),
26-
mc: newMutex(),
27-
mf: newMutex(),
2827
}
2928
}
3029

test/bench_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ func BenchmarkCompare(b *testing.B) {
5555
{
5656
name: "memfs",
5757
fn: fn,
58-
sut: memfs.New(memfs.WithoutMutex()),
59-
openF: billyOpen,
60-
createF: billyCreate,
61-
},
62-
{
63-
name: "memfs_mutex",
64-
fn: fn,
6558
sut: memfs.New(),
6659
openF: billyOpen,
6760
createF: billyCreate,

0 commit comments

Comments
 (0)