Skip to content

Commit

Permalink
Fix a problem with optimization 5bcfdb5 (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
baconpaul authored Sep 15, 2024
1 parent 9c5ca31 commit 444418e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
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

0 comments on commit 444418e

Please sign in to comment.