Skip to content

Commit c8502d2

Browse files
committed
Remove playground and debug printing
1 parent d3e0c06 commit c8502d2

File tree

5 files changed

+51
-216
lines changed

5 files changed

+51
-216
lines changed

macros/src/memoize.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub fn expand(item: &syn::Item) -> Result<proc_macro2::TokenStream> {
2020
/// Details about a function that should be memoized.
2121
struct Function {
2222
item: syn::ItemFn,
23-
name: syn::Ident,
2423
args: Vec<Argument>,
2524
output: syn::Type,
2625
}
@@ -55,12 +54,7 @@ fn prepare(function: &syn::ItemFn) -> Result<Function> {
5554
syn::ReturnType::Type(_, ty) => ty.as_ref().clone(),
5655
};
5756

58-
Ok(Function {
59-
item: function.clone(),
60-
name: function.sig.ident.clone(),
61-
args,
62-
output,
63-
})
57+
Ok(Function { item: function.clone(), args, output })
6458
}
6559

6660
/// Preprocess a function argument.
@@ -135,14 +129,12 @@ fn process(function: &Function) -> Result<TokenStream> {
135129

136130
// Adjust the function's body.
137131
let mut wrapped = function.item.clone();
138-
let name = function.name.to_string();
139132
let unique = quote! { __ComemoUnique };
140133

141134
wrapped.block = parse_quote! { {
142135
struct #unique;
143136
#(#bounds;)*
144137
::comemo::internal::memoized(
145-
#name,
146138
::core::any::TypeId::of::<#unique>(),
147139
::comemo::internal::Args(#arg_tuple),
148140
#closure,

macros/src/track.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub fn expand(item: &syn::Item) -> Result<TokenStream> {
4444

4545
/// Details about a method that should be tracked.
4646
struct Method {
47-
name: syn::Ident,
4847
vis: syn::Visibility,
4948
sig: syn::Signature,
5049
args: Vec<syn::Ident>,
@@ -168,7 +167,6 @@ fn prepare_method(vis: syn::Visibility, sig: &syn::Signature) -> Result<Method>
168167
}
169168

170169
Ok(Method {
171-
name: sig.ident.clone(),
172170
vis,
173171
sig: sig.clone(),
174172
args,
@@ -240,7 +238,7 @@ fn create(
240238

241239
/// Produce a constraint validation for a method.
242240
fn create_validation(method: &Method) -> TokenStream {
243-
let name = &method.name;
241+
let name = &method.sig.ident;
244242
let args = &method.args;
245243
let prepared = method.args.iter().zip(&method.kinds).map(|(arg, kind)| match kind {
246244
Kind::Normal => quote! { #arg.to_owned() },
@@ -256,7 +254,7 @@ fn create_validation(method: &Method) -> TokenStream {
256254
fn create_wrapper(method: &Method) -> TokenStream {
257255
let vis = &method.vis;
258256
let sig = &method.sig;
259-
let name = &method.name;
257+
let name = &method.sig.ident;
260258
let args = &method.args;
261259
quote! {
262260
#vis #sig {
@@ -273,7 +271,7 @@ fn create_wrapper(method: &Method) -> TokenStream {
273271

274272
/// Produce a constraint field for a method.
275273
fn create_constraint(method: &Method) -> TokenStream {
276-
let name = &method.name;
274+
let name = &method.sig.ident;
277275
let types = &method.types;
278276
if types.is_empty() {
279277
quote! { #name: ::comemo::internal::SoloConstraint, }
@@ -288,6 +286,6 @@ fn create_constraint(method: &Method) -> TokenStream {
288286

289287
/// Produce a join call for a method's constraint.
290288
fn create_join(method: &Method) -> TokenStream {
291-
let name = &method.name;
289+
let name = &method.sig.ident;
292290
quote! { self.#name.join(&inner.#name); }
293291
}

src/cache.rs

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,6 @@ thread_local! {
1414
static CACHE: RefCell<Cache> = RefCell::new(Cache::default());
1515
}
1616

17-
/// Execute a function or use a cached result for it.
18-
pub fn memoized<In, Out, F>(name: &'static str, id: TypeId, input: In, func: F) -> Out
19-
where
20-
In: Input,
21-
Out: Debug + Clone + 'static,
22-
F: for<'f> FnOnce(<In::Tracked as Family<'f>>::Out) -> Out,
23-
{
24-
CACHE.with(|cache| {
25-
// Compute the hash of the input's key part.
26-
let key = {
27-
let mut state = SipHasher::new();
28-
input.key(&mut state);
29-
let hash = state.finish128().as_u128();
30-
(id, hash)
31-
};
32-
33-
let mut hit = true;
34-
let mut borrowed = cache.borrow_mut();
35-
36-
// Check whether there is a cached entry.
37-
let output = match borrowed.lookup::<In, Out>(key, &input) {
38-
Some(output) => output,
39-
None => {
40-
hit = false;
41-
borrowed.depth += 1;
42-
drop(borrowed);
43-
44-
// Point all tracked parts of the input to these constraints.
45-
let constraint = In::Constraint::default();
46-
let (tracked, outer) = input.retrack(&constraint);
47-
48-
// Execute the function with the new constraints hooked in.
49-
let output = func(tracked);
50-
51-
// Add the new constraints to the previous outer ones.
52-
outer.join(&constraint);
53-
54-
// Insert the result into the cache.
55-
borrowed = cache.borrow_mut();
56-
borrowed.insert::<In, Out>(key, constraint, output.clone());
57-
borrowed.depth -= 1;
58-
59-
output
60-
}
61-
};
62-
63-
// Print details.
64-
let depth = borrowed.depth;
65-
let label = if hit { "[hit]" } else { "[miss]" };
66-
eprintln!("{depth} {name:<12} {label:<7} {output:?}");
67-
68-
output
69-
})
70-
}
71-
7217
/// Configure the caching behaviour.
7318
pub fn config(config: Config) {
7419
CACHE.with(|cache| cache.borrow_mut().config = config);
@@ -109,6 +54,50 @@ pub fn evict() {
10954
});
11055
}
11156

57+
/// Execute a function or use a cached result for it.
58+
pub fn memoized<In, Out, F>(id: TypeId, input: In, func: F) -> Out
59+
where
60+
In: Input,
61+
Out: Debug + Clone + 'static,
62+
F: for<'f> FnOnce(<In::Tracked as Family<'f>>::Out) -> Out,
63+
{
64+
CACHE.with(|cache| {
65+
// Compute the hash of the input's key part.
66+
let key = {
67+
let mut state = SipHasher::new();
68+
input.key(&mut state);
69+
let hash = state.finish128().as_u128();
70+
(id, hash)
71+
};
72+
73+
// Check if there is a cached output.
74+
let mut borrow = cache.borrow_mut();
75+
if let Some(output) = borrow.lookup::<In, Out>(key, &input) {
76+
return output;
77+
}
78+
79+
borrow.depth += 1;
80+
drop(borrow);
81+
82+
// Point all tracked parts of the input to these constraints.
83+
let constraint = In::Constraint::default();
84+
let (tracked, outer) = input.retrack(&constraint);
85+
86+
// Execute the function with the new constraints hooked in.
87+
let output = func(tracked);
88+
89+
// Add the new constraints to the previous outer ones.
90+
outer.join(&constraint);
91+
92+
// Insert the result into the cache.
93+
borrow = cache.borrow_mut();
94+
borrow.insert::<In, Out>(key, constraint, output.clone());
95+
borrow.depth -= 1;
96+
97+
output
98+
})
99+
}
100+
112101
/// The global cache.
113102
#[derive(Default)]
114103
struct Cache {
@@ -189,6 +178,7 @@ impl Entry {
189178
{
190179
let Constrained::<In::Constraint, Out> { constraint, output } =
191180
self.constrained.downcast_ref().expect("wrong entry type");
181+
192182
input.valid(constraint).then(|| {
193183
self.age = 0;
194184
output.clone()

src/main.rs

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/prehashed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use siphasher::sip128::{Hasher128, SipHasher};
1515
/// # `Hash` and `Eq`
1616
/// When implementing both `Hash` and `Eq`, the following property [must
1717
/// hold][property]:
18-
/// ```
18+
/// ```text
1919
/// a == b -> hash(a) == hash(b)
2020
/// ```
2121
/// The inverse implication does not follow from this immediately. However,

0 commit comments

Comments
 (0)