forked from tailcallhq/tailcall
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dedupe operations that are cacheable (tailcallhq#3099)
Co-authored-by: Tushar Mathur <[email protected]>
- Loading branch information
1 parent
076260f
commit fb0974d
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::convert::Infallible; | ||
use std::num::NonZeroU64; | ||
|
||
use tailcall_valid::Valid; | ||
|
||
use crate::core::ir::model::IR; | ||
use crate::core::jit::OperationPlan; | ||
use crate::core::Transform; | ||
|
||
/// A transformer that sets the minimum cache TTL for the operation plan based | ||
/// on the IR. | ||
pub struct CheckCache<A>(std::marker::PhantomData<A>); | ||
impl<A> CheckCache<A> { | ||
pub fn new() -> Self { | ||
Self(std::marker::PhantomData) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn check_cache(ir: &IR) -> Option<NonZeroU64> { | ||
match ir { | ||
IR::IO(_) => None, | ||
IR::Cache(cache) => Some(cache.max_age), | ||
IR::Path(ir, _) => check_cache(ir), | ||
IR::Protect(ir) => check_cache(ir), | ||
IR::Pipe(ir, ir1) => match (check_cache(ir), check_cache(ir1)) { | ||
(Some(age1), Some(age2)) => Some(age1.min(age2)), | ||
_ => None, | ||
}, | ||
IR::Discriminate(_, ir) => check_cache(ir), | ||
IR::Entity(hash_map) => { | ||
let mut ttl = Some(NonZeroU64::MAX); | ||
for ir in hash_map.values() { | ||
ttl = std::cmp::min(ttl, check_cache(ir)); | ||
} | ||
ttl | ||
} | ||
IR::Dynamic(_) | IR::ContextPath(_) | IR::Map(_) | IR::Service(_) => None, | ||
} | ||
} | ||
|
||
impl<A> Transform for CheckCache<A> { | ||
type Value = OperationPlan<A>; | ||
type Error = Infallible; | ||
|
||
fn transform(&self, mut plan: Self::Value) -> Valid<Self::Value, Self::Error> { | ||
let mut ttl = Some(NonZeroU64::MAX); | ||
|
||
for field in plan.selection.iter() { | ||
if let Some(ir) = field.ir.as_ref() { | ||
ttl = std::cmp::min(ttl, check_cache(ir)); | ||
} | ||
} | ||
|
||
plan.min_cache_ttl = ttl; | ||
|
||
Valid::succeed(plan) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters