Cache uint32-typed ids in lookup{Username,Group}

NodeFillUser-8        1.92µs ± 5%    1.75µs ± 3%   -8.89%  (p=0.000 n=10+10)
NodeFromFileInfo-8    1.89µs ± 7%    1.76µs ± 4%   -6.69%  (p=0.001 n=10+10)

name                old alloc/op   new alloc/op   delta
NodeFillUser-8          504B ± 0%      496B ± 0%   -1.59%  (p=0.000 n=10+10)
NodeFromFileInfo-8      504B ± 0%      496B ± 0%   -1.59%  (p=0.000 n=10+10)

name                old allocs/op  new allocs/op  delta
NodeFillUser-8          5.00 ± 0%      3.00 ± 0%  -40.00%  (p=0.000 n=10+10)
NodeFromFileInfo-8      5.00 ± 0%      3.00 ± 0%  -40.00%  (p=0.000 n=10+10)
This commit is contained in:
greatroar 2020-10-12 14:46:13 +02:00
parent a03fe4562e
commit f0cd16e5ea
1 changed files with 12 additions and 17 deletions

View File

@ -509,29 +509,26 @@ func (node Node) sameExtendedAttributes(other Node) bool {
func (node *Node) fillUser(stat statT) { func (node *Node) fillUser(stat statT) {
uid, gid := stat.uid(), stat.gid() uid, gid := stat.uid(), stat.gid()
node.UID, node.GID = uid, gid node.UID, node.GID = uid, gid
node.User = lookupUsername(uid)
node.User = lookupUsername(strconv.Itoa(int(uid))) node.Group = lookupGroup(gid)
node.Group = lookupGroup(strconv.Itoa(int(gid)))
} }
var ( var (
uidLookupCache = make(map[string]string) uidLookupCache = make(map[uint32]string)
uidLookupCacheMutex = sync.RWMutex{} uidLookupCacheMutex = sync.RWMutex{}
) )
// Cached user name lookup by uid. Returns "" when no name can be found. // Cached user name lookup by uid. Returns "" when no name can be found.
func lookupUsername(uid string) string { func lookupUsername(uid uint32) string {
uidLookupCacheMutex.RLock() uidLookupCacheMutex.RLock()
value, ok := uidLookupCache[uid] username, ok := uidLookupCache[uid]
uidLookupCacheMutex.RUnlock() uidLookupCacheMutex.RUnlock()
if ok { if ok {
return value return username
} }
username := "" u, err := user.LookupId(strconv.Itoa(int(uid)))
u, err := user.LookupId(uid)
if err == nil { if err == nil {
username = u.Username username = u.Username
} }
@ -544,23 +541,21 @@ func lookupUsername(uid string) string {
} }
var ( var (
gidLookupCache = make(map[string]string) gidLookupCache = make(map[uint32]string)
gidLookupCacheMutex = sync.RWMutex{} gidLookupCacheMutex = sync.RWMutex{}
) )
// Cached group name lookup by gid. Returns "" when no name can be found. // Cached group name lookup by gid. Returns "" when no name can be found.
func lookupGroup(gid string) string { func lookupGroup(gid uint32) string {
gidLookupCacheMutex.RLock() gidLookupCacheMutex.RLock()
value, ok := gidLookupCache[gid] group, ok := gidLookupCache[gid]
gidLookupCacheMutex.RUnlock() gidLookupCacheMutex.RUnlock()
if ok { if ok {
return value return group
} }
group := "" g, err := user.LookupGroupId(strconv.Itoa(int(gid)))
g, err := user.LookupGroupId(gid)
if err == nil { if err == nil {
group = g.Name group = g.Name
} }