#![allow(missing_docs, clippy::use_debug)]
use std::{
fs::File,
io::Read,
path::Path,
};
use fsst::Compressor;
fn main() {
let args: Vec<_> = std::env::args().skip(1).collect();
let input_path = Path::new(&args[0]);
let mut string = String::new();
{
let mut f = File::open(input_path).unwrap();
f.read_to_string(&mut string).unwrap();
}
let uncompressed_size = string.len();
let lines: Vec<&[u8]> = string.lines().map(|line| line.as_bytes()).collect();
let start = std::time::Instant::now();
let compressor = Compressor::train(&lines);
let duration = std::time::Instant::now().duration_since(start);
println!("train took {}µs", duration.as_micros());
let mut compressed_size = 0;
let mut buffer = Vec::with_capacity(8 * 1024 * 1024);
let start = std::time::Instant::now();
for text in lines {
unsafe { compressor.compress_into(text, &mut buffer) };
compressed_size += buffer.len();
}
let duration = std::time::Instant::now().duration_since(start);
println!("compression took {}µs", duration.as_micros());
println!(
"compressed {} -> {} ({}%)",
uncompressed_size,
compressed_size,
100.0 * (compressed_size as f64) / (uncompressed_size as f64)
);
}