Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace strings.SplitN with strings.Cut #4405

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

amghazanfari
Copy link
Contributor

there are some cases in the code that this pattern used "strings.SplitN(expression, pattern, 2)"

as we see in #4403 strings.Cut has better performance than strings.SplitN.

i changed some of these cases (for rest of them i wasn't sure that it break the functionality or not)

exec.go Outdated
Comment on lines 250 to 261
u := strings.SplitN(user, ":", 2)
if len(u) > 1 {
gid, err := strconv.Atoi(u[1])
uidString, gidString, doesHaveGid := strings.Cut(user, ":")
if doesHaveGid {
gid, err := strconv.Atoi(gidString)
if err != nil {
return nil, fmt.Errorf("bad gid: %w", err)
}
p.User.GID = uint32(gid)
}
uid, err := strconv.Atoi(u[0])
uid, err := strconv.Atoi(uidString)
if err != nil {
return nil, fmt.Errorf("bad uid: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

It's common in go to use short variable names when the distance between defining and using them is short; also to avoid type suffixes (like String here`)

In situations like this, ok is also a common name (instead of the doesHaveGid); while doesHaveGid is somewhat more descriptive, it's likely clear from the code what it's handling; using ok can reduce cognitive overload, as a reader can recognize the pattern without understanding if doesHaveGid has additional purpose or (due to the longer name) possibly used further away from its declaration.

e.g., this could look like;

uid, gid, ok := strings.Cut(user, ":")
if ok {
	v, err := strconv.Atoi(gid)
	if err != nil {
		return nil, fmt.Errorf("bad gid: %w", err)
	}
	p.User.GID = uint32(v)
}
v, err := strconv.Atoi(uid)
if err != nil {
    return nil, fmt.Errorf("bad uid: %w", err)
}
p.User.UID = uint32(v)

Somewhat orthogonal to this PR (as it's existing code), but wondering if the strconv.Atoi(gid) would be better replaced with strconv.ParseUint(gid, 10, 32), given that we're ultimately casting to a uint32; i.e. strconv.Atoi("4294967296") will result in gid (uint32) 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for your comment. I change the variable names.

Copy link
Contributor Author

@amghazanfari amghazanfari Sep 24, 2024

Choose a reason for hiding this comment

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

i did a little benchmark for the second issue you told. and the result is like this:

goos: linux
goarch: amd64
pkg: bench_go
cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
BenchmarkAtoiToUint32-4   	116347470	        10.19 ns/op
BenchmarkParseUint-4      	62446058	        17.87 ns/op
PASS
ok  	bench_go	3.086s

as you can see using strconv.Atoi has better performance than ParseUint.

and also this is the code used for benchmark

package main

import (
	"strconv"
	"testing"
)

func AtoiToUint32(s string) uint32 {
	i, _ := strconv.Atoi(s)
	return uint32(i)
}

func ParseUint(s string) uint32 {
	i, _ := strconv.ParseUint(s, 10, 32)
	return uint32(i)
}

func BenchmarkAtoiToUint32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = AtoiToUint32("123456")
	}
}

func BenchmarkParseUint(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = ParseUint("123456")
	}
}

now with these results, do you think it's better if we change ParseUint to Atoi? and also i didn't know that but ParseUint return an uint64 so we need uint32 casting anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for checking that. Hm.. that's a good point. I was hoping it wouldn't make a big difference. It looks like we use the strconv.ParseUint approach in some other places already, so it's not unprecedented, and we're still looking at nanoseconds difference, so it's not a massive overhead, but of course every bit stacks up.

I was mostly looking at the "correctness" part here, because invalid values could be silently ignored, which may not be desirable, so perhaps worth the trade-off.

Alternatively, we could manually check for values to be out of range

@kolyshkin any thoughts?

☝️ regardless, I think it's a change we should do in a separate PR (for visibility; to not stuff it into a refactor).

libcontainer/init_linux.go Outdated Show resolved Hide resolved
libcontainer/utils/utils.go Outdated Show resolved Hide resolved
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

thanks!

code changes LGTM, but could you squash the commits?

@amghazanfari
Copy link
Contributor Author

thanks!

code changes LGTM, but could you squash the commits?

done

@thaJeztah
Copy link
Member

oh! If you still have time; I notice your DCO sign-off is using your GitHub handle, not your actual name; https://github.com/opencontainers/runc/blob/main/CONTRIBUTING.md#sign-your-work

using your real name (sorry, no pseudonyms or anonymous contributions.)

To be on the safe side (the DCO bot is sometimes picky), you may want to update your Git config (user.name) as well accordingly.

@amghazanfari
Copy link
Contributor Author

oh! If you still have time; I notice your DCO sign-off is using your GitHub handle, not your actual name; https://github.com/opencontainers/runc/blob/main/CONTRIBUTING.md#sign-your-work

using your real name (sorry, no pseudonyms or anonymous contributions.)

To be on the safe side (the DCO bot is sometimes picky), you may want to update your Git config (user.name) as well accordingly.

thanks for saying. I changed my user.name as well as my sign

@amghazanfari
Copy link
Contributor Author

in the test case. it gives permission denied. can you guide me, what is the problem. I didn't change anything that need permission to run

@thaJeztah
Copy link
Member

I think it may have been a flakey test; it looks green now.

I see there's a merge commit in the PR though; could you try doing another rebase to get rid of the merge commit? After that changes LGTM

@thaJeztah
Copy link
Member

And, of course now it's failing again! :sadpanda:

This one looks very unrelated, but for some reason it wants to use sudo, which isn't available (wondering if something changed in the "actuated" runners), or is the output just confusing and it's not sudo it's looking for?

Script started, output log file is 'typescript'.
rm -f libcontainer/dmz/binary/runc-dmz
go generate -tags "seccomp urfave_cli_no_docs" ./libcontainer/dmz
make[1]: Entering directory '/home/runner/actions-runner/_work/runc/runc/libcontainer/dmz'
gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc -static -o binary/runc-dmz _dmz.c
strip -gs binary/runc-dmz
make[1]: Leaving directory '/home/runner/actions-runner/_work/runc/runc/libcontainer/dmz'
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o runc .
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o contrib/cmd/memfd-bind/memfd-bind ./contrib/cmd/memfd-bind
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/recvtty/recvtty ./tests/cmd/recvtty
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/sd-helper/sd-helper ./tests/cmd/sd-helper
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/seccompagent/seccompagent ./tests/cmd/seccompagent
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/fs-idmap/fs-idmap ./tests/cmd/fs-idmap
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/pidfd-kill/pidfd-kill ./tests/cmd/pidfd-kill
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/remap-rootfs/remap-rootfs ./tests/cmd/remap-rootfs
tests/rootless.sh
[01] run rootless tests ... ()
path: /home/runner/.local/share/bats/bin:/home/runner/go/bin:/home/runner/actions-runner/_work/_tool/go/1.23.1/arm64/bin:/home/runner/.arkade/bin:/home/runner/actions-runner/_work/_temp:/home/runner/.dotnet/tools:/home/runner/.cargo/bin:/home/runner/go/bin:/home/runner/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sudo: : command not found

Same for this one; this one is missing bats;

Script started, output log file is 'typescript'.
rm -f libcontainer/dmz/binary/runc-dmz
go generate -tags "seccomp urfave_cli_no_docs" ./libcontainer/dmz
make[1]: Entering directory '/home/runner/actions-runner/_work/runc/runc/libcontainer/dmz'
gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib -lgcc -static -o binary/runc-dmz _dmz.c
strip -gs binary/runc-dmz
make[1]: Leaving directory '/home/runner/actions-runner/_work/runc/runc/libcontainer/dmz'
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o runc .
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o contrib/cmd/memfd-bind/memfd-bind ./contrib/cmd/memfd-bind
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/recvtty/recvtty ./tests/cmd/recvtty
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/sd-helper/sd-helper ./tests/cmd/sd-helper
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/seccompagent/seccompagent ./tests/cmd/seccompagent
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/fs-idmap/fs-idmap ./tests/cmd/fs-idmap
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/pidfd-kill/pidfd-kill ./tests/cmd/pidfd-kill
go build -trimpath "-buildmode=pie"  -tags "seccomp urfave_cli_no_docs" -ldflags "-X main.gitCommit=a7bcecd -X main.version=1.2.0-rc.3+dev " -o tests/cmd/remap-rootfs/remap-rootfs ./tests/cmd/remap-rootfs
bats -t tests/integration
/bin/bash: line 1: bats: command not found
make: *** [Makefile:181: localintegration] Error 127
Script done.
Error: Process completed with exit code 2.

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

changes LGTM

not sure what's happening in CI (but at least looks like very unrelated)

@thaJeztah
Copy link
Member

Oh! Perhaps related to this PR?

@thaJeztah
Copy link
Member

@kolyshkin Looks like DCO is now picky again 😞 perhaps needs a .mailmap entry; I guess we could manually mark it to pass?

Screenshot 2024-09-26 at 11 35 05

@lifubang
Copy link
Member

lifubang commented Sep 26, 2024

Maybe related to the commit msg:

Signed-off-by:  Amir M. Ghazanfari <[email protected]>
Signed-off-by: Amir M. Ghazanfari <[email protected]>

@amghazanfari Please remove the first line.
There are two spaces in the first line.

@amghazanfari
Copy link
Contributor Author

Maybe related to the commit msg:

Signed-off-by:  Amir M. Ghazanfari <[email protected]>
Signed-off-by: Amir M. Ghazanfari <[email protected]>

@amghazanfari Please remove the first line. There are two spaces in the first line.

sorry my bad. i fixed it

Copy link
Contributor

@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, thanks!

@kolyshkin kolyshkin merged commit f2d5624 into opencontainers:main Oct 4, 2024
42 checks passed
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.

5 participants