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

View file

@ -509,29 +509,26 @@ func (node Node) sameExtendedAttributes(other Node) bool {
func (node *Node) fillUser(stat statT) {
uid, gid := stat.uid(), stat.gid()
node.UID, node.GID = uid, gid
node.User = lookupUsername(strconv.Itoa(int(uid)))
node.Group = lookupGroup(strconv.Itoa(int(gid)))
node.User = lookupUsername(uid)
node.Group = lookupGroup(gid)
}
var (
uidLookupCache = make(map[string]string)
uidLookupCache = make(map[uint32]string)
uidLookupCacheMutex = sync.RWMutex{}
)
// 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()
value, ok := uidLookupCache[uid]
username, ok := uidLookupCache[uid]
uidLookupCacheMutex.RUnlock()
if ok {
return value
return username
}
username := ""
u, err := user.LookupId(uid)
u, err := user.LookupId(strconv.Itoa(int(uid)))
if err == nil {
username = u.Username
}
@ -544,23 +541,21 @@ func lookupUsername(uid string) string {
}
var (
gidLookupCache = make(map[string]string)
gidLookupCache = make(map[uint32]string)
gidLookupCacheMutex = sync.RWMutex{}
)
// 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()
value, ok := gidLookupCache[gid]
group, ok := gidLookupCache[gid]
gidLookupCacheMutex.RUnlock()
if ok {
return value
return group
}
group := ""
g, err := user.LookupGroupId(gid)
g, err := user.LookupGroupId(strconv.Itoa(int(gid)))
if err == nil {
group = g.Name
}