From c83c5ce3e685d1263954e694a34ae0049920b69a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 1 Dec 2024 00:30:51 +0000 Subject: [PATCH] sort _realNamePairs from shortest to longest obfuscated name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This lets us only check names up to the remaining input string length. │ old │ new │ │ sec/op │ sec/op vs base │ AbiRealName-8 196.7µ ± 1% 172.3µ ± 1% -12.41% (p=0.001 n=7) │ old │ new │ │ B/s │ B/s vs base │ AbiRealName-8 1.774Mi ± 1% 2.022Mi ± 0% +13.98% (p=0.001 n=7) │ old │ new │ │ B/op │ B/op vs base │ AbiRealName-8 5.359Ki ± 0% 5.336Ki ± 0% -0.44% (p=0.001 n=7) │ old │ new │ │ allocs/op │ allocs/op vs base │ AbiRealName-8 19.00 ± 0% 18.00 ± 0% -5.26% (p=0.001 n=7) --- reflect_abi_code.go | 6 +++++- reflect_abi_patch.go | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/reflect_abi_code.go b/reflect_abi_code.go index ce90d8d..53b8dfe 100644 --- a/reflect_abi_code.go +++ b/reflect_abi_code.go @@ -44,7 +44,9 @@ func _realName(name string) string { real := pair[1] keyLen := len(obfName) if remLen < keyLen { - continue + // Since the pairs are sorted from shortest to longest name, + // we know that the rest of the pairs are at least just as long. + break } if name[i:i+keyLen] == obfName { name = name[:i] + real + name[i+keyLen:] @@ -60,4 +62,6 @@ func _realName(name string) string { return name } +// Each pair is the obfuscated and then the real name. +// The slice is sorted from shortest to longest obfuscated name. var _realNamePairs = [][2]string{} diff --git a/reflect_abi_patch.go b/reflect_abi_patch.go index 731b47b..3af6104 100644 --- a/reflect_abi_patch.go +++ b/reflect_abi_patch.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "cmp" _ "embed" "fmt" "maps" @@ -62,7 +63,12 @@ func reflectMainPostPatch(file []byte, lpkg *listedPackage, pkg pkgCache) []byte nameMap := fmt.Sprintf("%s = [][2]string{", obfVarName) var b strings.Builder - keys := slices.Sorted(maps.Keys(pkg.ReflectObjectNames)) + keys := slices.SortedFunc(maps.Keys(pkg.ReflectObjectNames), func(a, b string) int { + if c := cmp.Compare(len(a), len(b)); c != 0 { + return c + } + return cmp.Compare(a, b) + }) for _, obf := range keys { b.WriteString(fmt.Sprintf("{%q, %q},", obf, pkg.ReflectObjectNames[obf])) }