Skip to content

Commit 52d274d

Browse files
committed
feat: moved disposer out of graph class
1 parent fa4d2f0 commit 52d274d

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

packages/react-native-audio-api/common/cpp/audioapi/core/utils/graph/Graph.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ class Graph {
5555
using HNode = HostGraph::Node;
5656

5757
public:
58+
static constexpr size_t kDisposerPayloadSize = HostGraph::kDisposerPayloadSize;
5859
using ResultError = HostGraph::ResultError;
5960
using Res = Result<NoneType, ResultError>;
6061

61-
explicit Graph(size_t eventQueueCapacity) {
62+
Graph(size_t eventQueueCapacity, Disposer<kDisposerPayloadSize> *disposer) : disposer_(disposer) {
6263
using namespace audioapi::channels::spsc;
6364

6465
auto [es, er] = channel<AGEvent, OverflowStrategy::WAIT_ON_FULL, WaitStrategy::BUSY_LOOP>(
@@ -69,9 +70,10 @@ class Graph {
6970

7071
Graph(
7172
size_t eventQueueCapacity,
73+
Disposer<kDisposerPayloadSize> *disposer,
7274
std::uint32_t initialNodeCapacity,
7375
std::uint32_t initialEdgeCapacity)
74-
: Graph(eventQueueCapacity) {
76+
: Graph(eventQueueCapacity, disposer) {
7577
if (initialNodeCapacity > 0) {
7678
audioGraph.reserveNodes(initialNodeCapacity);
7779
nodeCapacity_ = initialNodeCapacity;
@@ -97,7 +99,7 @@ class Graph {
9799
AGEvent event;
98100
while (eventReceiver_.try_receive(event) == audioapi::channels::spsc::ResponseStatus::SUCCESS) {
99101
if (event) {
100-
event(audioGraph, disposer_);
102+
event(audioGraph, *disposer_);
101103
}
102104
}
103105
}
@@ -281,8 +283,6 @@ class Graph {
281283
}
282284

283285
private:
284-
static constexpr size_t kDisposerPayloadSize = HostGraph::kDisposerPayloadSize;
285-
286286
using OwnedSlotBuffer = std::unique_ptr<InputPool::Slot[]>;
287287

288288
// Aligning to cache line size to prevent false sharing between audio and main thread
@@ -296,7 +296,7 @@ class Graph {
296296

297297
// ── Disposer — destroys old pool buffers off the audio thread ───────────
298298

299-
DisposerImpl<kDisposerPayloadSize> disposer_{64};
299+
Disposer<kDisposerPayloadSize> *disposer_;
300300

301301
// ── Main-thread tracking for pre-growth ─────────────────────────────────
302302

packages/react-native-audio-api/common/cpp/test/src/graph/BridgeNodeTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,12 @@ TEST_F(BridgeGraphTest, BridgeOrphanedAndNoInputsGetsCompacted) {
320320

321321
class BridgeGraphWrapperTest : public ::testing::Test {
322322
protected:
323+
static constexpr size_t kPayloadSize = HostGraph::kDisposerPayloadSize;
324+
DisposerImpl<kPayloadSize> disposer_{64};
323325
std::shared_ptr<Graph> graph;
324326

325327
void SetUp() override {
326-
graph = std::make_shared<Graph>(4096);
328+
graph = std::make_shared<Graph>(4096, &disposer_);
327329
}
328330

329331
void processAll() {
@@ -465,7 +467,7 @@ TEST_F(BridgeGraphWrapperTest, ConcurrentWithMockGraphProcessor) {
465467
using Processor = audioapi::test::MockGraphProcessor<ProcessableMockNode>;
466468
// Pre-allocate node/pool capacity to avoid grow-event allocations inside
467469
// the AudioThreadGuard scope.
468-
auto sharedGraph = std::make_shared<Graph>(4096, 16, 64);
470+
auto sharedGraph = std::make_shared<Graph>(4096, &disposer_, 16, 64);
469471
Processor processor(*sharedGraph);
470472
processor.start();
471473

@@ -497,14 +499,16 @@ TEST_F(BridgeGraphWrapperTest, ConcurrentWithMockGraphProcessor) {
497499
class BridgeFuzzTest : public ::testing::TestWithParam<uint64_t> {
498500
protected:
499501
using HNode = HostGraph::Node;
502+
static constexpr size_t kPayloadSize = HostGraph::kDisposerPayloadSize;
500503

504+
DisposerImpl<kPayloadSize> disposer_{64};
501505
std::shared_ptr<Graph> graph;
502506
std::mt19937_64 rng;
503507
std::vector<HNode *> liveNodes;
504508
std::vector<AudioParam *> fakeParams;
505509

506510
void SetUp() override {
507-
graph = std::make_shared<Graph>(4096);
511+
graph = std::make_shared<Graph>(4096, &disposer_);
508512
rng.seed(GetParam());
509513

510514
// Create a set of fake param pointers

packages/react-native-audio-api/common/cpp/test/src/graph/GraphFuzzTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using namespace audioapi::utils::graph;
1717
using audioapi::test::MockGraphProcessor;
18+
using audioapi::utils::DisposerImpl;
1819

1920
// =========================================================================
2021
// Fixture — parameterized by seed for reproducible randomized testing
@@ -27,6 +28,8 @@ class GraphFuzzTest : public ::testing::TestWithParam<uint64_t> {
2728
using Res = Graph::Res;
2829
using ResultError = Graph::ResultError;
2930

31+
static constexpr size_t kPayloadSize = HostGraph::kDisposerPayloadSize;
32+
DisposerImpl<kPayloadSize> disposer_{64};
3033
std::mt19937_64 rng;
3134
std::unique_ptr<Graph> graph;
3235
std::vector<HNode *> nodes; // tracks live (non-removed) nodes
@@ -49,7 +52,7 @@ class GraphFuzzTest : public ::testing::TestWithParam<uint64_t> {
4952
// Ensure graph growth does not happen on the audio thread during this fuzz run.
5053
const auto maxNodes = static_cast<std::uint32_t>(initialNodeCount + operationCount + 64);
5154
const auto maxEdges = static_cast<std::uint32_t>(operationCount * 2 + 64);
52-
graph = std::make_unique<Graph>(4096, maxNodes, maxEdges);
55+
graph = std::make_unique<Graph>(4096, &disposer_, maxNodes, maxEdges);
5356

5457
// Randomly partition the range 0..99 into 4 operation weights
5558
size_t total = 100;

packages/react-native-audio-api/common/cpp/test/src/graph/GraphTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ namespace audioapi::utils::graph {
1313

1414
class GraphTest : public ::testing::Test {
1515
protected:
16+
static constexpr size_t kPayloadSize = HostGraph::kDisposerPayloadSize;
17+
DisposerImpl<kPayloadSize> disposer_{64};
1618
std::unique_ptr<Graph> graph;
1719

1820
void SetUp() override {
19-
graph = std::make_unique<Graph>(4096);
21+
graph = std::make_unique<Graph>(4096, &disposer_);
2022
}
2123

2224
const AudioGraph &getAudioGraph() {

0 commit comments

Comments
 (0)