I am working on a Sha1 function in risingwave where I need to create a new Sha1 hasher every time, causing malloc overhead. I want to create a global thread-local hasher once at the start of the program and reset it after each function call. How can I achieve this in risingwave?
BanBuDu0
Asked on May 05, 2023
You can use the lazy_static
crate to create a global thread-local hasher in risingwave.
Here's an example of how you can achieve this:
use lazy_static::lazy_static;
use std::cell::RefCell;
use sha1::{Sha1, Digest};
lazy_static! {
static ref HASHER: RefCell<Sha1> = RefCell::new(Sha1::new());
}
pub fn sha1(data: &[u8]) -> Result<Box<[u8]>> {
let mut hasher = HASHER.borrow_mut();
// Use the hasher for processing data
let result = hasher.result();
hasher.reset(); // Reset the hasher for the next function call
Ok(result.into_boxed_slice())
}