Skip to content

Commit

Permalink
perf: improve offset_to_bytes performance (#128)
Browse files Browse the repository at this point in the history
# 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
JayWhite2357 authored Sep 5, 2024
1 parent 1c01ad5 commit 51c4f98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
56 changes: 26 additions & 30 deletions crates/proof-of-sql/src/proof_primitive/dory/offset_to_bytes.rs
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)
}
}
4 changes: 2 additions & 2 deletions crates/proof-of-sql/src/proof_primitive/dory/pack_scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn modify_commits(
/// * `current_byte_size` - The current byte size of the column.
/// * `bit_table_sum_in_bytes` - The full bit table size in bytes.
/// * `num_columns` - The number of columns in a matrix commitment.
fn pack_bit<T: OffsetToBytes>(
fn pack_bit<const LEN: usize, T: OffsetToBytes<LEN>>(
column: &[T],
packed_scalars: &mut [u8],
current_bit_table_sum: usize,
Expand Down Expand Up @@ -179,7 +179,7 @@ fn pack_bit<T: OffsetToBytes>(
/// * `offset` - The offset to the data.
/// * `bit_table_sum_in_bytes` - The full bit table size in bytes.
/// * `num_columns` - The number of columns in a matrix commitment.
fn pack_offset_bit<T: OffsetToBytes>(
fn pack_offset_bit<const LEN: usize, T: OffsetToBytes<LEN>>(
column: &[T],
packed_scalars: &mut [u8],
current_bit_table_sum: usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-of-sql/src/proof_primitive/dory/transpose.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::proof_primitive::dory::offset_to_bytes::OffsetToBytes;

#[tracing::instrument(name = "transpose_for_fixed_msm (gpu)", level = "debug", skip_all)]
pub fn transpose_for_fixed_msm<T: OffsetToBytes>(
pub fn transpose_for_fixed_msm<const LEN: usize, T: OffsetToBytes<LEN>>(
column: &[T],
offset: usize,
rows: usize,
Expand Down

0 comments on commit 51c4f98

Please sign in to comment.