diff options
author | 2023-08-30 22:20:54 +0000 | |
---|---|---|
committer | 2023-08-31 00:30:24 +0000 | |
commit | 43dfeb6d10040e968c036485f87203212f868731 (patch) | |
tree | 5098d48a7f0e62f9afd58bc78e97438a73545ed9 | |
parent | 37aa33fcb1d1e7de18534ecebf7cd63825b8c201 (diff) |
Replace sync.Mutex with Sync.Map
createProtoLibraryTargetsForIncludeDirs uses a lock-protected plain map
to see if a directory has been handled previously during concurrent visits
of modules in bp2build.
From golang's docs https://pkg.go.dev/sync#Map, replacing sync.Mutex
with sync.Map will be faster in this use case since the keys (directores
here) are written just once but read many times.
Test: m bp2build
Change-Id: Ia2a471b4db5d2890fa6048bc05a17cebc5f686af
-rw-r--r-- | android/proto.go | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/android/proto.go b/android/proto.go index 3c4b4c7cd..7c418c693 100644 --- a/android/proto.go +++ b/android/proto.go @@ -350,13 +350,12 @@ type PkgPathInterface interface { var ( protoIncludeDirGeneratedSuffix = ".include_dir_bp2build_generated_proto" protoIncludeDirsBp2buildKey = NewOnceKey("protoIncludeDirsBp2build") - protoIncludeDirsBp2buildLock sync.Mutex ) -func getProtoIncludeDirsBp2build(config Config) *map[protoIncludeDirKey]bool { +func getProtoIncludeDirsBp2build(config Config) *sync.Map { return config.Once(protoIncludeDirsBp2buildKey, func() interface{} { - return &map[protoIncludeDirKey]bool{} - }).(*map[protoIncludeDirKey]bool) + return &sync.Map{} + }).(*sync.Map) } // key for dynamically creating proto_library per proto.include_dirs @@ -370,9 +369,6 @@ type protoIncludeDirKey struct { // might create the targets in a subdirectory of `includeDir` // Returns the labels of the proto_library targets func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, includeDirs []string) bazel.LabelList { - protoIncludeDirsBp2buildLock.Lock() - defer protoIncludeDirsBp2buildLock.Unlock() - var ret bazel.LabelList for _, dir := range includeDirs { if exists, _, _ := ctx.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists { @@ -389,11 +385,10 @@ func createProtoLibraryTargetsForIncludeDirs(ctx Bp2buildMutatorContext, include Label: "//" + pkg + ":" + label, }) key := protoIncludeDirKey{dir: dir, subpackgeInDir: pkg} - if _, exists := (*dirMap)[key]; exists { + if _, exists := dirMap.LoadOrStore(key, true); exists { // A proto_library has already been created for this package relative to this include dir continue } - (*dirMap)[key] = true srcs := protoLabelelsPartitionedByPkg[pkg] rel, err := filepath.Rel(dir, pkg) if err != nil { |