From d37a9726f322ee0307cafc6b81e93fc6687c7f99 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 1 Feb 2022 10:46:14 -0800 Subject: [PATCH 1/2] libct/specconv: test nits Commit 643f8a2b408d5f renamed isValidName to checkPropertyName, but fell short of renaming its test and benchmark. Fix that. Fixes: 643f8a2b408d5f Signed-off-by: Kir Kolyshkin --- libcontainer/specconv/spec_linux_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libcontainer/specconv/spec_linux_test.go b/libcontainer/specconv/spec_linux_test.go index 56d808699..a7df54411 100644 --- a/libcontainer/specconv/spec_linux_test.go +++ b/libcontainer/specconv/spec_linux_test.go @@ -762,7 +762,7 @@ func TestInitSystemdProps(t *testing.T) { } } -func TestIsValidName(t *testing.T) { +func TestCheckPropertyName(t *testing.T) { testCases := []struct { in string valid bool @@ -787,7 +787,7 @@ func TestIsValidName(t *testing.T) { } } -func BenchmarkIsValidName(b *testing.B) { +func BenchmarkCheckPropertyName(b *testing.B) { for i := 0; i < b.N; i++ { for _, s := range []string{"", "xx", "xxx", "someValidName", "A name", "Кир", "მადლობა", "合い言葉"} { _ = checkPropertyName(s) From 376c988618108040679ee9413353e0da07fae404 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 1 Feb 2022 11:44:12 -0800 Subject: [PATCH 2/2] libct/specconv: improve checkPropertyName MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit 029b73c1b replaced a regular expression with code checking the characters. Despite what the comment said about ASCII, the check was performed rune by rune, not byte by byte. Note the check was still correct, basically comparing int32's, but the byte by byte way is a tad faster and more straightforward. The change also fixes the issue of a misleading comment. Benchmark before/after shows a modest improvement: name old time/op new time/op delta CheckPropertyName-4 164ns ± 2% 123ns ± 2% -24.73% (p=0.029 n=4+4) name old alloc/op new alloc/op delta CheckPropertyName-4 96.0B ± 0% 64.0B ± 0% -33.33% (p=0.029 n=4+4) name old allocs/op new allocs/op delta CheckPropertyName-4 6.00 ± 0% 4.00 ± 0% -33.33% (p=0.029 n=4+4) Fixes: 029b73c1b Signed-off-by: Kir Kolyshkin --- libcontainer/specconv/spec_linux.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index d8a4d026b..fad7b802e 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -538,8 +538,10 @@ func checkPropertyName(s string) error { if len(s) < 3 { return errors.New("too short") } - // Check ASCII characters rather than Unicode runes. - for _, ch := range s { + // Check ASCII characters rather than Unicode runes, + // so we have to use indexes rather than range. + for i := 0; i < len(s); i++ { + ch := s[i] if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') { continue }