-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve
offset_to_bytes
performance (#128)
# Rationale for this change This PR reduces the cost of packing bytes in order to set up a packed MSM. # What changes are included in this PR? `offset_to_bytes` now returns an array rather than allocating a `Vec`. # Are these changes tested? Yes
- Loading branch information
1 parent
1c01ad5
commit 51c4f98
Showing
3 changed files
with
29 additions
and
33 deletions.
There are no files selected for viewing
56 changes: 26 additions & 30 deletions
56
crates/proof-of-sql/src/proof_primitive/dory/offset_to_bytes.rs
This file contains 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 |
---|---|---|
@@ -1,59 +1,55 @@ | ||
use zerocopy::AsBytes; | ||
|
||
pub trait OffsetToBytes { | ||
fn offset_to_bytes(&self) -> Vec<u8>; | ||
pub trait OffsetToBytes<const LEN: usize> { | ||
fn offset_to_bytes(&self) -> [u8; LEN]; | ||
} | ||
|
||
impl OffsetToBytes for u8 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
vec![*self] | ||
impl OffsetToBytes<1> for u8 { | ||
fn offset_to_bytes(&self) -> [u8; 1] { | ||
[*self] | ||
} | ||
} | ||
|
||
impl OffsetToBytes for i16 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
impl OffsetToBytes<2> for i16 { | ||
fn offset_to_bytes(&self) -> [u8; 2] { | ||
let shifted = self.wrapping_sub(i16::MIN); | ||
shifted.to_le_bytes().to_vec() | ||
shifted.to_le_bytes() | ||
} | ||
} | ||
|
||
impl OffsetToBytes for i32 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
impl OffsetToBytes<4> for i32 { | ||
fn offset_to_bytes(&self) -> [u8; 4] { | ||
let shifted = self.wrapping_sub(i32::MIN); | ||
shifted.to_le_bytes().to_vec() | ||
shifted.to_le_bytes() | ||
} | ||
} | ||
|
||
impl OffsetToBytes for i64 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
impl OffsetToBytes<8> for i64 { | ||
fn offset_to_bytes(&self) -> [u8; 8] { | ||
let shifted = self.wrapping_sub(i64::MIN); | ||
shifted.to_le_bytes().to_vec() | ||
shifted.to_le_bytes() | ||
} | ||
} | ||
|
||
impl OffsetToBytes for i128 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
impl OffsetToBytes<16> for i128 { | ||
fn offset_to_bytes(&self) -> [u8; 16] { | ||
let shifted = self.wrapping_sub(i128::MIN); | ||
shifted.to_le_bytes().to_vec() | ||
shifted.to_le_bytes() | ||
} | ||
} | ||
|
||
impl OffsetToBytes for bool { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
vec![*self as u8] | ||
impl OffsetToBytes<1> for bool { | ||
fn offset_to_bytes(&self) -> [u8; 1] { | ||
[*self as u8] | ||
} | ||
} | ||
|
||
impl OffsetToBytes for u64 { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
let bytes = self.to_le_bytes(); | ||
bytes.to_vec() | ||
impl OffsetToBytes<8> for u64 { | ||
fn offset_to_bytes(&self) -> [u8; 8] { | ||
self.to_le_bytes() | ||
} | ||
} | ||
|
||
impl OffsetToBytes for [u64; 4] { | ||
fn offset_to_bytes(&self) -> Vec<u8> { | ||
let slice = self.as_bytes(); | ||
slice.to_vec() | ||
impl OffsetToBytes<32> for [u64; 4] { | ||
fn offset_to_bytes(&self) -> [u8; 32] { | ||
bytemuck::cast(*self) | ||
} | ||
} |
This file contains 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
This file contains 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