optimize SHORT_COMMIT to bytes

This commit is contained in:
Simon Gardling
2022-05-20 12:44:18 -04:00
parent 276d1f3f20
commit 2ba11008b5
2 changed files with 11 additions and 14 deletions

View File

@@ -155,7 +155,7 @@ impl MathApp {
assume(!cached_data.is_empty());
}
if commit == build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>().as_slice() {
if commit == const { unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) } } {
tracing::info!("Reading decompression cache. Bytes: {}", cached_data.len());
return Some(cached_data.to_vec());
} else {
@@ -171,7 +171,7 @@ impl MathApp {
}
tracing::info!("Setting decompression cache");
let commit: [u8; crate::misc::HASH_LENGTH] = unsafe { build::SHORT_COMMIT.as_bytes().try_into().unwrap_unchecked() };
let commit: crate::misc::HashBytes = const { unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) } };
let saved_data = &crate::misc::hashed_storage_create(commit, data);
tracing::info!("Bytes: {}", saved_data.len());
get_localstorage().set_item(DATA_NAME, saved_data).expect("failed to set local storage cache");
@@ -193,7 +193,7 @@ impl MathApp {
assume(!func_data.is_empty());
}
if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } {
if commit == const { unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } } {
tracing::info!("Reading previous function data");
let function_manager: FunctionManager = bincode::deserialize(&func_data).ok()?;
return Some(function_manager);
@@ -209,7 +209,9 @@ impl MathApp {
let mut data = Vec::new();
let _ = unsafe {
ruzstd::StreamingDecoder::new(
&mut include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice(),
&mut const {
include_bytes!(concat!(env!("OUT_DIR"), "/compressed_data")).as_slice()
},
)
.unwrap_unchecked()
.read_to_end(&mut data)
@@ -623,16 +625,11 @@ impl App for MathApp {
.local_storage()
{
tracing::info!("Setting current functions");
let hash: crate::misc::HashBytes = unsafe {
std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT)
};
let saved_data = &crate::misc::hashed_storage_create(
unsafe {
build::SHORT_COMMIT
.chars()
.map(|c| c as u8)
.collect::<Vec<u8>>()
.as_slice()
.try_into()
.unwrap_unchecked()
},
hash,
bincode::serialize(&self.functions).unwrap().as_slice(),
);
// tracing::info!("Bytes: {}", saved_data.len());

View File

@@ -329,7 +329,7 @@ pub fn almost_variable(x: f64) -> Option<char> {
pub const HASH_LENGTH: usize = 8;
/// Represents bytes used to represent hash info
type HashBytes = [u8; HASH_LENGTH];
pub type HashBytes = [u8; HASH_LENGTH];
#[allow(dead_code)]
pub fn hashed_storage_create(hash: HashBytes, data: &[u8]) -> String {