Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mwlon committed Jul 15, 2023
1 parent aa95363 commit da24596
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 72 deletions.
6 changes: 5 additions & 1 deletion bench/src/codecs/pco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ impl CodecInternal for PcoConfig {
fn get_conf(&self, key: &str) -> String {
match key {
"level" => self.compressor_config.compression_level.to_string(),
"delta_order" => self.compressor_config.delta_encoding_order.map(|order| order.to_string()).unwrap_or("auto".to_string()),
"delta_order" => self
.compressor_config
.delta_encoding_order
.map(|order| order.to_string())
.unwrap_or("auto".to_string()),
"use_gcds" => self.compressor_config.use_gcds.to_string(),
"use_float_mult" => self.compressor_config.use_float_mult.to_string(),
_ => panic!("bad conf"),
Expand Down
6 changes: 4 additions & 2 deletions pco/src/ans/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::constants::Bitlen;
use crate::data_types::UnsignedLike;
use crate::errors::PcoResult;

use crate::chunk_metadata::DataPageStreamMetadata;
use crate::ChunkStreamMetadata;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -49,7 +48,10 @@ impl Decoder {
}
}

pub fn from_stream_meta<U: UnsignedLike>(stream: &ChunkStreamMetadata<U>, final_state: usize) -> PcoResult<Self> {
pub fn from_stream_meta<U: UnsignedLike>(
stream: &ChunkStreamMetadata<U>,
final_state: usize,
) -> PcoResult<Self> {
let weights = stream.bins.iter().map(|bin| bin.weight).collect::<Vec<_>>();
let spec = Spec::from_weights(stream.ans_size_log, weights)?;
Ok(Self::new(&spec, final_state))
Expand Down
20 changes: 10 additions & 10 deletions pco/src/base_compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::modes::classic::ClassicMode;
use crate::modes::gcd::{use_gcd_arithmetic, GcdMode};
use crate::modes::{gcd, ConstMode, Mode};
use crate::unsigned_src_dst::{Decomposed, DecomposedSrc, StreamSrc};
use crate::{auto, Flags};
use crate::{ans, delta_encoding};
use crate::{auto, Flags};
use crate::{bin_optimization, float_mult_utils};

/// All configurations available for a compressor.
Expand Down Expand Up @@ -144,8 +144,7 @@ impl InternalCompressorConfig {
if order > MAX_DELTA_ENCODING_ORDER {
return Err(PcoError::invalid_argument(format!(
"delta encoding order may not exceed {} (was {})",
MAX_DELTA_ENCODING_ORDER,
order,
MAX_DELTA_ENCODING_ORDER, order,
)));
}
}
Expand Down Expand Up @@ -368,11 +367,12 @@ fn mode_decompose_unsigneds<U: UnsignedLike, M: ConstMode<U>>(
};
let mut decomposeds: [Vec<Decomposed<U>>; MAX_N_STREAMS] =
core::array::from_fn(|stream_idx| empty_decomposeds(src.stream(stream_idx).len()));
let mut ans_final_states = core::array::from_fn(|stream_idx|
1 << stream_configs.get(stream_idx)
let mut ans_final_states = core::array::from_fn(|stream_idx| {
1 << stream_configs
.get(stream_idx)
.map(|config| config.encoder.size_log())
.unwrap_or_default()
);
});
for stream_idx in 0..n_nontrivial_streams {
let stream = src.stream(stream_idx);
let StreamConfig { table, encoder, .. } = &mut stream_configs[stream_idx];
Expand Down Expand Up @@ -406,10 +406,10 @@ fn decompose_unsigneds<U: UnsignedLike>(
needs_gcds,
..
} = mid_chunk_info;
match needs_gcds {
false => mode_decompose_unsigneds::<U, ClassicMode>(stream_configs, src, *n_nontrivial_streams),
true => mode_decompose_unsigneds::<U, GcdMode>(stream_configs, src, *n_nontrivial_streams),
_ => panic!("unknown streams; should be unreachable"),
if *needs_gcds {
mode_decompose_unsigneds::<U, GcdMode>(stream_configs, src, *n_nontrivial_streams)
} else {
mode_decompose_unsigneds::<U, ClassicMode>(stream_configs, src, *n_nontrivial_streams)
}
}

Expand Down
11 changes: 8 additions & 3 deletions pco/src/base_decompressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::io::Write;
use crate::bit_reader::BitReader;
use crate::bit_words::PaddedBytes;
use crate::body_decompressor::BodyDecompressor;
use crate::chunk_metadata::{ChunkMetadata, DataPageMetadata, DataPageStreamMetadata};
use crate::chunk_metadata::{ChunkMetadata, DataPageMetadata};
use crate::constants::{MAGIC_CHUNK_BYTE, MAGIC_HEADER, MAGIC_TERMINATION_BYTE};
use crate::data_types::NumberLike;
use crate::delta_encoding::DeltaMoments;

use crate::errors::{PcoError, PcoResult};
use crate::Flags;

Expand Down Expand Up @@ -124,7 +124,12 @@ impl<T: NumberLike> State<T> {
PcoError::corruption("compressed page size {} is less than data page metadata size")
})?;

BodyDecompressor::new(n, compressed_body_size, chunk_meta, data_page_meta)
BodyDecompressor::new(
n,
compressed_body_size,
chunk_meta,
data_page_meta,
)
}

pub fn step(&self) -> Step {
Expand Down
5 changes: 2 additions & 3 deletions pco/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ans::Token;
use crate::chunk_metadata::DataPageStreamMetadata;

use crate::constants::Bitlen;
use crate::data_types::UnsignedLike;
use crate::{ChunkStreamMetadata, Mode};

/// Part of [`ChunkStreamMetadata`][`crate::ChunkStreamMetadata`] representing
/// a numerical range.
Expand Down Expand Up @@ -82,5 +81,5 @@ impl<U: UnsignedLike> From<&Bin<U>> for BinDecompressionInfo<U> {
}

pub(crate) fn bins_are_trivial<U: UnsignedLike>(bins: &[Bin<U>]) -> bool {
bins.len() == 0 || (bins.len() == 1 && bins[0].offset_bits == 0)
bins.is_empty() || (bins.len() == 1 && bins[0].offset_bits == 0)
}
16 changes: 13 additions & 3 deletions pco/src/body_decompressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::errors::PcoResult;
use crate::num_decompressor::NumDecompressor;
use crate::progress::Progress;
use crate::unsigned_src_dst::UnsignedDst;
use crate::{ChunkMetadata, delta_encoding, float_mult_utils};
use crate::{delta_encoding, float_mult_utils, ChunkMetadata};
use crate::{num_decompressor, Mode};

// BodyDecompressor wraps NumDecompressor and handles reconstruction from
Expand Down Expand Up @@ -40,13 +40,23 @@ fn join_streams<U: UnsignedLike>(mode: Mode<U>, dst: UnsignedDst<U>) {
}

impl<T: NumberLike> BodyDecompressor<T> {
pub(crate) fn new(n: usize, compressed_body_size: usize, chunk_meta: &ChunkMetadata<T::Unsigned>, data_page_meta: DataPageMetadata<T::Unsigned>) -> PcoResult<Self> {
pub(crate) fn new(
n: usize,
compressed_body_size: usize,
chunk_meta: &ChunkMetadata<T::Unsigned>,
data_page_meta: DataPageMetadata<T::Unsigned>,
) -> PcoResult<Self> {
let delta_momentss = data_page_meta
.streams
.iter()
.map(|stream| stream.delta_moments.clone())
.collect();
let num_decompressor = num_decompressor::new(n, compressed_body_size, chunk_meta, data_page_meta)?;
let num_decompressor = num_decompressor::new(
n,
compressed_body_size,
chunk_meta,
data_page_meta,
)?;
Ok(Self {
// we don't store the whole ChunkMeta because it can get large due to bins
mode: chunk_meta.mode,
Expand Down
57 changes: 34 additions & 23 deletions pco/src/chunk_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl<U: UnsignedLike> ChunkStreamMetadata<U> {

write_bins(&self.bins, mode, self.ans_size_log, writer);
}

}

#[derive(Clone, Debug)]
Expand All @@ -57,14 +56,18 @@ pub struct DataPageStreamMetadata<U: UnsignedLike> {
}

impl<U: UnsignedLike> DataPageStreamMetadata<U> {
pub fn write_to(&self, ans_size_log: Bitlen, writer: &mut BitWriter) {
self.delta_moments.write_to(writer);

// write the final ANS state, moving it down the range [0, table_size)
writer.write_usize(self.ans_final_state - (1 << ans_size_log), ans_size_log);
}

pub fn parse_from(reader: &mut BitReader, delta_order: usize, ans_size_log: Bitlen) -> PcoResult<Self> {
// pub fn write_to(&self, ans_size_log: Bitlen, writer: &mut BitWriter) {
// self.delta_moments.write_to(writer);
//
// // write the final ANS state, moving it down the range [0, table_size)
// writer.write_usize(self.ans_final_state - (1 << ans_size_log), ans_size_log);
// }

pub fn parse_from(
reader: &mut BitReader,
delta_order: usize,
ans_size_log: Bitlen,
) -> PcoResult<Self> {
let delta_moments = DeltaMoments::parse_from(reader, delta_order)?;
let ans_final_state = (1 << ans_size_log) + reader.read_usize(ans_size_log)?;
Ok(Self {
Expand Down Expand Up @@ -115,23 +118,25 @@ pub struct DataPageMetadata<U: UnsignedLike> {
}

impl<U: UnsignedLike> DataPageMetadata<U> {
pub fn write_to(&self, chunk_meta: &ChunkMetadata<U>, writer: &mut BitWriter) {
for (stream_idx, stream_meta) in chunk_meta.streams.iter().enumerate() {
self.streams[stream_idx].write_to(stream_meta.ans_size_log, writer);
}
writer.finish_byte();
}
// pub fn write_to(&self, chunk_meta: &ChunkMetadata<U>, writer: &mut BitWriter) {
// for (stream_idx, stream_meta) in chunk_meta.streams.iter().enumerate() {
// self.streams[stream_idx].write_to(stream_meta.ans_size_log, writer);
// }
// writer.finish_byte();
// }

pub fn parse_from(reader: &mut BitReader, chunk_meta: &ChunkMetadata<U>) -> PcoResult<Self> {
let mut streams = Vec::with_capacity(chunk_meta.streams.len());
for (stream_idx, stream_meta) in chunk_meta.streams.iter().enumerate() {
streams.push(DataPageStreamMetadata::parse_from(reader, chunk_meta.stream_delta_order(stream_idx), stream_meta.ans_size_log)?);
streams.push(DataPageStreamMetadata::parse_from(
reader,
chunk_meta.stream_delta_order(stream_idx),
stream_meta.ans_size_log,
)?);
}
reader.drain_empty_byte("non-zero bits at end of data page metadata")?;

Ok(Self {
streams
})
Ok(Self { streams })
}
}

Expand Down Expand Up @@ -210,7 +215,12 @@ fn write_bins<U: UnsignedLike>(
}

impl<U: UnsignedLike> ChunkMetadata<U> {
pub(crate) fn new(n: usize, mode: Mode<U>, delta_encoding_order: usize, streams: Vec<ChunkStreamMetadata<U>>) -> Self {
pub(crate) fn new(
n: usize,
mode: Mode<U>,
delta_encoding_order: usize,
streams: Vec<ChunkStreamMetadata<U>>,
) -> Self {
ChunkMetadata {
n,
compressed_body_size: 0,
Expand Down Expand Up @@ -317,7 +327,7 @@ impl<U: UnsignedLike> ChunkMetadata<U> {
let needs_gcd = gcd::use_gcd_arithmetic(primary_bins);
(needs_gcd, 1)
}
},
}
Mode::FloatMult(_) => {
let n_streams = if bin::bins_are_trivial(&self.streams[1].bins) {
if bin::bins_are_trivial(primary_bins) {
Expand All @@ -334,7 +344,9 @@ impl<U: UnsignedLike> ChunkMetadata<U> {
}

pub(crate) fn stream_delta_order(&self, stream_idx: usize) -> usize {
self.mode.stream_delta_order(stream_idx, self.delta_encoding_order)
self
.mode
.stream_delta_order(stream_idx, self.delta_encoding_order)
}
}

Expand All @@ -345,4 +357,3 @@ pub enum DataPagingSpec {
SinglePage,
ExactPageSizes(Vec<usize>),
}

4 changes: 1 addition & 3 deletions pco/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ impl Flags {
}

pub(crate) fn from_config(_config: &CompressorConfig, use_wrapped_mode: bool) -> Self {
Flags {
use_wrapped_mode,
}
Flags { use_wrapped_mode }
}
}
1 change: 0 additions & 1 deletion pco/src/modes/mode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt::Debug;
use crate::Bin;

use crate::bin::{BinCompressionInfo, BinDecompressionInfo};
use crate::bit_reader::BitReader;
Expand Down
47 changes: 26 additions & 21 deletions pco/src/num_decompressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ use std::mem::MaybeUninit;
use crate::bin::BinDecompressionInfo;
use crate::bit_reader::BitReader;
use crate::chunk_metadata::DataPageMetadata;
use crate::constants::{Bitlen, DECOMPRESS_UNCHECKED_THRESHOLD, MAX_DELTA_ENCODING_ORDER, MAX_N_STREAMS};
use crate::constants::{
Bitlen, DECOMPRESS_UNCHECKED_THRESHOLD, MAX_DELTA_ENCODING_ORDER, MAX_N_STREAMS,
};
use crate::data_types::UnsignedLike;
use crate::errors::{ErrorKind, PcoError, PcoResult};
use crate::modes::classic::ClassicMode;
use crate::modes::gcd::GcdMode;
use crate::modes::{ConstMode, gcd};
use crate::modes::ConstMode;
use crate::progress::Progress;
use crate::unsigned_src_dst::UnsignedDst;
use crate::{ans, ChunkMetadata, Mode};
use crate::{ans, ChunkMetadata};

#[derive(Clone, Debug)]
pub struct State<const STREAMS: usize> {
Expand Down Expand Up @@ -107,6 +109,7 @@ struct NumDecompressorInputs<'a, U: UnsignedLike> {
initial_values_required: [Option<U>; MAX_N_STREAMS],
}

#[allow(clippy::needless_range_loop)]
pub fn new<U: UnsignedLike>(
n: usize,
compressed_body_size: usize,
Expand All @@ -130,7 +133,9 @@ pub fn new<U: UnsignedLike>(

let mut initial_values_required = [None; MAX_N_STREAMS];
for stream_idx in n_streams..MAX_N_STREAMS {
initial_values_required[stream_idx] = chunk_meta.streams.get(stream_idx)
initial_values_required[stream_idx] = chunk_meta
.streams
.get(stream_idx)
.and_then(|stream_meta| stream_meta.bins.get(0))
.map(|only_bin| only_bin.lower);
}
Expand All @@ -144,18 +149,13 @@ pub fn new<U: UnsignedLike>(
};

let res: Box<dyn NumDecompressor<U>> = match (needs_gcd, n_streams) {
(false, 0) => Box::new(
NumDecompressorImpl::<U, ClassicMode, 0>::new(inputs)?
),
(false, 1) => Box::new(
NumDecompressorImpl::<U, ClassicMode, 1>::new(inputs)?
),
(true, 1) => Box::new(NumDecompressorImpl::<U, GcdMode, 1>::new(inputs)?
),
(false, 2) => Box::new(
NumDecompressorImpl::<U, ClassicMode, 2>::new(inputs)?
),
_ => panic!("unknown decompression implementation; should be unreachable")
(false, 0) => Box::new(NumDecompressorImpl::<U, ClassicMode, 0>::new(inputs)?),
(false, 1) => Box::new(NumDecompressorImpl::<U, ClassicMode, 1>::new(inputs)?),
(true, 1) => Box::new(NumDecompressorImpl::<U, GcdMode, 1>::new(
inputs,
)?),
(false, 2) => Box::new(NumDecompressorImpl::<U, ClassicMode, 2>::new(inputs)?),
_ => panic!("unknown decompression implementation; should be unreachable"),
};

Ok(res)
Expand Down Expand Up @@ -215,9 +215,7 @@ impl<U: UnsignedLike, M: ConstMode<U>, const STREAMS: usize> NumDecompressor<U>
}

impl<U: UnsignedLike, M: ConstMode<U>, const STREAMS: usize> NumDecompressorImpl<U, M, STREAMS> {
fn new(
inputs: NumDecompressorInputs<U>,
) -> PcoResult<Self> {
fn new(inputs: NumDecompressorInputs<U>) -> PcoResult<Self> {
let NumDecompressorInputs {
n,
compressed_body_size,
Expand All @@ -229,7 +227,11 @@ impl<U: UnsignedLike, M: ConstMode<U>, const STREAMS: usize> NumDecompressorImpl
let mut decoders: [MaybeUninit<ans::Decoder>; STREAMS] =
unsafe { MaybeUninit::uninit().assume_init() };

let delta_orders = data_page_meta.streams.iter().map(|stream| stream.delta_moments.order()).collect::<Vec<_>>();
let delta_orders = data_page_meta
.streams
.iter()
.map(|stream| stream.delta_moments.order())
.collect::<Vec<_>>();
for stream_idx in 0..STREAMS {
let chunk_stream = &chunk_meta.streams[stream_idx];
let page_stream = &data_page_meta.streams[stream_idx];
Expand All @@ -242,7 +244,10 @@ impl<U: UnsignedLike, M: ConstMode<U>, const STREAMS: usize> NumDecompressorImpl
)));
}

decoders[stream_idx].write(ans::Decoder::from_stream_meta(chunk_stream, page_stream.ans_final_state)?);
decoders[stream_idx].write(ans::Decoder::from_stream_meta(
chunk_stream,
page_stream.ans_final_state,
)?);
}

let stream_configs = core::array::from_fn(|stream_idx| {
Expand Down
Loading

0 comments on commit da24596

Please sign in to comment.