Giter VIP home page Giter VIP logo

min-batch.rs's Introduction

min-batch

Build status Cargo Documentation

An adapter that turns elements into a batch and its weight is computed by given closure. It is needed for efficient work parallelization so that following tasks running in parallel are all processing a batch of at least min_batch_weight to avoid context switching overhead of cpu intensive workloads. Otherwise we usually need to introduce some kind of publish/subscribe model with dedicated long-running thread for each consumer, broadcasting messages to them and establishing back-pressure through barrier.

Usage

There are 2 stream extension methods :

  • min_batch(min_batch_weight, fn_to_extract_weight)
  • min_batch_with_weight(min_batch_weight, fn_to_extract_weight)

The elements are grouped into batches of minimal weight, possible returning the weight of a batch with it

use futures::{stream, StreamExt};
use min_batch::ext::MinBatchExt;

#[derive(Debug, PartialEq, Eq)]
struct BlockOfTxs {
    name: char,
    txs_count: usize,
}

#[tokio::main]
async fn main() {
   let mut block_names: Vec<char> = vec!['a', 'b', 'c', 'd'];
   let min_batch_weight = 3;
   let batches: Vec<Vec<BlockOfTxs>> = 
       stream::iter(1..=4)
           .map(|x| BlockOfTxs {
               name: block_names[x - 1],
               txs_count: x,
           })
           .min_batch(min_batch_weight, |block: &BlockOfTxs| block.txs_count)
           .collect()
           .await;
   
   assert_eq!(batches.len(), 3);
   
   // collect first two Blocks of Transactions until the total count of transactions is >= 3
   assert_eq!(
       batches[0],
       vec![
           BlockOfTxs {
               name: 'a',
               txs_count: 1
           },
           BlockOfTxs {
               name: 'b',
               txs_count: 2
           }
       ],
   );
   
   // the third Block has already 3 transactions so lets move to the next one
   assert_eq!(
       batches[1],
       vec![BlockOfTxs {
           name: 'c',
           txs_count: 3
       }],
   );
   assert_eq!(
       batches[2],
       vec![BlockOfTxs {
           name: 'd',
           txs_count: 4
       }],
   );
}

min-batch.rs's People

Contributors

pragmaxim avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.