Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
some unsafe code for performance :P
Browse files Browse the repository at this point in the history
  • Loading branch information
pwbh committed Oct 30, 2023
1 parent 975130e commit 1bdb41c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
11 changes: 11 additions & 0 deletions nyx-storage/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ pub struct Prune<'a> {
pub offsets: &'a [Offset],
}

impl<'a> Prune<'a> {
pub fn as_bytes(&self) -> &[u8] {
unsafe {
std::slice::from_raw_parts(
self.buffer.as_ptr() as *const u8,
self.buffer.len() * std::mem::size_of::<Offset>(),
)
}
}
}

#[derive(Debug)]
pub struct Batch {
buffer: [u8; MAX_BATCH_SIZE],
Expand Down
2 changes: 1 addition & 1 deletion nyx-storage/src/indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Indices {
pub total_bytes: usize,
}

const INDEX_SIZE: usize = std::mem::size_of::<usize>() * 4;
const INDEX_SIZE: usize = std::mem::size_of::<Offset>();

impl Indices {
pub async fn from(directory: &Directory) -> io::Result<Self> {
Expand Down
21 changes: 9 additions & 12 deletions nyx-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use batch::{Batch, BatchState};
use compactor::Compactor;
use directory::{DataType, Directory};
use indices::Indices;
use offset::Offset;
use segment::Segment;
use segmentation_manager::SegmentationManager;

Expand Down Expand Up @@ -123,23 +124,19 @@ impl Storage {

latest_partition_file.write_all(prune.buffer).await?;

// TODO: get ptr to underlying layout and just write that to file.
for offset in prune.offsets {
let length = self.indices.data.len();
// println!("Inserting offset {:?} at index {}", offset, length);
self.indices.data.insert(length, offset.clone());
}

let offset = offset.as_bytes();

let latest_indices_segment = self
.segmentation_manager
.get_latest_segment(DataType::Indices)
.await?;
let latest_indices_segment = self
.segmentation_manager
.get_latest_segment(DataType::Indices)
.await?;

let mut latest_indices_file = &latest_indices_segment.file;
let mut latest_indices_file = &latest_indices_segment.file;

latest_indices_file.write_all(offset).await?;
}
latest_indices_file.write_all(prune.as_bytes()).await?;

Ok(prune.buffer.len())
}
Expand Down Expand Up @@ -229,7 +226,7 @@ mod tests {
#[async_std::test]
#[cfg_attr(miri, ignore)]
async fn get_returns_ok() {
let message_count = 1_000_000;
let message_count = 100_000;
let test_message = b"hello guys";

let mut storage = setup_test_storage(&function!(), test_message, message_count).await;
Expand Down

0 comments on commit 1bdb41c

Please sign in to comment.