From 5ae89b310932808e6d1f1d33ea6b70be5922d2e9 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Tue, 28 Jan 2025 21:29:19 +0000 Subject: [PATCH] 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 --- memfs/memory.go | 12 ++++-------- memfs/memory_option.go | 9 --------- memfs/mutex.go | 33 --------------------------------- memfs/storage.go | 9 ++++----- test/bench_test.go | 7 ------- 5 files changed, 8 insertions(+), 62 deletions(-) delete mode 100644 memfs/mutex.go diff --git a/memfs/memory.go b/memfs/memory.go index c69eeed..0147ee1 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -10,6 +10,7 @@ import ( "path/filepath" "sort" "strings" + "sync" "syscall" "github.com/go-git/go-billy/v6" @@ -23,23 +24,18 @@ const separator = filepath.Separator type Memory struct { s *storage - m mutex + m sync.RWMutex } // New returns a new Memory filesystem. func New(opts ...Option) billy.Filesystem { - o := &options{ - // Enable thread-safety by default. - newMutex: newMutex, - } - + o := &options{} for _, opt := range opts { opt(o) } fs := &Memory{ - s: newStorage(o.newMutex), - m: o.newMutex(), + s: newStorage(), } _, err := fs.s.New("/", 0755|os.ModeDir, 0) if err != nil { diff --git a/memfs/memory_option.go b/memfs/memory_option.go index 3d7170a..bbdd22d 100644 --- a/memfs/memory_option.go +++ b/memfs/memory_option.go @@ -3,13 +3,4 @@ package memfs type Option func(*options) type options struct { - newMutex func() mutex -} - -// WithoutMutex disables thread safety. This is a temporary option -// for users to opt-out from the default setting. -func WithoutMutex() Option { - return func(o *options) { - o.newMutex = newNoOpMutex - } } diff --git a/memfs/mutex.go b/memfs/mutex.go deleted file mode 100644 index 4b84da2..0000000 --- a/memfs/mutex.go +++ /dev/null @@ -1,33 +0,0 @@ -package memfs - -import "sync" - -func newMutex() mutex { - return &sync.RWMutex{} -} - -func newNoOpMutex() mutex { - return &sync.RWMutex{} -} - -type mutex interface { - Lock() - Unlock() - RLock() - RUnlock() -} - -type noOpMutex struct { //nolint:unused -} - -func (noOpMutex) Lock() { //nolint:unused -} - -func (noOpMutex) Unlock() { //nolint:unused -} - -func (noOpMutex) RLock() { //nolint:unused -} - -func (noOpMutex) RUnlock() { //nolint:unused -} diff --git a/memfs/storage.go b/memfs/storage.go index 868d525..f939e56 100644 --- a/memfs/storage.go +++ b/memfs/storage.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "strings" + "sync" "time" "github.com/go-git/go-billy/v6" @@ -15,16 +16,14 @@ type storage struct { files map[string]*file children map[string]map[string]*file - mf mutex - mc mutex + mf sync.RWMutex + mc sync.RWMutex } -func newStorage(newMutex func() mutex) *storage { +func newStorage() *storage { return &storage{ files: make(map[string]*file, 0), children: make(map[string]map[string]*file, 0), - mc: newMutex(), - mf: newMutex(), } } diff --git a/test/bench_test.go b/test/bench_test.go index 53c561f..6e33511 100644 --- a/test/bench_test.go +++ b/test/bench_test.go @@ -55,13 +55,6 @@ func BenchmarkCompare(b *testing.B) { { name: "memfs", fn: fn, - sut: memfs.New(memfs.WithoutMutex()), - openF: billyOpen, - createF: billyCreate, - }, - { - name: "memfs_mutex", - fn: fn, sut: memfs.New(), openF: billyOpen, createF: billyCreate,