From a03fe4562efa84d6a85086a32f7d7d4138c7d299 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Mon, 12 Oct 2020 14:40:54 +0200 Subject: [PATCH 1/2] Remove unused error return in lookup{Username,Group} --- internal/restic/node.go | 45 +++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/internal/restic/node.go b/internal/restic/node.go index 7efb35022..2ea9496ff 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -506,24 +506,12 @@ func (node Node) sameExtendedAttributes(other Node) bool { return true } -func (node *Node) fillUser(stat statT) error { - node.UID = stat.uid() - node.GID = stat.gid() +func (node *Node) fillUser(stat statT) { + uid, gid := stat.uid(), stat.gid() + node.UID, node.GID = uid, gid - username, err := lookupUsername(strconv.Itoa(int(stat.uid()))) - if err != nil { - return err - } - - group, err := lookupGroup(strconv.Itoa(int(stat.gid()))) - if err != nil { - return err - } - - node.User = username - node.Group = group - - return nil + node.User = lookupUsername(strconv.Itoa(int(uid))) + node.Group = lookupGroup(strconv.Itoa(int(gid))) } var ( @@ -531,13 +519,14 @@ var ( uidLookupCacheMutex = sync.RWMutex{} ) -func lookupUsername(uid string) (string, error) { +// Cached user name lookup by uid. Returns "" when no name can be found. +func lookupUsername(uid string) string { uidLookupCacheMutex.RLock() value, ok := uidLookupCache[uid] uidLookupCacheMutex.RUnlock() if ok { - return value, nil + return value } username := "" @@ -551,7 +540,7 @@ func lookupUsername(uid string) (string, error) { uidLookupCache[uid] = username uidLookupCacheMutex.Unlock() - return username, nil + return username } var ( @@ -559,13 +548,14 @@ var ( gidLookupCacheMutex = sync.RWMutex{} ) -func lookupGroup(gid string) (string, error) { +// Cached group name lookup by gid. Returns "" when no name can be found. +func lookupGroup(gid string) string { gidLookupCacheMutex.RLock() value, ok := gidLookupCache[gid] gidLookupCacheMutex.RUnlock() if ok { - return value, nil + return value } group := "" @@ -579,7 +569,7 @@ func lookupGroup(gid string) (string, error) { gidLookupCache[gid] = group gidLookupCacheMutex.Unlock() - return group, nil + return group } func (node *Node) fillExtra(path string, fi os.FileInfo) error { @@ -597,11 +587,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error { node.fillTimes(stat) - var err error - - if err = node.fillUser(stat); err != nil { - return err - } + node.fillUser(stat) switch node.Type { case "file": @@ -609,6 +595,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error { node.Links = uint64(stat.nlink()) case "dir": case "symlink": + var err error node.LinkTarget, err = fs.Readlink(path) node.Links = uint64(stat.nlink()) if err != nil { @@ -626,7 +613,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error { return errors.Errorf("invalid node type %q", node.Type) } - if err = node.fillExtendedAttributes(path); err != nil { + if err := node.fillExtendedAttributes(path); err != nil { return err } From f0cd16e5eaf10abdf16751a1b8bd61268e25ed2b Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Mon, 12 Oct 2020 14:46:13 +0200 Subject: [PATCH 2/2] Cache uint32-typed ids in lookup{Username,Group} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- internal/restic/node.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/internal/restic/node.go b/internal/restic/node.go index 2ea9496ff..7870459cb 100644 --- a/internal/restic/node.go +++ b/internal/restic/node.go @@ -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 }