mount.RecursiveUnmount(): minor improvements#46
Merged
kolyshkin merged 2 commits intomoby:masterfrom Oct 2, 2020
Merged
Conversation
Member
thaJeztah
commented
Oct 2, 2020
- calculate "last mount" outside of the loop
- omit empty "sub-errors" in error message
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Member
Author
thaJeztah
commented
Oct 2, 2020
| var suberr error | ||
| var ( | ||
| suberr error | ||
| lastMount = len(mounts) - 1 |
Member
Author
There was a problem hiding this comment.
FWIW, not sure how smart the compiler would be, and if this change would be a no-op; if so, I can drop this commit.
Collaborator
There was a problem hiding this comment.
I think len() is a very fast operation in Go, as it just returns the length from the internal structure that represents a slice.
A benchmark would be helpful to find out for sure
Collaborator
There was a problem hiding this comment.
The difference is negligible (63 vs 64 ns per run), but it makes the code more readable so I think we can keep it.
Test case:
package mountinfo
import "testing"
func BenchmarkLenInLoop(b *testing.B) {
mounts, err := GetMounts(nil)
if err != nil {
b.Fatal(err)
}
var last *Info
b.ResetTimer()
for n := 0; n < b.N; n++ {
for i, m := range mounts {
if i == len(mounts)-1 { // last mount
last = m
}
}
}
b.Log("Last mount " + last.Mountpoint)
}
func BenchmarkLenOutOfLoop(b *testing.B) {
mounts, err := GetMounts(nil)
if err != nil {
b.Fatal(err)
}
var (
last *Info
lastMountIdx = len(mounts) - 1
)
b.ResetTimer()
for n := 0; n < b.N; n++ {
for i, m := range mounts {
if i == lastMountIdx { // last mount
last = m
}
}
}
b.Log("Last mount " + last.Mountpoint)
}Results:
$ go test -bench Len -benchtime 10s .
goos: linux
goarch: amd64
pkg: github.com/moby/sys/mountinfo
BenchmarkLenInLoop-8 186409401 64.3 ns/op
--- BENCH: BenchmarkLenInLoop-8
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:20: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
BenchmarkLenOutOfLoop-8 191245992 63.2 ns/op
--- BENCH: BenchmarkLenOutOfLoop-8
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
bench_test.go:41: Last mount /run/media/kir/8885b6f8-2cc0-4416-b5e9-dfecb3137d9a
PASS
ok github.com/moby/sys/mountinfo 36.921s
Member
Author
There was a problem hiding this comment.
Ah, yes, that's not much of a difference; thanks for testing!
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.