Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safeguards to activateRoots and optimize render ops #41

Merged
merged 17 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
.cljs_node_repl
.cpcache
.cache
.DS_Store

*.swp

Expand Down
6 changes: 3 additions & 3 deletions js/packages/core/__tests__/__snapshots__/core.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,9 @@ exports[`garbage collection 1`] = `

exports[`garbage collection 2`] = `
[
[
5,
],
[
1,
520300429,
Expand All @@ -537,9 +540,6 @@ exports[`garbage collection 2`] = `
1,
1423955117,
],
[
5,
],
]
`;

Expand Down
4 changes: 2 additions & 2 deletions js/packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class Delegate {

this.batch = {
createNode: [],
deleteNode: [],
appendChild: [],
setProperty: [],
activateRoots: [],
commitUpdates: [],
deleteNode: []
};
}

Expand Down Expand Up @@ -128,11 +128,11 @@ class Delegate {
getPackedInstructions() {
return [
...this.batch.createNode,
...this.batch.deleteNode,
...this.batch.appendChild,
...this.batch.setProperty,
...this.batch.activateRoots,
...this.batch.commitUpdates,
...this.batch.deleteNode,
];
}
}
Expand Down
18 changes: 9 additions & 9 deletions runtime/elem/GraphRenderSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ namespace elem

// Next we prepare the render operation
bufferMap.emplace(node->getId(), ba.next());
auto* outputData = bufferMap.at(node->getId());

renderOps.push_back([=, bufferMap = this->bufferMap](HostContext<FloatType>& ctx) {
auto* outputData = bufferMap.at(node->getId());
renderOps.push_back([=](HostContext<FloatType>& ctx) {

node->process(BlockContext<FloatType> {
ctx.inputData,
Expand All @@ -117,18 +117,18 @@ namespace elem
}

// Next we prepare the render operation
bufferMap.emplace(node->getId(), ba.next());
auto* outputData = ba.next();
bufferMap.emplace(node->getId(), outputData);

// Allocate room for the child pointers here, gets moved into the lambda capture group below
std::vector<FloatType*> ptrs(children.size());
auto const numChildren = children.size();

renderOps.push_back([=, bufferMap = this->bufferMap, ptrs = std::move(ptrs)](HostContext<FloatType>& ctx) mutable {
auto* outputData = bufferMap.at(node->getId());
auto const numChildren = children.size();
for (size_t j = 0; j < numChildren; ++j) {
ptrs[j] = bufferMap.at(children[j]);
}

for (size_t j = 0; j < numChildren; ++j) {
ptrs[j] = bufferMap.at(children[j]);
}
renderOps.push_back([=, ptrs = std::move(ptrs)](HostContext<FloatType>& ctx) mutable {

node->process(BlockContext<FloatType> {
const_cast<const FloatType**>(ptrs.data()),
Expand Down
60 changes: 43 additions & 17 deletions runtime/elem/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,42 +332,68 @@ namespace elem
// Populate and activate from the incoming event
std::set<NodeId> active;

for (auto const& v : roots) {
for (auto const& v : roots)
{
if (!v.isNumber())
{
ELEM_DBG("[Error] activateRoot - Invalid nodeId format.");
return ReturnCode::InvalidInstructionFormat();
}

int32_t nodeId = static_cast<int32_t>((js::Number) v);
int32_t nodeId = static_cast<int32_t>((js::Number)v);
ELEM_DBG("[Native] activateRoot " << nodeIdToHex(nodeId));

if (nodeTable.find(nodeId) == nodeTable.end())
// Using find() method for safe access to nodeTable elements
auto it = nodeTable.find(nodeId);
if (it == nodeTable.end())
{
ELEM_DBG("[Error] activateRoot - NodeId not found: " << nodeIdToHex(nodeId));
return ReturnCode::NodeNotFound();
}

if (auto ptr = std::dynamic_pointer_cast<RootNode<FloatType>>(nodeTable.at(nodeId))) {
ptr->setProperty("active", true);
// Attempt to cast the found node to a RootNode type and activate it
auto ptr = std::dynamic_pointer_cast<RootNode<FloatType>>(it->second);
if (ptr)
{
ptr->activate(currentRoots.empty() ? FloatType(1) : FloatType(0));
active.insert(nodeId);
ELEM_DBG("[Success] Activated root: " << nodeIdToHex(nodeId));
}
else
{
ELEM_DBG("[Error] Failed to cast to RootNode or activate: " << nodeIdToHex(nodeId));
}
}

// Deactivate any prior roots
for (auto const& n : currentRoots) {
if (auto ptr = std::dynamic_pointer_cast<RootNode<FloatType>>(nodeTable.at(n))) {
// Deactivate any prior roots not included in the incoming active set
for (auto const& n : currentRoots)
{
auto it = nodeTable.find(n);
if (it != nodeTable.end())
{
auto ptr = std::dynamic_pointer_cast<RootNode<FloatType>>(it->second);
// If any current root was not marked active in this event, we deactivate it
cannc4 marked this conversation as resolved.
Show resolved Hide resolved
if (active.count(n) == 0) {
ptr->setProperty("active", false);
}

// And if it's still running, we hang onto it
if (ptr->stillRunning()) {
active.insert(n);
if (ptr)
{
if (active.count(n) == 0)
{
ptr->setProperty("active", false);
ELEM_DBG("[Success] Deactivated root: " << nodeIdToHex(n));
}
// And if it's still running, we hang onto it
if (ptr->stillRunning())
{
active.insert(n);
}
}
}
}

// Merge
currentRoots = active;
currentRoots.swap(active);
return ReturnCode::Ok();
}


//==============================================================================
template <typename FloatType>
void Runtime<FloatType>::processQueuedEvents(std::function<void(std::string const&, js::Value)>&& evtCallback)
Expand Down
6 changes: 6 additions & 0 deletions runtime/elem/builtins/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ namespace elem
return (t >= 0.5 || (std::abs(c - t) >= std::numeric_limits<FloatType>::epsilon()));
}

void activate(FloatType initialGain = FloatType(0))
{
setProperty("active", true);
currentGain.store(initialGain);
}

int setProperty(std::string const& key, js::Value const& val) override
{
if (key == "active") {
Expand Down
Loading