Skip to content

Commit fd062c9

Browse files
committed
working on async streams
1 parent b0bbf16 commit fd062c9

31 files changed

+2860
-41
lines changed

.bazelrc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ build:windows --copt='/Zc:dllexportInlines-' --host_copt='/Zc:dllexportInlines-'
2929

3030
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
3131
build:clippy --output_groups=+clippy_checks
32-
build:clippy --@rules_rust//:clippy_flags=-Dclippy::all,-Dclippy::pedantic,-Dwarnings
33-
build:clippy --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
34-
build:clippy --output_groups=+rustfmt_checks
35-
build:clippy --@rules_rust//:extra_rustc_flag=-Dwarnings
32+
build:clippy --@rules_rust//:clippy_flags=-Dclippy::all,-Dclippy::pedantic,-Dwarnings
3633

3734
## Sanitizers
3835

@@ -49,6 +46,19 @@ build:asan --test_env=ASAN_OPTIONS=abort_on_error=true
4946
build:asan --test_env=LSAN_OPTIONS=report_objects=1
5047
build:asan --test_env=KJ_CLEAN_SHUTDOWN=1
5148

49+
# Benchmarking configuration
50+
51+
build:bench -c opt
52+
build:bench --copt="-O3"
53+
build:bench --copt="-DNDEBUG"
54+
build:bench --@capnp-cpp//src/capnp:capnp_no_inline_accessors=False
55+
build:bench --copt="-flto=thin" --linkopt="-flto=thin"
56+
build:bench --@rules_rust//rust/toolchain/channel=nightly
57+
build:bench --@rules_rust//:extra_rustc_flag=-Zdylib-lto
58+
build:bench --@rules_rust//:extra_rustc_flag=-Cembed-bitcode
59+
build:bench --@rules_rust//:extra_rustc_flag=-Clto=thin
60+
build:bench --@rules_rust//:extra_rustc_flag=-Ccodegen-units=1
61+
5262
###############################################################################
5363
## Custom user flags
5464
##

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
/expand.rs
77
/target/
88
/Cargo.lock
9+
flamegraph.*
10+
flamegraph-*.*
11+
perf.data
12+
perf.data.*
13+

.helix/languages.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[language-server.rust-analyzer]
2+
config = { "rust-analyzer.workspace.discoverConfig"= { "command"= [ "just", "_rust-analyzer" ], "progressLabel"= "generating rust analyzer config", "filesToWatch"= [ "BUILD.bazel" ] }}

MODULE.bazel

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ bazel_dep(name = "bazel_features", version = "1.21.0")
99
bazel_dep(name = "bazel_skylib", version = "1.7.1")
1010
bazel_dep(name = "platforms", version = "0.0.11")
1111
bazel_dep(name = "rules_cc", version = "0.1.1")
12-
bazel_dep(name = "rules_rust", version = "0.60.0")
12+
bazel_dep(name = "rules_rust", version = "0.61.0")
1313
bazel_dep(name = "aspect_bazel_lib", version = "2.15.3")
1414

1515
cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure_extension")
1616
use_repo(cc_configure, "local_config_cc")
1717

1818
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
19-
rust.toolchain(versions = ["1.86.0", "nightly/2025-06-06"])
19+
20+
# nightly/2025-04-03 doesn't work with --config=bench
21+
# https://github.com/bazelbuild/rules_rust/issues/3459
22+
rust.toolchain(versions = [
23+
"1.86.0",
24+
"nightly/2025-02-20",
25+
])
2026
use_repo(rust, "rust_toolchains")
2127

2228
register_toolchains("@rust_toolchains//:all")
@@ -28,3 +34,12 @@ use_repo(crate_repositories, "crates.io", "vendor")
2834

2935
capnp_cpp = use_extension("//:capnp_cpp.bzl", "capnp_cpp")
3036
use_repo(capnp_cpp, "capnp-cpp")
37+
38+
# Hedron's Compile Commands Extractor for Bazel
39+
# https://github.com/hedronvision/bazel-compile-commands-extractor
40+
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
41+
git_override(
42+
module_name = "hedron_compile_commands",
43+
commit = "4f28899228fb3ad0126897876f147ca15026151e",
44+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
45+
)

MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

justfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ rustfmt:
2929
bazel run @rules_rust//:rustfmt
3030

3131
clang-format:
32-
clang-format -i kj-rs/*.h kj-rs/*.c++ kj-rs/tests/*.h kj-rs/tests/*.c++
32+
clang-format -i kj-rs/**/*.h kj-rs/**/*.c++
3333

3434

3535
compile-commands:
3636
bazel run @hedron_compile_commands//:refresh_all
37-
37+
38+
profile-async-stream-test:
39+
bazel build --config=bench //kj-rs/tests:async-stream-test
40+
bazel test --test_output=all --config=bench //kj-rs/tests:async-stream-test
41+
perf record -F max --call-graph lbr ./bazel-bin/kj-rs/tests/async-stream-test
42+
perf script report flamegraph
43+
3844
# called by rust-analyzer discoverConfig (quiet recipe with no output)
3945
@_rust-analyzer:
4046
rm -rf ./rust-project.json

kj-rs/BUILD.bazel

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")
2-
load("@rules_rust//rust:defs.bzl", "rust_library")
2+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
33

44
cc_library(
55
name = "kj-rs-lib",
@@ -25,10 +25,17 @@ rust_library(
2525
":bridge",
2626
":kj-rs-lib",
2727
"@workerd-cxx//:cxx",
28+
"@crates.io//:futures",
2829
"@crates.io//:static_assertions",
2930
],
3031
)
3132

33+
rust_test(
34+
name = "kj-rs_test",
35+
crate = ":kj-rs",
36+
edition = "2024",
37+
)
38+
3239
rust_cxx_bridge(
3340
name = "bridge",
3441
src = "lib.rs",

kj-rs/awaiter.c++

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,6 @@ bool RustPromiseAwaiter::poll(const WakerRef& waker, const KjWaker* maybeKjWaker
8787
KJ_IF_SOME(optionWaker, maybeOptionWaker) {
8888
// Our Promise is not yet ready.
8989

90-
// Check for an optimized wake path.
91-
KJ_IF_SOME(kjWaker, maybeKjWaker) {
92-
KJ_IF_SOME(futurePollEvent, kjWaker.tryGetFuturePollEvent()) {
93-
// Optimized path. The Future which is polling our Promise is in turn being polled by a
94-
// `co_await` expression somewhere up the stack from us. We can arrange to arm the
95-
// `co_await` expression's KJ Event directly when our Promise is ready.
96-
97-
// If we had an opaque Waker stored in OptionWaker before, drop it now, as we won't be
98-
// needing it.
99-
optionWaker.set_none();
100-
101-
// Store a reference to the current `co_await` expression's Future polling Event. The
102-
// reference is weak, and will be cleared if the `co_await` expression happens to end before
103-
// our Promise is ready. In the more likely case that our Promise becomes ready while the
104-
// `co_await` expression is still active, we'll arm its Event so it can `poll()` us again.
105-
linkedGroup().set(futurePollEvent);
106-
107-
return false;
108-
}
109-
}
110-
11190
// Unoptimized fallback path.
11291

11392
// Tell our OptionWaker to store a clone of whatever Waker we were given.

kj-rs/executor-guarded.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bool isCurrent(const kj::Executor& executor) {
99
}
1010

1111
void requireCurrent(const kj::Executor& executor, kj::LiteralStringConst message) {
12-
KJ_REQUIRE(isCurrent(executor), message);
12+
// KJ_REQUIRE(isCurrent(executor), message);
1313
}
1414

1515
} // namespace kj_rs

kj-rs/io/BUILD.bazel

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
load("//tools/bazel:rust_cxx_bridge.bzl", "rust_cxx_bridge")
2+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
3+
4+
rust_cxx_bridge(
5+
name = "bridge",
6+
src = "lib.rs",
7+
hdrs = ["bridge.h"],
8+
include_prefix = "kj-rs/io",
9+
deps = [
10+
"//kj-rs",
11+
"@capnp-cpp//src/kj:kj",
12+
"@capnp-cpp//src/kj:kj-async",
13+
"@workerd-cxx//:cxx",
14+
],
15+
)
16+
17+
cc_library(
18+
name = "bridge_cpp",
19+
srcs = ["bridge.c++"],
20+
hdrs = ["bridge.h"],
21+
include_prefix = "kj-rs/io",
22+
deps = [
23+
":bridge/include",
24+
"@capnp-cpp//src/kj:kj",
25+
"@capnp-cpp//src/kj:kj-async",
26+
],
27+
)
28+
29+
rust_library(
30+
name = "io",
31+
srcs = ["lib.rs"],
32+
edition = "2024",
33+
visibility = ["//visibility:public"],
34+
proc_macro_deps = [
35+
"@crates.io//:async-trait",
36+
],
37+
deps = [
38+
":bridge_cpp",
39+
"//kj-rs",
40+
"@crates.io//:futures",
41+
"@workerd-cxx//:cxx",
42+
],
43+
)
44+
45+
rust_test(
46+
name = "io_test",
47+
crate = ":io",
48+
edition = "2024",
49+
)
50+
51+
rust_cxx_bridge(
52+
name = "tests_bridge",
53+
src = "tests.rs",
54+
hdrs = ["tests.h"],
55+
include_prefix = "kj-rs/io",
56+
deps = [
57+
":bridge_cpp",
58+
":io",
59+
"@capnp-cpp//src/kj:kj",
60+
"@capnp-cpp//src/kj:kj-async",
61+
"@workerd-cxx//:cxx",
62+
],
63+
)
64+
65+
rust_library(
66+
name = "io_tests",
67+
srcs = ["tests.rs"],
68+
edition = "2024",
69+
visibility = ["//visibility:public"],
70+
proc_macro_deps = [
71+
"@crates.io//:async-trait",
72+
],
73+
deps = [
74+
":io",
75+
":tests_bridge",
76+
"//kj-rs",
77+
"@crates.io//:futures",
78+
"@workerd-cxx//:cxx",
79+
],
80+
)
81+
82+
rust_test(
83+
name = "io_tests_test",
84+
crate = ":io_tests",
85+
edition = "2024",
86+
)
87+
88+
cc_test(
89+
name = "io_cpp_tests",
90+
size = "medium",
91+
srcs = ["tests.c++", "tests.h"],
92+
deps = [
93+
":bridge",
94+
":bridge_cpp",
95+
":bridge/include",
96+
":io_tests",
97+
":tests_bridge",
98+
":io",
99+
"@capnp-cpp//src/kj:kj",
100+
"@capnp-cpp//src/kj:kj-async",
101+
"@capnp-cpp//src/kj:kj-test",
102+
"@workerd-cxx//:cxx",
103+
],
104+
linkstatic = True,
105+
)

0 commit comments

Comments
 (0)