Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 44 additions & 57 deletions benches/byod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ mod benchmark {

#[cfg(feature = "experimental")]
mod benchmark {
use criterion::{BenchmarkId, Criterion};
use ion_rs::{v1_1::BinaryWriter, Element, *};
use std::{env, fs, hint::black_box};
use criterion::{Criterion, BenchmarkId};
use ion_rs::{v1_1::BinaryWriter, *, Element};

pub fn bench_byod_full(c: &mut Criterion) {
let Some(file) = env::var("ION_BENCH").ok() else {
Expand All @@ -56,15 +56,12 @@ mod benchmark {
read_group.measurement_time(std::time::Duration::from_secs(30));

// Read the provided data as-is with an encoding-agnostic reader.
read_group.bench_with_input(
BenchmarkId::new("full-read", &file),
&data,
|b, data| b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("Unable to create reader");
read_group.bench_with_input(BenchmarkId::new("full-read", &file), &data, |b, data| {
b.iter(|| {
let reader = Reader::new(AnyEncoding, data).expect("Unable to create reader");
full_read(reader);
})
);
});

// Convert the provided data into an ion 1.0 stream, and then measure the performance of
// reading the stream-equivalent data using a 1.0 reader.
Expand All @@ -75,11 +72,10 @@ mod benchmark {
|b, data| {
// Benchmark Read of known 1.0 data.
b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("Unable to create reader");
let reader = Reader::new(AnyEncoding, data).expect("Unable to create reader");
full_read(reader);
});
}
},
);
drop(one_oh_data);

Expand All @@ -91,11 +87,10 @@ mod benchmark {
&delimited_data,
|b, data| {
b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("unable to create reader");
let reader = Reader::new(AnyEncoding, data).expect("unable to create reader");
full_read(reader);
});
}
},
);
drop(delimited_data);

Expand All @@ -107,11 +102,10 @@ mod benchmark {
&prefixed_data,
|b, data| {
b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("unable to create reader");
let reader = Reader::new(AnyEncoding, data).expect("unable to create reader");
full_read(reader);
});
}
},
);
drop(prefixed_data);

Expand All @@ -123,11 +117,10 @@ mod benchmark {
&inlined_data,
|b, data| {
b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("unable to create reader");
let reader = Reader::new(AnyEncoding, data).expect("unable to create reader");
full_read(reader);
});
}
},
);
drop(inlined_data);

Expand All @@ -139,11 +132,10 @@ mod benchmark {
&referenced_data,
|b, data| {
b.iter(|| {
let reader = Reader::new(AnyEncoding, data)
.expect("unable to create reader");
let reader = Reader::new(AnyEncoding, data).expect("unable to create reader");
full_read(reader);
});
}
},
);
drop(referenced_data);

Expand All @@ -162,14 +154,13 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
b.iter(|| {
let buffer = Vec::<u8>::with_capacity(size);
let mut writer = BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
let mut writer =
BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
for elem in &elems {
writer
.write(elem)
.expect("unable to write value");
writer.write(elem).expect("unable to write value");
}
});
}
},
);

// Read the original data using the Element API, and re-write it to an ion 1.1 stream using
Expand All @@ -182,7 +173,8 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
b.iter(|| {
let buffer = Vec::<u8>::with_capacity(size);
let mut writer = BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
let mut writer =
BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
for elem in &elems {
writer
.value_writer()
Expand All @@ -191,7 +183,7 @@ mod benchmark {
.expect("unable to write value");
}
});
}
},
);

// Read the original data using the Element API, and re-write it to an ion 1.1 stream using
Expand All @@ -204,7 +196,8 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
b.iter(|| {
let buffer = Vec::<u8>::with_capacity(size);
let mut writer = BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
let mut writer =
BinaryWriter::new(v1_1::Binary, buffer).expect("unable to create writer");
for elem in &elems {
writer
.value_writer()
Expand All @@ -213,7 +206,7 @@ mod benchmark {
.expect("unable to write value");
}
});
}
},
);

// Read the original data using the Element API, and re-write it to an ion 1.1 stream using
Expand All @@ -226,7 +219,8 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
b.iter(|| {
let buffer = Vec::<u8>::with_capacity(size);
let mut writer = Writer::new(v1_1::Binary, buffer).expect("unable to create writer");
let mut writer =
Writer::new(v1_1::Binary, buffer).expect("unable to create writer");
for elem in &elems {
writer
.value_writer()
Expand All @@ -235,7 +229,7 @@ mod benchmark {
.expect("unable to write value");
}
});
}
},
);

// Read the original data using the Element API, and re-write it to an ion 1.0 stream using
Expand All @@ -248,15 +242,14 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
b.iter(|| {
let buffer = Vec::<u8>::with_capacity(size);
let mut writer = Writer::new(v1_0::Binary, buffer).expect("unable to create writer");
let mut writer =
Writer::new(v1_0::Binary, buffer).expect("unable to create writer");
for elem in &elems {
writer
.write(elem)
.expect("unable to write value");
writer.write(elem).expect("unable to write value");
}
let _ = writer.close();
});
}
},
);

write_group.finish();
Expand All @@ -274,9 +267,7 @@ mod benchmark {
.write(elem)
.expect("unable to write value");
}
writer
.close()
.expect("unable to close writer")
writer.close().expect("unable to close writer")
}

fn rewrite_delimited_containers(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -291,9 +282,7 @@ mod benchmark {
.write(elem)
.expect("unable to write value");
}
writer
.close()
.expect("unable to close writer")
writer.close().expect("unable to close writer")
}

fn rewrite_inline_symbols(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -310,9 +299,7 @@ mod benchmark {
.write(elem)
.expect("unable to write value");
}
writer
.close()
.expect("unable to close writer")
writer.close().expect("unable to close writer")
}

fn rewrite_referenced_symbols(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -329,9 +316,7 @@ mod benchmark {
.write(elem)
.expect("unable to write value");
}
writer
.close()
.expect("unable to close writer")
writer.close().expect("unable to close writer")
}

fn rewrite_inline_annotations(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -346,9 +331,7 @@ mod benchmark {
.write(elem)
.expect("unable to write value");
}
writer
.close()
.expect("unable to close writer")
writer.close().expect("unable to close writer")
}

fn rewrite_as_1_0(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -357,7 +340,9 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
// Write data as 1.0
let buffer = Vec::<u8>::with_capacity(size);
elems.encode_to(buffer, v1_0::Binary).expect("unable to re-encode elements")
elems
.encode_to(buffer, v1_0::Binary)
.expect("unable to re-encode elements")
}

fn rewrite_as_1_1(data: &Vec<u8>) -> Vec<u8> {
Expand All @@ -366,7 +351,9 @@ mod benchmark {
let elems = Element::read_all(data).expect("unable to read elements");
// Write data as 1.1
let buffer = Vec::<u8>::with_capacity(size);
elems.encode_to(buffer, v1_1::Binary).expect("unable to re-encode elements")
elems
.encode_to(buffer, v1_1::Binary)
.expect("unable to re-encode elements")
}

#[inline]
Expand Down
26 changes: 17 additions & 9 deletions benches/encoding_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod benchmark {
#[cfg(feature = "experimental")]
mod benchmark {
use criterion::{black_box, Criterion};
use ion_rs::{IonResult, EncodingContext};
use ion_rs::{EncodingContext, IonResult};
use rand::prelude::StdRng;
use rand::{distributions::Uniform, Rng, SeedableRng};
use std::io;
Expand Down Expand Up @@ -73,7 +73,8 @@ mod benchmark {
b.iter(|| {
let encoding_context = EncodingContext::empty();
let mut decoded_length: usize = 0;
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_var_uints.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_var_uints.as_slice());
for _ in 0..unsigned_values.len() {
let (var_uint, remaining) = input.read_var_uint().unwrap();
input = remaining;
Expand All @@ -96,7 +97,8 @@ mod benchmark {
b.iter(|| {
let encoding_context = EncodingContext::empty();
let mut decoded_length: usize = 0;
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_var_ints.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_var_ints.as_slice());
for _ in 0..unsigned_values.len() {
let (var_int, remaining) = input.read_var_int().unwrap();
input = remaining;
Expand All @@ -122,7 +124,8 @@ mod benchmark {
b.iter(|| {
let encoding_context = EncodingContext::empty();
let mut decoded_length: usize = 0;
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_flex_uints.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_flex_uints.as_slice());
for _ in 0..unsigned_values.len() {
let (flex_uint, remaining) = input.read_flex_uint().unwrap();
input = remaining;
Expand All @@ -145,7 +148,8 @@ mod benchmark {
b.iter(|| {
let encoding_context = EncodingContext::empty();
let mut decoded_length: usize = 0;
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_flex_ints.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_flex_ints.as_slice());
for _ in 0..unsigned_values.len() {
let (flex_int, remaining) = input.read_flex_int().unwrap();
input = remaining;
Expand All @@ -164,7 +168,8 @@ mod benchmark {
VarUInt::write_u64(&mut encoded_values_buffer, *value)?;
}
let mut decoded_values = Vec::new();
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
for _ in 0..unsigned_values.len() {
let (var_uint, remaining) = input.read_var_uint()?;
input = remaining;
Expand All @@ -181,7 +186,8 @@ mod benchmark {
VarInt::write_i64(&mut encoded_values_buffer, *value)?;
}
let mut decoded_values = Vec::new();
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
for _ in 0..signed_values.len() {
let (var_int, remaining) = input.read_var_int()?;
input = remaining;
Expand All @@ -198,7 +204,8 @@ mod benchmark {
FlexUInt::write(&mut encoded_values_buffer, *value)?;
}
let mut decoded_values = Vec::new();
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
for _ in 0..unsigned_values.len() {
let (flex_uint, remaining) = input.read_flex_uint()?;
input = remaining;
Expand All @@ -215,7 +222,8 @@ mod benchmark {
FlexInt::write_i64(&mut encoded_values_buffer, *value)?;
}
let mut decoded_values = Vec::new();
let mut input = BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
let mut input =
BinaryBuffer::new(encoding_context.get_ref(), encoded_values_buffer.as_slice());
for _ in 0..signed_values.len() {
let (flex_int, remaining) = input.read_flex_int()?;
input = remaining;
Expand Down
2 changes: 1 addition & 1 deletion benches/write_many_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod benchmark {

#[cfg(feature = "experimental")]
mod benchmark {
use std::hint::black_box;
use criterion::Criterion;
use ion_rs::{v1_0, v1_1, IonResult, RawSymbolRef, SequenceWriter, StructWriter, ValueWriter};
use std::hint::black_box;

fn write_struct_with_string_values(value_writer: impl ValueWriter) -> IonResult<()> {
let mut struct_ = value_writer.struct_writer()?;
Expand Down
11 changes: 9 additions & 2 deletions src/lazy/binary/raw/v1_1/binary_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,20 @@ impl<'a> BinaryBuffer<'a> {
Ok((value, remaining_input))
}

pub fn read_fixed_uint_as_lazy_value(self, encoding: BinaryValueEncoding) -> ParseResult<'a, LazyRawBinaryValue_1_1<'a>> {
pub fn read_fixed_uint_as_lazy_value(
self,
encoding: BinaryValueEncoding,
) -> ParseResult<'a, LazyRawBinaryValue_1_1<'a>> {
let size_in_bytes = match encoding {
BinaryValueEncoding::UInt8 => 1,
BinaryValueEncoding::UInt16 => 2,
BinaryValueEncoding::UInt32 => 4,
BinaryValueEncoding::UInt64 => 8,
_ => return IonResult::illegal_operation(format!("invalid binary encoding for fixed uint: {encoding:?}")),
_ => {
return IonResult::illegal_operation(format!(
"invalid binary encoding for fixed uint: {encoding:?}"
))
}
};

if self.len() < size_in_bytes {
Expand Down
Loading
Loading