Skip to content

Commit

Permalink
move bounded_traversal from mononoke directory to common/rust/shed
Browse files Browse the repository at this point in the history
Summary: I'd like to make use of the `bounded_traversal` crate outside of mononoke's codebase to limit memory usage.

Reviewed By: mitrandir77, liubov-dmitrieva

Differential Revision: D70098058

fbshipit-source-id: 3f0cf6d007ef7bc3d37b4bbf4ecc28b62005708d
  • Loading branch information
YousefSalama authored and facebook-github-bot committed Feb 24, 2025
1 parent 15b8f94 commit cfe000f
Show file tree
Hide file tree
Showing 13 changed files with 3,220 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"shed/ascii_ext",
"shed/async_once_cell",
"shed/borrowed",
"shed/bounded_traversal",
"shed/buffered_weighted",
"shed/cached_config",
"shed/cachelib_stub",
Expand Down
26 changes: 26 additions & 0 deletions shed/bounded_traversal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# @generated by autocargo from //common/rust/shed/bounded_traversal:bounded_traversal

[package]
name = "bounded_traversal"
version = "0.1.0"
authors = ["Facebook <opensource+rust-shed@fb.com>"]
edition = "2021"
readme = "../../README.md"
repository = "https://github.com/facebookexperimental/rust-shed"
license = "MIT OR Apache-2.0"

[dependencies]
either = "1.5"
futures = { version = "0.3.30", features = ["async-await", "compat"] }
smallvec = { version = "1.13.2", features = ["serde", "specialization", "union"] }
thiserror = "2"

[dev-dependencies]
anyhow = "1.0.95"
cloned = { version = "0.1.0", path = "../cloned" }
lock_ext = { version = "0.1.0", path = "../lock_ext" }
maplit = "1.0"
pretty_assertions = { version = "1.2", features = ["alloc"], default-features = false }
quickcheck = "1.0"
quickcheck_async = "0.1.1"
tokio = { version = "1.41.0", features = ["full", "test-util", "tracing"] }
71 changes: 71 additions & 0 deletions shed/bounded_traversal/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under both the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree and the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree.
*/

use std::future::Future;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;

use either::Either;
use futures::ready;

/// Return value from `unfold` callbacks for ordered traversals. Each element
/// in the unfolded vector can be either an item to output from the traversal,
/// or a recursive step with an associated weight.
///
/// The associated weight should be an estimate of the number of eventual
/// output items this recursive step will expand into.
pub enum OrderedTraversal<Out, In> {
Output(Out),
Recurse(usize, In),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct NodeLocation<Index> {
pub node_index: Index, // node index inside execution tree
pub child_index: usize, // index inside parents children list
}

impl<Index> NodeLocation<Index> {
pub fn new(node_index: Index, child_index: usize) -> Self {
NodeLocation {
node_index,
child_index,
}
}
}

/// Equivalent of `futures::future::Either` but with heterogeneous output
/// types using `either::Either`.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub(crate) enum Either2<A, B> {
Left(A),
Right(B),
}

impl<A, B> Future for Either2<A, B>
where
A: Future,
B: Future,
{
type Output = Either<A::Output, B::Output>;

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
// see `impl<Left, Right> Future for Either<Left, Right>`
unsafe {
let result = match self.get_unchecked_mut() {
Either2::Left(future) => Either::Left(ready!(Pin::new_unchecked(future).poll(cx))),
Either2::Right(future) => {
Either::Right(ready!(Pin::new_unchecked(future).poll(cx)))
}
};
Poll::Ready(result)
}
}
}
Loading

0 comments on commit cfe000f

Please sign in to comment.