-
Notifications
You must be signed in to change notification settings - Fork 4k
refactor: pre calculate the empty hash to save cost #25359
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
base: main
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughRefactors empty hash handling in store/internal/tree/hash.go by replacing a helper function with a package-level byte slice initialized from a hex string, updating call sites accordingly, and adding the required import. Other hashing logic remains unchanged. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (2)
store/internal/tree/hash.go (2)
5-5
: Drop hex import after switching to array precompute (or handle the error).If you adopt the array approach,
encoding/hex
is unused; remove it. If you keep hex decoding, don’t ignore the error—validate at init and panic on malformed constant.Apply this diff when using the array:
import ( "crypto/sha256" - "encoding/hex" "hash" "math/bits" )
If keeping hex:
-var emptyHash, _ = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") +var emptyHash = func() []byte { + b, err = hex.DecodeString("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") + if err != nil { panic(err) } + return b +}()
21-31
: Trim the generator comment; keep a terse, source-of-truth note.The multi-line “generate from this code” block is noisy. Replace with a concise doc explaining the precomputation.
Apply this diff:
-// generate from this code -// -// -// func emptyHash() []byte { -// h := sha256.Sum256([]byte{}) -// return h[:] -// } -// -// -// func main() { -// println(hex.EncodeToString(emptyHash())) -// } +// emptyHashArr is sha256.Sum256(nil) precomputed once to avoid per-call hashing.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Knowledge base: Disabled due to Reviews -> Disable Knowledge Base
setting
📒 Files selected for processing (1)
store/internal/tree/hash.go
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Summary
Use a precomputed [32]byte and copy on return (32B), which is safe and still cheap. why cant we just do this ^^ |
75ee605
to
a9af71e
Compare
done |
I'd like to add a test to verify that this is the actual empty hash |
a9af71e
to
cd8ef35
Compare
Done. |
Summary by CodeRabbit