Skip to content

Limit mem usage + back-pressure via bounded channels - #4986

Merged
sophiajt merged 1 commit into
nushell:mainfrom
b333z:bounded-mpsc-externals
Mar 27, 2022
Merged

sophiajt merged 1 commit into
nushell:mainfrom
b333z:bounded-mpsc-externals

Conversation

@b333z

@b333z b333z commented Mar 27, 2022

Copy link
Copy Markdown
Contributor

Description

Prior to this change, a pipeline of externals would result in high memory
usage if any of the producers in the chain, produced data faster than
the consumers.

For example a pipeline:

fast-producer | slow-consumer

Would cause a build up of Value::{String,Binary}'s in the mpsc channels
between each command as values are added to the channels faster than they
are consumed, eventually OOM'ing depnding on system resources, the volume
of data and speed diff. between fast v's slow.

This change replaces the unbounded channels with bounded channels
to limit the number of values that can build up and providing
back-pressure to limit ram usage.

I randomly select a small value of 3 buffers in flight, this could probably be tuned
further.

Tests

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo build; cargo test --all --all-features to check that all the tests pass

Prior to this change, a pipeline of externals would result in high memory
usage if any of the producers in the chain, produced data faster than
the consumers.

For example a pipeline:

  > fast-producer | slow-consumer

Would cause a build up of `Value::{String,Binary}`'s in the mpsc channels
between each command as values are added to the channels faster than they
are consumed, eventually OOM'ing depnding on system resources, the volume
of data and speed diff. between fast v's slow.

This change replaces the unbounded channels with bounded channels
to limit the number of values that can build up and providing
back-pressure to limit ram usage.
@sophiajt

Copy link
Copy Markdown
Contributor

Sure, let's give it a try. Thanks!

@sophiajt
sophiajt merged commit 91e17d2 into nushell:main Mar 27, 2022
@b333z
b333z deleted the bounded-mpsc-externals branch April 14, 2022 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants