Skip to content

Commit 998cea7

Browse files
committed
A
1 parent 3272634 commit 998cea7

File tree

11 files changed

+1023
-631
lines changed

11 files changed

+1023
-631
lines changed

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ comemo-macros = { version = "0.4.0", path = "macros" }
1717
once_cell = "1.18"
1818
parking_lot = "0.12"
1919
proc-macro2 = "1"
20+
quickcheck = "1"
21+
quickcheck_macros = "1"
2022
quote = "1"
2123
serial_test = "3"
2224
siphasher = "1"
25+
slab = "0.4"
2326
syn = { version = "2", features = ["full"] }
2427

2528
[package]
@@ -43,8 +46,12 @@ testing = []
4346
comemo-macros = { workspace = true, optional = true }
4447
parking_lot = { workspace = true }
4548
siphasher = { workspace = true }
49+
bumpalo = "*"
50+
slab = { workspace = true }
4651

4752
[dev-dependencies]
53+
quickcheck = { workspace = true }
54+
quickcheck_macros = { workspace = true }
4855
serial_test = { workspace = true }
4956

5057
[[test]]

macros/src/memoize.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn process(function: &Function) -> Result<TokenStream> {
134134
// Construct the inner closure.
135135
let output = &function.output;
136136
let body = &function.item.block;
137-
let closure = quote! { |::comemo::internal::Args(#param_tuple)| -> #output #body };
137+
let closure = quote! { |::comemo::internal::Multi(#param_tuple)| -> #output #body };
138138

139139
// Adjust the function's body.
140140
let mut wrapped = function.item.clone();
@@ -148,7 +148,7 @@ fn process(function: &Function) -> Result<TokenStream> {
148148

149149
wrapped.block = parse_quote! { {
150150
static __CACHE: ::comemo::internal::Cache<
151-
<::comemo::internal::Args<#arg_ty_tuple> as ::comemo::internal::Input>::Constraint,
151+
<::comemo::internal::Multi<#arg_ty_tuple> as ::comemo::internal::Input>::Call,
152152
#output,
153153
> = ::comemo::internal::Cache::new(|| {
154154
::comemo::internal::register_evictor(|max_age| __CACHE.evict(max_age));
@@ -158,7 +158,8 @@ fn process(function: &Function) -> Result<TokenStream> {
158158
#(#bounds;)*
159159

160160
::comemo::internal::memoized(
161-
::comemo::internal::Args(#arg_tuple),
161+
::comemo::internal::Multi(#arg_tuple),
162+
&::core::default::Default::default(),
162163
&::core::default::Default::default(),
163164
&__CACHE,
164165
#enabled,

macros/src/track.rs

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -245,37 +245,8 @@ fn create(
245245
impl_params_t.params.push(t.clone());
246246
type_params_t.params.push(t.clone());
247247

248-
// Prepare validations.
249248
let prefix = trait_.as_ref().map(|name| quote! { #name for });
250-
let validations: Vec<_> = methods.iter().map(create_validation).collect();
251-
let validate = if !methods.is_empty() {
252-
quote! {
253-
let mut this = #maybe_cloned;
254-
constraint.validate(|call| match &call.0 { #(#validations,)* })
255-
}
256-
} else {
257-
quote! { true }
258-
};
259-
let validate_with_id = if !methods.is_empty() {
260-
quote! {
261-
let mut this = #maybe_cloned;
262-
constraint.validate_with_id(
263-
|call| match &call.0 { #(#validations,)* },
264-
id,
265-
)
266-
}
267-
} else {
268-
quote! { true }
269-
};
270-
271-
// Prepare replying.
272-
let immutable = methods.iter().all(|m| !m.mutable);
273-
let replays = methods.iter().map(create_replay);
274-
let replay = (!immutable).then(|| {
275-
quote! {
276-
constraint.replay(|call| match &call.0 { #(#replays,)* });
277-
}
278-
});
249+
let calls: Vec<_> = methods.iter().map(create_call).collect();
279250

280251
// Prepare variants and wrapper methods.
281252
let wrapper_methods = methods
@@ -284,32 +255,20 @@ fn create(
284255
.map(|m| create_wrapper(m, false));
285256
let wrapper_methods_mut = methods.iter().map(|m| create_wrapper(m, true));
286257

287-
let constraint = if immutable {
288-
quote! { ImmutableConstraint }
289-
} else {
290-
quote! { MutableConstraint }
291-
};
292-
293258
Ok(quote! {
294-
impl #impl_params ::comemo::Track for #ty #where_clause {}
295-
296-
impl #impl_params ::comemo::Validate for #ty #where_clause {
297-
type Constraint = ::comemo::internal::#constraint<__ComemoCall>;
259+
impl #impl_params ::comemo::Track for #ty #where_clause {
260+
type Call = __ComemoCall;
298261

299262
#[inline]
300-
fn validate(&self, constraint: &Self::Constraint) -> bool {
301-
#validate
263+
fn call(&self, call: Self::Call) -> u128 {
264+
let mut this = #maybe_cloned;
265+
match call.0 { #(#calls,)* }
302266
}
303267

304268
#[inline]
305-
fn validate_with_id(&self, constraint: &Self::Constraint, id: usize) -> bool {
306-
#validate_with_id
307-
}
308-
309-
#[inline]
310-
#[allow(unused_variables)]
311-
fn replay(&mut self, constraint: &Self::Constraint) {
312-
#replay
269+
fn call_mut(&mut self, call: Self::Call) -> u128 {
270+
let mut this = self;
271+
match call.0 { #(#calls,)* }
313272
}
314273
}
315274

@@ -371,35 +330,19 @@ fn create_variant(method: &Method) -> TokenStream {
371330
}
372331

373332
/// Produce a constraint validation for a method.
374-
fn create_validation(method: &Method) -> TokenStream {
333+
fn create_call(method: &Method) -> TokenStream {
375334
let name = &method.sig.ident;
376335
let args = &method.args;
377336
let prepared = method.args.iter().zip(&method.kinds).map(|(arg, kind)| match kind {
378337
Kind::Normal => quote! { #arg.to_owned() },
379338
Kind::Reference => quote! { #arg },
380339
});
381340
quote! {
382-
__ComemoVariant::#name(#(#args),*)
341+
__ComemoVariant::#name(#(ref #args),*)
383342
=> ::comemo::internal::hash(&this.#name(#(#prepared),*))
384343
}
385344
}
386345

387-
/// Produce a constraint validation for a method.
388-
fn create_replay(method: &Method) -> TokenStream {
389-
let name = &method.sig.ident;
390-
let args = &method.args;
391-
let prepared = method.args.iter().zip(&method.kinds).map(|(arg, kind)| match kind {
392-
Kind::Normal => quote! { #arg.to_owned() },
393-
Kind::Reference => quote! { #arg },
394-
});
395-
let body = method.mutable.then(|| {
396-
quote! {
397-
self.#name(#(#prepared),*);
398-
}
399-
});
400-
quote! { __ComemoVariant::#name(#(#args),*) => { #body } }
401-
}
402-
403346
/// Produce a wrapped surface method.
404347
fn create_wrapper(method: &Method, tracked_mut: bool) -> TokenStream {
405348
let name = &method.sig.ident;
@@ -417,16 +360,18 @@ fn create_wrapper(method: &Method, tracked_mut: bool) -> TokenStream {
417360
#[track_caller]
418361
#[inline]
419362
#vis #sig {
420-
let __comemo_variant = __ComemoVariant::#name(#(#args.to_owned()),*);
421-
let (__comemo_value, __comemo_constraint) = ::comemo::internal::#to_parts;
422-
let output = __comemo_value.#name(#(#args,)*);
423-
if let Some(constraint) = __comemo_constraint {
424-
constraint.push(
363+
let (__comemo_value, __comemo_sink) = ::comemo::internal::#to_parts;
364+
if let Some(__comemo_sink) = __comemo_sink {
365+
let __comemo_variant = __ComemoVariant::#name(#(#args.to_owned()),*);
366+
let output = __comemo_value.#name(#(#args,)*);
367+
__comemo_sink(
425368
__ComemoCall(__comemo_variant),
426369
::comemo::internal::hash(&output),
427370
);
371+
output
372+
} else {
373+
__comemo_value.#name(#(#args,)*)
428374
}
429-
output
430375
}
431376
}
432377
}

0 commit comments

Comments
 (0)