Skip to content

Commit a9fc87d

Browse files
committed
*: remove unnecessary async blocks to save memory
This commit favors FutureExt::map over async blocks to mitigate the issue of async block doubled memory usage. Through the sysbench oltp_read_only test, it was observed that this adjustment resulted in approximately 26% reduction in memory usage. See: rust-lang/rust#59087 Signed-off-by: Neil Shen <[email protected]>
1 parent a6b4f24 commit a9fc87d

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

components/tikv_util/src/yatp_pool/future_pool.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{
1313

1414
use fail::fail_point;
1515
use futures::channel::oneshot::{self, Canceled};
16+
use futures_util::future::FutureExt;
1617
use prometheus::{IntCounter, IntGauge};
1718
use tracker::TrackedFuture;
1819
use yatp::{queue::Extras, task::future};
@@ -179,11 +180,13 @@ impl PoolInner {
179180

180181
metrics_running_task_count.inc();
181182

182-
let f = async move {
183-
let _ = future.await;
183+
// NB: Prefer FutureExt::map to async block, because an async block
184+
// doubles memory usage.
185+
// See https://github.com/rust-lang/rust/issues/59087
186+
let f = future.map(move |_| {
184187
metrics_handled_task_count.inc();
185188
metrics_running_task_count.dec();
186-
};
189+
});
187190

188191
if let Some(extras) = extras {
189192
self.pool.spawn(future::TaskCell::new(f, extras));
@@ -208,12 +211,14 @@ impl PoolInner {
208211

209212
let (tx, rx) = oneshot::channel();
210213
metrics_running_task_count.inc();
211-
self.pool.spawn(async move {
212-
let res = future.await;
214+
// NB: Prefer FutureExt::map to async block, because an async block
215+
// doubles memory usage.
216+
// See https://github.com/rust-lang/rust/issues/59087
217+
self.pool.spawn(future.map(move |res| {
213218
metrics_handled_task_count.inc();
214219
metrics_running_task_count.dec();
215220
let _ = tx.send(res);
216-
});
221+
}));
217222
Ok(rx)
218223
}
219224
}

src/read_pool.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use std::{
1212
};
1313

1414
use file_system::{set_io_type, IoType};
15-
use futures::{channel::oneshot, future::TryFutureExt};
15+
use futures::{
16+
channel::oneshot,
17+
future::{FutureExt, TryFutureExt},
18+
};
1619
use kvproto::{errorpb, kvrpcpb::CommandPri};
1720
use online_config::{ConfigChange, ConfigManager, ConfigValue, Result as CfgResult};
1821
use prometheus::{core::Metric, Histogram, IntCounter, IntGauge};
@@ -171,10 +174,9 @@ impl ReadPoolHandle {
171174
TaskCell::new(
172175
TrackedFuture::new(with_resource_limiter(
173176
ControlledFuture::new(
174-
async move {
175-
f.await;
177+
f.map(move |_| {
176178
running_tasks.dec();
177-
},
179+
}),
178180
resource_ctl.clone(),
179181
group_name,
180182
),
@@ -184,10 +186,9 @@ impl ReadPoolHandle {
184186
)
185187
} else {
186188
TaskCell::new(
187-
TrackedFuture::new(async move {
188-
f.await;
189+
TrackedFuture::new(f.map(move |_| {
189190
running_tasks.dec();
190-
}),
191+
})),
191192
extras,
192193
)
193194
};
@@ -211,10 +212,9 @@ impl ReadPoolHandle {
211212
{
212213
let (tx, rx) = oneshot::channel::<T>();
213214
let res = self.spawn(
214-
async move {
215-
let res = f.await;
215+
f.map(move |res| {
216216
let _ = tx.send(res);
217-
},
217+
}),
218218
priority,
219219
task_id,
220220
metadata,

src/storage/txn/sched_pool.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88

99
use collections::HashMap;
1010
use file_system::{set_io_type, IoType};
11+
use futures::future::FutureExt;
1112
use kvproto::{kvrpcpb::CommandPri, pdpb::QueryKind};
1213
use pd_client::{Feature, FeatureGate};
1314
use prometheus::local::*;
@@ -131,13 +132,7 @@ impl PriorityQueue {
131132
extras.set_metadata(metadata.to_vec());
132133
self.worker_pool.spawn_with_extras(
133134
with_resource_limiter(
134-
ControlledFuture::new(
135-
async move {
136-
f.await;
137-
},
138-
self.resource_ctl.clone(),
139-
group_name,
140-
),
135+
ControlledFuture::new(f, self.resource_ctl.clone(), group_name),
141136
resource_limiter,
142137
),
143138
extras,

0 commit comments

Comments
 (0)