Skip to content

mount.RecursiveUnmount(): minor improvements#46

Merged
kolyshkin merged 2 commits intomoby:masterfrom
thaJeztah:last_mount
Oct 2, 2020
Merged

mount.RecursiveUnmount(): minor improvements#46
kolyshkin merged 2 commits intomoby:masterfrom
thaJeztah:last_mount

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

  • 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>
@thaJeztah
Copy link
Copy Markdown
Member Author

@kolyshkin

Comment thread mount/mount_unix.go
var suberr error
var (
suberr error
lastMount = len(mounts) - 1
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

@kolyshkin kolyshkin Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that's not much of a difference; thanks for testing!

Copy link
Copy Markdown
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kolyshkin kolyshkin merged commit 13322f1 into moby:master Oct 2, 2020
@thaJeztah thaJeztah deleted the last_mount branch October 2, 2020 21:10
@thaJeztah thaJeztah mentioned this pull request Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants