Skip to content

Commit 53f5c14

Browse files
committed
reduce async call depth
Unfortunately Rust doesn't inline async functions, even for tail calls. Each level of indirection adds to the state size and that can scale with the amount of calls into the library. Remove some avoidable uses of async to help memory usage somewhat until Rust fixes these issues. rust-lang/rust#69826
1 parent e955dc7 commit 53f5c14

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

src/decode.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -254,44 +254,44 @@ impl<R: AsyncRead + Unpin> MsgPackFuture<R> {
254254
}
255255
}
256256

257-
async fn read_u8(&mut self) -> IoResult<u8> {
258-
self.read_1().await
257+
fn read_u8(&mut self) -> impl Future<Output = IoResult<u8>> + '_ {
258+
self.read_1()
259259
}
260260

261-
async fn read_u16(&mut self) -> IoResult<u16> {
262-
Ok(BigEndian::read_u16(&self.read_2().await?))
261+
fn read_u16(&mut self) -> impl Future<Output = IoResult<u16>> + '_ {
262+
self.read_2().map_ok(|val| BigEndian::read_u16(&val))
263263
}
264264

265-
async fn read_u32(&mut self) -> IoResult<u32> {
266-
Ok(BigEndian::read_u32(&self.read_4().await?))
265+
fn read_u32(&mut self) -> impl Future<Output = IoResult<u32>> + '_ {
266+
self.read_4().map_ok(|val| BigEndian::read_u32(&val))
267267
}
268268

269-
async fn read_u64(&mut self) -> IoResult<u64> {
270-
Ok(BigEndian::read_u64(&self.read_8().await?))
269+
fn read_u64(&mut self) -> impl Future<Output = IoResult<u64>> + '_ {
270+
self.read_8().map_ok(|val| BigEndian::read_u64(&val))
271271
}
272272

273-
async fn read_i8(&mut self) -> IoResult<i8> {
274-
self.read_1().await.map(|x| x as i8)
273+
fn read_i8(&mut self) -> impl Future<Output = IoResult<i8>> + '_ {
274+
self.read_1().map_ok(|val| val as i8)
275275
}
276276

277-
async fn read_i16(&mut self) -> IoResult<i16> {
278-
Ok(BigEndian::read_i16(&self.read_2().await?))
277+
fn read_i16(&mut self) -> impl Future<Output = IoResult<i16>> + '_ {
278+
self.read_2().map_ok(|val| BigEndian::read_i16(&val))
279279
}
280280

281-
async fn read_i32(&mut self) -> IoResult<i32> {
282-
Ok(BigEndian::read_i32(&self.read_4().await?))
281+
fn read_i32(&mut self) -> impl Future<Output = IoResult<i32>> + '_ {
282+
self.read_4().map_ok(|val| BigEndian::read_i32(&val))
283283
}
284284

285-
async fn read_i64(&mut self) -> IoResult<i64> {
286-
Ok(BigEndian::read_i64(&self.read_8().await?))
285+
fn read_i64(&mut self) -> impl Future<Output = IoResult<i64>> + '_ {
286+
self.read_8().map_ok(|val| BigEndian::read_i64(&val))
287287
}
288288

289-
async fn read_f32(&mut self) -> IoResult<f32> {
290-
Ok(BigEndian::read_f32(&self.read_4().await?))
289+
fn read_f32(&mut self) -> impl Future<Output = IoResult<f32>> + '_ {
290+
self.read_4().map_ok(|val| BigEndian::read_f32(&val))
291291
}
292292

293-
async fn read_f64(&mut self) -> IoResult<f64> {
294-
Ok(BigEndian::read_f64(&self.read_8().await?))
293+
fn read_f64(&mut self) -> impl Future<Output = IoResult<f64>> + '_ {
294+
self.read_8().map_ok(|val| BigEndian::read_f64(&val))
295295
}
296296

297297
pub async fn skip(self) -> IoResult<R>

src/encode.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -282,54 +282,54 @@ impl<W: AsyncWrite + Unpin> MsgPackWriter<W> {
282282
self.writer.write_all(&val).await
283283
}
284284

285-
async fn write_u8(&mut self, val: u8) -> IoResult<()> {
285+
fn write_u8(&mut self, val: u8) -> impl Future<Output = IoResult<()>> + '_ {
286286
let buf = [val];
287-
self.write_1(buf).await
287+
self.write_1(buf)
288288
}
289289

290-
async fn write_u16(&mut self, val: u16) -> IoResult<()> {
290+
fn write_u16(&mut self, val: u16) -> impl Future<Output = IoResult<()>> + '_ {
291291
let mut buf = [0u8; 2];
292292
BigEndian::write_u16(&mut buf, val);
293-
self.write_2(buf).await
293+
self.write_2(buf)
294294
}
295295

296-
async fn write_u32(&mut self, val: u32) -> IoResult<()> {
296+
fn write_u32(&mut self, val: u32) -> impl Future<Output = IoResult<()>> + '_ {
297297
let mut buf = [0u8; 4];
298298
BigEndian::write_u32(&mut buf, val);
299-
self.write_4(buf).await
299+
self.write_4(buf)
300300
}
301301

302-
async fn write_u64(&mut self, val: u64) -> IoResult<()> {
302+
fn write_u64(&mut self, val: u64) -> impl Future<Output = IoResult<()>> + '_ {
303303
let mut buf = [0u8; 8];
304304
BigEndian::write_u64(&mut buf, val);
305-
self.write_8(buf).await
305+
self.write_8(buf)
306306
}
307307

308-
async fn write_i8(&mut self, val: i8) -> IoResult<()> {
308+
fn write_i8(&mut self, val: i8) -> impl Future<Output = IoResult<()>> + '_ {
309309
let buf = [val as u8];
310-
self.write_1(buf).await
310+
self.write_1(buf)
311311
}
312312

313-
async fn write_i16(&mut self, val: i16) -> IoResult<()> {
313+
fn write_i16(&mut self, val: i16) -> impl Future<Output = IoResult<()>> + '_ {
314314
let mut buf = [0u8; 2];
315315
BigEndian::write_i16(&mut buf, val);
316-
self.write_2(buf).await
316+
self.write_2(buf)
317317
}
318318

319-
async fn write_i32(&mut self, val: i32) -> IoResult<()> {
319+
fn write_i32(&mut self, val: i32) -> impl Future<Output = IoResult<()>> + '_ {
320320
let mut buf = [0u8; 4];
321321
BigEndian::write_i32(&mut buf, val);
322-
self.write_4(buf).await
322+
self.write_4(buf)
323323
}
324324

325-
async fn write_i64(&mut self, val: i64) -> IoResult<()> {
325+
fn write_i64(&mut self, val: i64) -> impl Future<Output = IoResult<()>> + '_ {
326326
let mut buf = [0u8; 8];
327327
BigEndian::write_i64(&mut buf, val);
328-
self.write_8(buf).await
328+
self.write_8(buf)
329329
}
330330

331-
async fn write_marker(&mut self, marker: Marker) -> IoResult<()> {
332-
self.write_u8(marker.to_u8()).await
331+
fn write_marker(&mut self, marker: Marker) -> impl Future<Output = IoResult<()>> + '_ {
332+
self.write_u8(marker.to_u8())
333333
}
334334

335335
pub async fn write_nil(mut self) -> IoResult<W> {

0 commit comments

Comments
 (0)