• Stars
    star
    160
  • Rank 234,703 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Bounded SPSC queue for Rust

Bounded SPSC Queue

Nightly Build Status

This crate provides a very simple bounded, Single-producer Single-consumer (SPSC) queue for Rust. It provides a data structure for two threads to communicate in one direction with minimal overhead and bounded semantics.

Compared to a sync_channel, this queue provides a small but consistent speedup. sync_channel utilizes an unbounded linked-list data structure under the covers, while bounded_spsc_queue is a simple ring buffer with single, solid block of allocated memory. The solid block of memory allows better cache pre-fetching due to less pointer indirection, and generally simpler operations to achieve a bounded SPSC queue.

Documentation

Documentation can be found here

Example

use std::thread;
use bounded_spsc_queue::{Producer, Consumer};

// Initialize a queue with capacity of 500 values
let (p, c) = bounded_spsc_queue::make(500);

// Spawn a new thread and move the Producer into it
thread::spawn(move|| {
  for i in 0..100000 {
    p.push(i);
  }
});

// Back in the first thread, start pop'ing values off the queue
for i in 0..100000 {
  let t = c.pop();
  assert!(t == i);
}

Semi-scientific Benchmarks

On my Macbook Air (dual-core 1.7 GHz Intel Core i7), sync_channel can perform single threaded send()/recv() paired operations in ~200ns.

Sync Chan PDF

spsc performs the same test in ~10ns. SPSC PDF

A more realistic, although harder test to accurately benchmark, is threaded performance. This test spins up a "listener" thread which attempts to drain the queue as fast as possible. It must intermittently check an Atomic flag to determine if the test is over, which unfortunately will impact the benchmark results. To help alleviate the impact, the listener will only check every 500 iterations.

In the original thread, the bencher attempts to push data onto the queue. This is where timing can get flaky: if the listener is not draining fast enough, the producer may stall while waiting for the queue to free a spot. This test should not be viewed as a latency test, but more as a throughput test since it is really testing both producer and consumer performance in conjunction.

sync_channel scores ~6ΞΌs on this test:

Sync Chan Threaded PDF

While spsc scores ~20ns:

SPSC Threaded PDF

A second set of benchmarks look at the inverse (secondary thread pushes onto the queue continuously, while the main thread pops data off). The timings are very similar.

Put another way:

  • SPSC performs ~50m push operations per second
  • Sync Channel performs ~170k operations per second

More Repositories

1

elasticsearch-inquisitor

Site plugin for Elasticsearch to help understand and debug queries.
JavaScript
700
star
2

athletic

PHP Benchmarking Framework
PHP
299
star
3

Turbine

A low-latency, high-throughput inter-task communication library
Rust
178
star
4

elasticsearch-segmentspy

ElasticSearch plugin to watch segment dynamics (additions, merges, deletes)
CSS
136
star
5

sherlock

PHP
119
star
6

rustl8710

Rust on RTL8710
C
60
star
7

ElasticBayes

Naive Bayes Classifier implemented with Elasticsearch Aggregations
PHP
51
star
8

HNHalfLife

HNHalfLife aims to increase discussion half-lives on Hacker News by making it easier to find new comments
JavaScript
29
star
9

quicksilver

Quicksilver - a library of approximate algorithms and sketches for Rust
Rust
17
star
10

Bi-aspheric-Singlet-Lens-Generator

Translation of the mathematica code to python from "General Formula for bi-aspheric singlet lens design free of spherical aberration"
Python
9
star
11

elasticsearch-ansible

Bootstrapping a cluster with Debian 6, OracleJDK 7 and Ansible
Shell
7
star
12

Mach4ATC

Some modules/macros/screensets for my Mach4 ATC setup
Lua
7
star
13

cormorant

Cormorant is a toy distributed key:value store written in Rust
Rust
6
star
14

cajal

A biologically inspired neural cellular automata
Rust
5
star
15

playground

Small personal projects and experiments
Rust
5
star
16

HNHighlightUser

Simple Greasemonkey script to highlight posts in Hacker News threads
JavaScript
3
star
17

USForeignAidVis

Visualization of US foreign aid over time
JavaScript
3
star
18

sherlockphp.org

Documentation site for Sherlock PHP Client
JavaScript
2
star