Skip to content

perf: cache key building and handler hex processing#646

Open
Sahil-4555 wants to merge 1 commit intoethpandaops:masterfrom
Sahil-4555:simplify-cache-and-handler-internals
Open

perf: cache key building and handler hex processing#646
Sahil-4555 wants to merge 1 commit intoethpandaops:masterfrom
Sahil-4555:simplify-cache-and-handler-internals

Conversation

@Sahil-4555
Copy link
Copy Markdown
Contributor

Cleaned up internal string handling in the cache and handlers packages.

Used plain string concatenation instead of fmt.Sprintf for redis key building, and strconv.FormatUint / strconv.FormatBool for value serialization. Fixed a bug where GetBool was missing the key prefix so lookups never matched. Extracted a pageKeyPrefix helper using strings.Cut instead of strings.Split for cache key parsing. Fixed a race in the SSZ compatibility check by re-checking the map after acquiring the write lock. In the handlers, replaced strings.Replace(s, "0x", "", -1) with strings.TrimPrefix since we only strip the leading prefix. Switched fmt.Sprintf("%x", hash) to hex.EncodeToString for map key encoding to match the DB layer. Replaced fmt.Sprintf byte-slice comparison with bytes.Equal. Returned a string literal for constant client type name instead of formatting it at runtime.

What changed Before After Speedup Allocs
prefix + key instead of fmt.Sprintf for cache keys 122 ns/op 16.7 ns/op 1 → 0
strings.Cut instead of strings.Split for key prefix 89 ns/op 7.8 ns/op 11× 1 → 0
strings.TrimPrefix instead of strings.Replace for 0x 106 ns/op 0.27 ns/op 390× 1 → 0
hex.EncodeToString instead of fmt.Sprintf("%x") for map keys 261 ns/op 139 ns/op 1.9× same
bytes.Equal instead of fmt.Sprintf compare for block hashes 473 ns/op 4.8 ns/op 99× 4 → 0
String literal instead of fmt.Sprintf for client type 121 ns/op 0.27 ns/op 448× 1 → 0

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.

1 participant