Repository navigation
add caching in hashTreeRoot #62
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43e43d0
add caching in hashTreeRoot
anshalshukla 8f748ec
merge master
anshalshukla 7b4ef08
address comments, add make max, min checks comptime
anshalshukla 3293fb6
merge master
anshalshukla 648966f
Apply suggestions from code review
gballet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ const Allocator = std.mem.Allocator; | |
| const Sha256 = std.crypto.hash.sha2.Sha256; | ||
|
|
||
| /// Number of bytes per chunk. | ||
| const BYTES_PER_CHUNK = 32; | ||
| pub const BYTES_PER_CHUNK = 32; | ||
|
|
||
| pub fn serializedFixedSize(T: type) !usize { | ||
| const info = @typeInfo(T); | ||
|
|
@@ -417,17 +417,13 @@ pub fn deserialize(T: type, serialized: []const u8, out: *T, allocator: ?Allocat | |
| const enforce_min = !has_custom_decode or comptime std.meta.hasFn(T, "minInLength"); | ||
| const enforce_max = !has_custom_decode or comptime std.meta.hasFn(T, "maxInLength"); | ||
|
|
||
| // Bounds check: ensure serialized length is within [minInLength, maxInLength] | ||
| const min_len: ?usize = if (enforce_min) blk: { | ||
| const m = minInLength(T) catch break :blk null; | ||
| break :blk m; | ||
| } else null; | ||
| // Bounds check: ensure serialized length is within [minInLength, maxInLength]. | ||
| // The bounds depend only on T, so fold them to comptime constants; types | ||
| // with no static bound (e.g. a slice) resolve to null and skip the check. | ||
| const min_len: ?usize = comptime if (enforce_min) (minInLength(T) catch null) else null; | ||
| if (min_len) |m| if (serialized.len < m) return error.PayloadTooSmall; | ||
|
|
||
| const max_len: ?usize = if (enforce_max) blk: { | ||
| const m = maxInLength(T) catch break :blk null; | ||
| break :blk m; | ||
| } else null; | ||
| const max_len: ?usize = comptime if (enforce_max) (maxInLength(T) catch null) else null; | ||
|
Comment on lines
+421
to
+424
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to cache min and max length because they have been made comptime and deserialize functions called for types whose size can be calculated will be predetermined and added to binary itself |
||
| if (max_len) |m| if (serialized.len > m) return error.PayloadTooLarge; | ||
|
|
||
| // shortcut if the type implements its own decode method | ||
|
|
@@ -734,8 +730,8 @@ pub fn chunkCount(T: type) usize { | |
| } | ||
| } | ||
|
|
||
| const chunk = [BYTES_PER_CHUNK]u8; | ||
| const zero_chunk: chunk = [_]u8{0} ** BYTES_PER_CHUNK; | ||
| pub const chunk = [BYTES_PER_CHUNK]u8; | ||
| pub const zero_chunk: chunk = [_]u8{0} ** BYTES_PER_CHUNK; | ||
|
|
||
| pub fn pack(T: type, values: T, l: *ArrayList(u8), allocator: Allocator) ![]chunk { | ||
| try serialize(T, values, l, allocator); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.