Skip to content

Commit

Permalink
Cache now correctly hashed the data
Browse files Browse the repository at this point in the history
The cache previously used the subject ID as the key. However, the data is prone to change, and it is now checked for updates.
  • Loading branch information
mhovd committed Jun 3, 2024
1 parent 733ae43 commit 6faff2e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pharmsol"
version = "0.1.13"
version = "0.1.14"
edition = "2021"
authors = [
"Julián D. Otálvaro <[email protected]>",
Expand Down
34 changes: 34 additions & 0 deletions src/data/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::data::*;
use serde::Deserialize;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::{collections::HashMap, fmt};

/// [Data] is a collection of [Subject]s, which are collections of [Occasion]s, which are collections of [Event]s
Expand Down Expand Up @@ -133,6 +134,39 @@ impl Subject {
pub fn id(&self) -> &String {
&self.id
}

// Hasher for subject
pub fn hash(&self) -> u64 {
let mut hasher = DefaultHasher::new();

// Hash the subject ID
self.id().hash(&mut hasher);

// Hash each occasion
for occasion in &self.occasions() {
occasion.index().hash(&mut hasher);
for event in &occasion.events {
match event {
Event::Observation(observation) => {
observation.time().to_bits().hash(&mut hasher);
observation.value().to_bits().hash(&mut hasher);
observation.outeq().hash(&mut hasher);
}
Event::Infusion(infusion) => {
infusion.time().to_bits().hash(&mut hasher);
infusion.duration().to_bits().hash(&mut hasher);
infusion.amount().to_bits().hash(&mut hasher);
}
Event::Bolus(bolus) => {
bolus.time().to_bits().hash(&mut hasher);
bolus.amount().to_bits().hash(&mut hasher);
}
}
}
}

hasher.finish()
}
}

/// An [Occasion] is a collection of events, for a given [Subject], that are from a specific occasion
Expand Down
20 changes: 15 additions & 5 deletions src/simulator/cache.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::data::Subject;
use crate::simulator::likelihood::SubjectPredictions;
use dashmap::DashMap;
use lazy_static::lazy_static;
Expand All @@ -8,12 +9,21 @@ const CACHE_SIZE: usize = 10000;

#[derive(Clone, Debug, PartialEq, Hash)]
struct CacheKey {
subject: String,
subject: SubjectHash,
support_point: SupportPointHash,
}

impl Eq for CacheKey {}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct SubjectHash(u64);

impl SubjectHash {
fn new(subject: &Subject) -> Self {
SubjectHash(subject.hash())
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct SupportPointHash(u64);

Expand All @@ -33,9 +43,9 @@ lazy_static! {
static ref CACHE: DashMap<CacheKey, SubjectPredictions> = DashMap::with_capacity(CACHE_SIZE);
}

pub(crate) fn get_entry(subject: &str, support_point: &Vec<f64>) -> Option<SubjectPredictions> {
pub(crate) fn get_entry(subject: &Subject, support_point: &Vec<f64>) -> Option<SubjectPredictions> {
let cache_key = CacheKey {
subject: subject.to_owned(),
subject: SubjectHash::new(subject),
support_point: SupportPointHash::new(support_point),
};

Expand All @@ -46,12 +56,12 @@ pub(crate) fn get_entry(subject: &str, support_point: &Vec<f64>) -> Option<Subje
}

pub(crate) fn insert_entry(
subject: &str,
subject: &Subject,
support_point: &Vec<f64>,
predictions: SubjectPredictions,
) {
let cache_key = CacheKey {
subject: subject.to_owned(),
subject: SubjectHash::new(subject),
support_point: SupportPointHash::new(support_point),
};

Expand Down
4 changes: 2 additions & 2 deletions src/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Equation {
let mut yout = vec![];
for occasion in subject.occasions() {
// Check for a cache entry
let pred = get_entry(subject.id(), support_point);
let pred = get_entry(subject, support_point);
if let Some(pred) = pred {
return pred;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Equation {
}
// Insert the cache entry
let pred: SubjectPredictions = yout.into();
insert_entry(subject.id(), support_point, pred.clone());
insert_entry(subject, support_point, pred.clone());
pred
}
#[inline(always)]
Expand Down

0 comments on commit 6faff2e

Please sign in to comment.