Skip to content

fix: Concurrent map read/write in Tree methods #1238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: previous-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions plumbing/object/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Tree struct {

s storer.EncodedObjectStorer
m map[string]*TreeEntry
t map[string]*Tree // tree path cache
}

// GetTree gets a tree from an object storer and decodes it.
Expand Down Expand Up @@ -128,28 +127,10 @@ func (t *Tree) TreeEntryFile(e *TreeEntry) (*File, error) {

// FindEntry search a TreeEntry in this tree or any subtree.
func (t *Tree) FindEntry(path string) (*TreeEntry, error) {
if t.t == nil {
t.t = make(map[string]*Tree)
}

pathParts := strings.Split(path, "/")
startingTree := t
pathCurrent := ""

// search for the longest path in the tree path cache
for i := len(pathParts) - 1; i > 1; i-- {
path := filepath.Join(pathParts[:i]...)

tree, ok := t.t[path]
if ok {
startingTree = tree
pathParts = pathParts[i:]
pathCurrent = path

break
}
}

var tree *Tree
var err error
for tree = startingTree; len(pathParts) > 1; pathParts = pathParts[1:] {
Expand All @@ -158,7 +139,6 @@ func (t *Tree) FindEntry(path string) (*TreeEntry, error) {
}

pathCurrent = filepath.Join(pathCurrent, pathParts[0])
t.t[pathCurrent] = tree
}

return tree.entry(pathParts[0])
Expand All @@ -182,10 +162,6 @@ func (t *Tree) dir(baseName string) (*Tree, error) {
}

func (t *Tree) entry(baseName string) (*TreeEntry, error) {
if t.m == nil {
t.buildMap()
}

entry, ok := t.m[baseName]
if !ok {
return nil, ErrEntryNotFound
Expand Down Expand Up @@ -269,6 +245,8 @@ func (t *Tree) Decode(o plumbing.EncodedObject) (err error) {
})
}

t.buildMap()

return nil
}

Expand Down
1 change: 1 addition & 0 deletions plumbing/object/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func (s *TreeSuite) TestTreeDecodeEncodeIdempotent() {
err = newTree.Decode(obj)
s.NoError(err)
tree.Hash = obj.Hash()
tree.buildMap()
s.Equal(tree, newTree)
}
}
Expand Down
115 changes: 115 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import (
"context"
"errors"
"fmt"
"github.com/go-git/go-git/v5/utils/merkletrie/noder"
"io"
"math"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -3513,3 +3517,114 @@ func BenchmarkPlainClone(b *testing.B) {
clone(b)
}
}

func benchmarkPrepareRepo(b *testing.B, dir string) {
b.Helper()
_, err := PlainClone(dir, true, &CloneOptions{
URL: "https://github.com/go-git/go-git.git",
Depth: 1,
Tags: plumbing.NoTags,
SingleBranch: true,
})
if err != nil {
b.Fatal(err)
}
}

func benchmarkPrepareTree(b *testing.B, dir string) *object.Tree {
repo, err := PlainOpen(dir)
if err != nil {
b.Fatal(err)
}
rev, err := repo.ResolveRevision(plumbing.Revision(plumbing.HEAD))
if err != nil {
b.Fatal(err)
}
commit, err := repo.CommitObject(*rev)
if err != nil {
b.Fatal(err)
}
tree, err := commit.Tree()
if err != nil {
b.Fatal(err)
}
return tree
}

func listFilesFromNoder(node noder.Noder, path []string) ([]string, error) {
if !node.IsDir() {
return []string{strings.Join(append(path[1:], node.Name()), "/")}, nil
}

thisName := node.Name()
children, err := node.Children()
if err != nil {
return nil, err
}
var files []string
for _, child := range children {
childFiles, err := listFilesFromNoder(child, append(path, thisName))
if err != nil {
return nil, err
}
files = append(files, childFiles...)
}
return files, nil
}

func BenchmarkTree_FindEntry(b *testing.B) {
b.StopTimer()
tempDir := b.TempDir()
benchmarkPrepareRepo(b, tempDir)
tree := benchmarkPrepareTree(b, tempDir)
ndr := object.NewTreeRootNode(tree)
filenames, err := listFilesFromNoder(ndr, nil)
if err != nil {
b.Fatal(err)
}

b.StartTimer()
for i := 0; i < b.N; i++ {
tree = benchmarkPrepareTree(b, tempDir) // Prevent the tree cache from warming up
for _, filename := range filenames {
_, err = tree.FindEntry(filename)
if err != nil {
b.Fatal(err)
}
}
}
}

func BenchmarkTree_FindEntry_Parallel(b *testing.B) {
b.StopTimer()
tempDir := b.TempDir()
benchmarkPrepareRepo(b, tempDir)
tree := benchmarkPrepareTree(b, tempDir)
ndr := object.NewTreeRootNode(tree)
filenames, err := listFilesFromNoder(ndr, nil)
if err != nil {
b.Fatal(err)
}

b.StartTimer()
for i := 0; i < b.N; i++ {
tree = benchmarkPrepareTree(b, tempDir) // Prevent the tree cache from warming up
var wg sync.WaitGroup
perCore := int(math.Ceil(float64(len(filenames)) / float64(runtime.NumCPU())))
for cpu := 0; cpu < runtime.NumCPU(); cpu++ {
wg.Add(1)
go func(cpu int) {
startIdx := perCore * cpu
endIdx := min(perCore*(cpu+1), len(filenames))
for fileIdx := startIdx; fileIdx < endIdx; fileIdx++ {
_, err = tree.FindEntry(filenames[fileIdx])
if err != nil {
b.Errorf("error reading %s: %s", filenames[fileIdx], err)
}
}
wg.Done()
}(cpu)
}
wg.Wait()
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy