-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce the size of the future returned by async get_with
and friend methods (v0.9)
#222
Conversation
e29e73e
to
ee0fab7
Compare
Change the type of the `init` future argument for internal `async fn`s from `Future<...>` to `Pin<&mut impl Future<...>>`.
ee0fab7
to
87d3679
Compare
To verify that this PR will reduce the future size, I ran the following program on Moka before and after applying the PR: Programcheck_future_sizes.rsuse std::convert::Infallible;
fn main() {
let cache = moka::future::Cache::new(1);
let get_fut = async {
let buf = [0u8; 1024];
async {}.await;
#[allow(clippy::drop_copy)]
drop(buf);
};
let get_fut_size = std::mem::size_of_val(&get_fut);
println!("get_fut size: {} bytes", get_fut_size);
let moka_fut = cache.get_with((), get_fut);
let moka_fut_size = std::mem::size_of_val(&moka_fut);
println!(
"moka_get_with_fut size: {} bytes ({:.2}x)",
moka_fut_size,
moka_fut_size as f64 / get_fut_size as f64
);
let opt_get_fut = async {
let buf = [0u8; 1024];
async {}.await;
#[allow(clippy::drop_copy)]
drop(buf);
Some(())
};
let opt_get_fut_size = std::mem::size_of_val(&opt_get_fut);
println!(
"opt_get_fut size: {} bytes",
opt_get_fut_size
);
let moka_fut = cache.optionally_get_with((), opt_get_fut);
let moka_fut_size = std::mem::size_of_val(&moka_fut);
println!(
"moka_opt_get_with_fut size: {} bytes ({:.2}x)",
moka_fut_size,
moka_fut_size as f64 / opt_get_fut_size as f64
);
let try_get_fut = async {
let buf = [0u8; 1024];
async {}.await;
#[allow(clippy::drop_copy)]
drop(buf);
Ok(()) as Result<(), Infallible>
};
let try_get_fut_size = std::mem::size_of_val(&try_get_fut);
println!("try_get_fut size: {} bytes", try_get_fut_size);
let moka_fut = cache.try_get_with((), try_get_fut);
let moka_fut_size = std::mem::size_of_val(&moka_fut);
println!(
"moka_try_get_with_fut size: {} bytes ({:.2}x)",
moka_fut_size,
moka_fut_size as f64 / try_get_fut_size as f64
);
} ResultsBefore applying this PR get_fut size: 1026 bytes
moka_get_with_fut size: 7688 bytes (7.49x)
opt_get_fut size: 1026 bytes
moka_opt_get_with_fut size: 7632 bytes (7.44x)
try_get_fut size: 1026 bytes
moka_try_get_with_fut size: 7632 bytes (7.44x) After applying this PR get_fut size: 1026 bytes
moka_get_with_fut size: 2632 bytes (2.57x)
opt_get_fut size: 1026 bytes
moka_opt_get_with_fut size: 2584 bytes (2.52x)
try_get_fut size: 1026 bytes
moka_try_get_with_fut size: 2584 bytes (2.52x) |
The above program covered only the following methods:
I modified the program and ran a couple of times to check the future sizes of the other methods:
I got similar outputs for them and verified that this PR reduces the future sizes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging.
This PR will mitigate #212 for upcoming Moka v0.9.7 release.
Changes
future::Cache
:rustc
's optimization issue, switched the internally using type for theinit
future fromimpl Future
to a referencePin<&mut impl Future>
.ValueInitializer:init_or_read(..., init, ...)
from the call path.Updated Methods:
future::Cache
:get_with(...)
get_with_if(...)
optionally_get_with(...)
try_get_with(...)
get_with_by_ref(...)
optionally_get_with_by_ref(...)
try_get_with_by_ref(...)