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

Fix a problem with optimization 5bcfdb5400 #1330

Merged
merged 2 commits into from
Sep 15, 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
46 changes: 26 additions & 20 deletions src/engine/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,16 @@ template <bool OS> void Group::processWithOS(scxt::engine::Engine &e)
}
modMatrix.process();

auto oAZ = activeZones;
rescanWeakRefs = 0;
for (int i = 0; i < activeZones; ++i)
{
auto z = activeZoneWeakRefs[i];
assert(z->isActive());
z->process(e);
assert(z->isActive() || rescanWeakRefs > 0);
assert(oAZ == activeZones);

/*
* This is just an optimization to not accumulate. The zone will
* have already routed to the approprite other bus and output will
Expand All @@ -204,6 +209,11 @@ template <bool OS> void Group::processWithOS(scxt::engine::Engine &e)
}
}

if (rescanWeakRefs)
{
postZoneTraversalRemoveHandler();
}

// Groups are always unpitched and stereo
auto fpitch = 0;
bool processorConsumesMono[4]{false, false, false, false};
Expand Down Expand Up @@ -384,37 +394,33 @@ void Group::addActiveZone(engine::Zone *zwp)
void Group::removeActiveZone(engine::Zone *zwp)
{
assert(activeZones);
rescanWeakRefs++;
}

// Manage the weak refs
// First find the zone
int zidx{-1};
for (int idx = 0; idx < activeZones; ++idx)
void Group::postZoneTraversalRemoveHandler()
{
/*
* Go backwards down the weak refs removing inactive ones
*/
assert(rescanWeakRefs);
assert(activeZones);
assert(activeZones >= rescanWeakRefs);
for (int i = activeZones - 1; i >= 0; --i)
{
if (activeZoneWeakRefs[idx] == zwp)
if (!activeZoneWeakRefs[i]->isActive())
{
zidx = idx;
break;
rescanWeakRefs--;
activeZoneWeakRefs[i] = activeZoneWeakRefs[activeZones - 1];
activeZones--;
}
}
assert(zidx >= 0);
// OK so now we want to swap the active zone from the end into my slot
activeZoneWeakRefs[zidx] = activeZoneWeakRefs[activeZones - 1];

activeZones--;
assert(rescanWeakRefs == 0);
if (activeZones == 0)
{
ringoutMax = 0;
ringoutTime = 0;
updateRingout();
}

/*
SCLOG("removeZone " << SCD(activeZones));
for (int i = 0; i < activeZones; ++i)
{
SCLOG("removeZone " << activeZoneWeakRefs[i] << " " << activeZoneWeakRefs[i]->getName());
}
*/
}

engine::Engine *Group::getEngine()
Expand Down
2 changes: 2 additions & 0 deletions src/engine/group.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ struct Group : MoveableOnly<Group>,
private:
zoneContainer_t zones;
std::vector<Zone *> activeZoneWeakRefs;
uint32_t rescanWeakRefs{0};
void postZoneTraversalRemoveHandler();
};
} // namespace scxt::engine

Expand Down
Loading