26 releases (12 breaking)

0.13.2 Mar 10, 2024
0.12.0 Jan 3, 2024
0.10.0 Dec 28, 2023
0.9.0 Nov 27, 2023
0.1.0 Dec 11, 2021

#113 in Asynchronous

Download history 136/week @ 2023-12-30 77/week @ 2024-01-06 51/week @ 2024-01-13 31/week @ 2024-01-20 137/week @ 2024-01-27 142/week @ 2024-02-03 43/week @ 2024-02-10 79/week @ 2024-02-17 90/week @ 2024-02-24 219/week @ 2024-03-02 471/week @ 2024-03-09 86/week @ 2024-03-16 67/week @ 2024-03-23 127/week @ 2024-03-30 56/week @ 2024-04-06 69/week @ 2024-04-13

322 downloads per month
Used in 26 crates (4 directly)

MIT/Apache

285KB
6.5K SLoC

🧬 fn_graph

Crates.io docs.rs CI Coverage Status

Dynamically managed function graph execution.

This crate provides a FnGraph, where consumers can register a list of functions and their interdependencies. The graph can then return a stream of functions to iterate over either sequentially or concurrently. Any data dependencies required by the functions are guaranteed to not conflict according to borrowing rules.

There is additional flexibility that the type of functions is not limited to closures and functions, but any type that implements the FnRes and FnMeta traits.

Usage

Add the following to Cargo.toml

fn_graph = "0.13.2"

# Integrate with `fn_meta` / `interruptible` / `resman`
fn_graph = { version = "0.13.2", features = ["fn_meta"] }
fn_graph = { version = "0.13.2", features = ["interruptible"] }
fn_graph = { version = "0.13.2", features = ["resman"] }
fn_graph = { version = "0.13.2", features = ["fn_meta", "interruptible", "resman"] }

Rationale

Suppose there are three tasks, each represented by a function. Each function needs different data:

Function Data
f1 &a, &b
f2 &a, &b, &mut c
f3 &mut a, &b, &mut c

When scheduling parallel execution, it is valid to execute f1 and f2 in parallel, since data a and b are accessed immutably, and c is exclusively accessed by b. f3 cannot be executed in parallel with f1 or f2 as it requires exclusive access to both a and c.

For a small number of functions and data, manually writing code to schedule function execution is manageable. However, as the number of functions and data increases, so does its complexity, and it is desirable for this boilerplate to be managed by code.

Notes

The concept of a runtime managed data-dependency task graph is from shred; fn_graph's implementation has the following differences:

  • Different API ergonomics and flexibility trade-offs.

    • Takes functions and closures as input instead of System impls.

    • Parameters are detected from function signature instead of SystemData implementation, but with a limit of 8 parameters. (manual SystemData implementation has arbitrary limit)

    • Return type is type parameterized instead of ().

  • Instead of grouping functions by stages to manage data access conflicts, fn_graph keeps a dependency graph of logic and data dependencies, and executes functions when the preceding functions are complete.

    This allows for slightly less waiting time for subsequent functions with data dependencies, as each may begin once its predecessors finish, whereas a staged approach may contain other functions that are still executing that prevent functions in the next stage from beginning execution.

See Also

  • fn_meta: Returns metadata about a function at runtime.
  • interruptible: Support for interruptible streams.
  • resman: Runtime managed resource borrowing.
  • shred: Shared resource dispatcher.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3.5–5.5MB
~91K SLoC