async-alloc-counter

Crates.ioasync-alloc-counter
lib.rsasync-alloc-counter
version0.2.1
sourcesrc
created_at2021-10-12 22:17:59.816391
updated_at2021-10-14 20:49:16.251954
descriptionasync-alloc-counter measures max allocations in a future invocation
homepage
repositoryhttps://github.com/Geal/async-alloc-counter
max_upload_size
id464266
size21,372
Geoffroy Couprie (Geal)

documentation

README

async-alloc-counter measures max allocations in a future invocation

see examples/ for usage

This allocator can be used as follows:

use async_alloc_counter::*;
use futures::FutureExt;
use std::{alloc::System, time::Duration};

// set up the counting allocator
#[global_allocator]
static GLOBAL: AsyncAllocatorCounter<System> = AsyncAllocatorCounter { allocator: System };

#[tokio::main]
async fn main() {
  async move {
    let mut v: Vec<u8> = Vec::with_capacity(1024);
  }.count_allocations()
   .map(move |(max, ())| {
     println!("future allocated {} max bytes",  max);
   })
   .await
}

Allocation measurement can be stacked:

async move {
  println!("wrapping future");
  tokio::time::sleep(std::timeDuration::from_secs(1)).await;
  let mut v: Vec<u8> = Vec::with_capacity(256);

  async move {
      let mut v: Vec<u8> = Vec::with_capacity(1024);
    }.count_allocations()
     .map(move |(max, ())| {
       println!("future allocated {} max bytes",  max);
     })
     .await
  }.count_allocations()
   .map(move |(max, ())| {
     println!("warpping future allocated {} max bytes",  max);
   })
   .await

Design inspired by the excellent tracing crate

Commit count: 12

cargo fmt