Skip to content

Commit

Permalink
HashMap to BTreeMap to improve startup
Browse files Browse the repository at this point in the history
  • Loading branch information
sokorototo committed Sep 2, 2022
1 parent 663b6ad commit be18502
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 188 deletions.
221 changes: 101 additions & 120 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion vach-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false
[dependencies]
criterion = "0.3.5"
vach = { path = "../vach", features = ["compression", "crypto"] }
rayon = "*"

[[bench]]
name = "benchmark"
Expand Down
24 changes: 2 additions & 22 deletions vach-benchmarks/benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;
use std::io;
use criterion::{Criterion, black_box, criterion_group, criterion_main, Throughput};

use rayon::iter::{ParallelIterator, IntoParallelRefIterator};
use criterion::{Criterion, black_box, criterion_group, criterion_main, Throughput};
use vach::prelude::*;
use vach::crypto_utils::gen_keypair;

Expand Down Expand Up @@ -80,12 +78,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {

{
// Builds an archive source from which to benchmark
let template = Leaf::default()
.encrypt(false)
.sign(false)
.compress(CompressMode::Never)
.compression_algo(CompressionAlgorithm::LZ4);
let mut builder = Builder::new().template(template);
let mut builder = Builder::new().template(Leaf::default().encrypt(false).sign(false));

// Add data
builder.add(data_1, "d1").unwrap();
Expand Down Expand Up @@ -119,19 +112,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
});
});

throughput_group.bench_function("Archive::fetch_batch(---)", |b| {
// Load data
b.iter(|| {
let resources = ["d2", "d1", "d3"]
.as_slice()
.par_iter()
.map(|id| (id, archive.fetch(&id)))
.collect::<HashMap<_, _>>();

criterion::black_box(resources)
});
});

drop(throughput_group);

c.bench_function("Archive::LOAD_NEW", |b| {
Expand Down
6 changes: 3 additions & 3 deletions vach/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ snap = { version = "1.0.5", optional = true }
brotli = { version = "3.3.4", optional = true }

# Multithreaded features
parking_lot = { version = "0.12.1", optional = true }
parking_lot = { version = "0.12.1" }
rayon = { version = "1.5.2", optional = true }
num_cpus = { version = "1.13.1", optional = true }

[features]
default = ["builder", "archive"]

archive = ["parking_lot"]
builder = ["parking_lot"]
archive = []
builder = []

crypto = ["ed25519-dalek", "aes-gcm", "rand"]
multithreaded = ["rayon", "num_cpus"]
Expand Down
12 changes: 6 additions & 6 deletions vach/src/loader/archive.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{
str,
io::{Read, Seek, SeekFrom},
collections::HashMap,
collections::BTreeMap,
};

use parking_lot::Mutex;

use super::resource::Resource;
use crate::{
global::{
Expand All @@ -17,6 +15,8 @@ use crate::{
},
};

use parking_lot::Mutex;

#[cfg(feature = "crypto")]
use crate::crypto;

Expand All @@ -37,7 +37,7 @@ pub struct Archive<T> {

// Archive metadata
header: Header,
entries: HashMap<String, RegistryEntry>,
entries: BTreeMap<String, RegistryEntry>,

// Optional parts
#[cfg(feature = "crypto")]
Expand Down Expand Up @@ -190,7 +190,7 @@ where
Header::validate(config, &header)?;

// Generate and store Registry Entries
let mut entries = HashMap::with_capacity(header.capacity as usize);
let mut entries = BTreeMap::new();

// Construct entries map
for _ in 0..header.capacity {
Expand Down Expand Up @@ -251,7 +251,7 @@ where

/// Returns an immutable reference to the underlying [`HashMap`]. This hashmap stores [`RegistryEntry`] values and uses `String` keys.
#[inline(always)]
pub fn entries(&self) -> &HashMap<String, RegistryEntry> {
pub fn entries(&self) -> &BTreeMap<String, RegistryEntry> {
&self.entries
}

Expand Down
32 changes: 0 additions & 32 deletions vach/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,38 +203,6 @@ fn fetch_with_signature() -> InternalResult {
Ok(())
}

#[test]
#[cfg(all(feature = "archive", feature = "crypto"))]
fn fetch_write_with_signature() -> InternalResult {
let target = File::open(SIGNED_TARGET)?;

// Load keypair
let mut config = ArchiveConfig::default();
let keypair = &KEYPAIR[crate::SECRET_KEY_LENGTH..];
config.load_public_key(keypair)?;

let archive = Archive::with_config(target, &config)?;

let resource = archive.fetch("test_data/poem.txt")?;
assert!(resource.authenticated);
assert!(resource.flags.contains(Flags::SIGNED_FLAG));

// Windows bullshit
#[cfg(target_os = "windows")]
{
assert_eq!(resource.data.len(), 359);
}
#[cfg(not(any(target_os = "windows", target_os = "ios")))]
{
assert_eq!(resource.data.len(), 345);
}

// Assert identity of retrieved data
println!("{}", String::from_utf8(resource.data).unwrap());

Ok(())
}

#[test]
#[cfg(feature = "crypto")]
fn edcryptor_test() -> InternalResult {
Expand Down
6 changes: 3 additions & 3 deletions vach/src/writer/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a> Builder<'a> {
/// The `data` is wrapped in the default [`Leaf`], without cloning the original data.
/// The second argument is the `ID` with which the embedded data will be tagged
/// ### Errors
/// Returns an `Err(())` if a Leaf with the specified ID exists.
/// - if a Leaf with the specified ID exists.
pub fn add<D: Read + Send + Sync + 'a>(&mut self, data: D, id: impl AsRef<str>) -> InternalResult {
let leaf = Leaf::new(data)
.id(id.as_ref().to_string())
Expand All @@ -62,10 +62,10 @@ impl<'a> Builder<'a> {

/// Loads all files from a directory, parses them into [`Leaf`]s and appends them into the processing queue.
/// An optional [`Leaf`] is passed as a template from which the new [`Leaf`]s shall implement, pass `None` to use the [`Builder`] internal default template.
/// Appended [`Leaf`]s have an `ID` in the form of of: `directory_name/file_name`. For example: "sounds/footstep.wav", "sample/script.data"
/// Appended [`Leaf`]s have an `ID` in the form of of: `directory_name/file_name`. For example: `sounds/footstep.wav1, `sample/script.data`
/// ## Errors
/// - Any of the underlying calls to the filesystem fail.
/// - The internal call to `Builder::add_leaf()` returns an error.
/// - The internal call to `Builder::add_leaf()` fails.
pub fn add_dir(&mut self, path: impl AsRef<Path>, template: Option<&Leaf<'a>>) -> InternalResult {
use std::fs;

Expand Down
2 changes: 1 addition & 1 deletion vach/src/writer/compress_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub enum CompressMode {
#[cfg(feature = "compression")]
impl Default for CompressMode {
fn default() -> CompressMode {
CompressMode::Detect
CompressMode::Never
}
}

0 comments on commit be18502

Please sign in to comment.