Skip to content

Commit

Permalink
memfs: Remove memfs.WithoutMutex
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pjbgf committed Jan 28, 2025
1 parent a989507 commit 7c5ae60
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 63 deletions.
11 changes: 2 additions & 9 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@ const separator = filepath.Separator
// Memory a very convenient filesystem based on memory files.
type Memory struct {
s *storage

m mutex
}

// 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 {
Expand Down
9 changes: 0 additions & 9 deletions memfs/memory_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
33 changes: 0 additions & 33 deletions memfs/mutex.go

This file was deleted.

9 changes: 4 additions & 5 deletions memfs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/go-git/go-billy/v6"
Expand All @@ -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(),
}
}

Expand Down
7 changes: 0 additions & 7 deletions test/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7c5ae60

Please sign in to comment.