Skip to content

Commit

Permalink
Implementing cached virtual methods (thx chatGPT) for projection methods
Browse files Browse the repository at this point in the history
LedFixture
- use cached virtual projection methods in projectAndMap

LedLeds:
- declare cached virtual projection methods in leds
- XYZ: call cached virtual projection method adjustXYZ

LedModEffects: pro: set cached virtual projection methods
  • Loading branch information
ewoudwijma committed Jun 26, 2024
1 parent 829d1bc commit 0843301
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/App/LedFixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ void Fixture::projectAndMap() {

mdl->getValueRowNr = rowNr; //run projection functions in the right rowNr context

if (projection) projection->adjustSizeAndPixel(sizeAdjusted, pixelAdjusted, midPosAdjusted);
//using cached virtual class methods!
if (projection) (projection->*leds->adjustSizeAndPixelCached)(sizeAdjusted, pixelAdjusted, midPosAdjusted);

if (leds->size == Coord3D{0,0,0}) { // first
ppf("projectAndMap first leds[%d] size:%d,%d,%d s:%d,%d,%d e:%d,%d,%d\n", rowNr, sizeAdjusted.x, sizeAdjusted.y, sizeAdjusted.z, startPosAdjusted.x, startPosAdjusted.y, startPosAdjusted.z, endPosAdjusted.x, endPosAdjusted.y, endPosAdjusted.z);
Expand All @@ -129,7 +130,8 @@ void Fixture::projectAndMap() {

mapped = pixelAdjusted;

if (projection) projection->adjustMapped(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);
//using cached virtual class methods!
if (projection) (projection->*leds->adjustMappedCached)(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);

mapped.x = mapped.distance(midPosAdjusted);
mapped.y = 0;
Expand Down Expand Up @@ -187,7 +189,8 @@ void Fixture::projectAndMap() {
break;
}

if (projection) projection->adjustMapped(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);
//using cached virtual class methods!
if (projection) (projection->*leds->adjustMappedCached)(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);

indexV = leds->XYZUnprojected(mapped);
break;
Expand Down Expand Up @@ -218,7 +221,8 @@ void Fixture::projectAndMap() {
break;
}

if (projection) projection->adjustMapped(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);
//using cached virtual class methods!
if (projection) (projection->*leds->adjustMappedCached)(mapped, sizeAdjusted, (pixel - startPosAdjusted)/10, midPosAdjusted);

indexV = leds->XYZUnprojected(mapped);

Expand Down
9 changes: 7 additions & 2 deletions src/App/LedLeds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ unsigned16 Leds::XYZ(Coord3D pixel) {
//as this is a call to a virtual function it reduces the theoretical (no show) speed by half, even if XYZ is not implemented
// the real speed is hardly affected, but room for improvement!
// so as a workaround we list them here explicetly
if ((projectionNr == p_TiltPanRoll || projectionNr == p_Preset1) && projectionNr < fixture->projections.size())
fixture->projections[projectionNr]->adjustXYZ(*this, pixel);
// if ((projectionNr == p_TiltPanRoll || projectionNr == p_Preset1) && projectionNr < fixture->projections.size())
// fixture->projections[projectionNr]->adjustXYZ(*this, pixel);


//using cached virtual class methods! (so no need for if projectionNr optimizations!)
if (projectionNr < fixture->projections.size())
(fixture->projections[projectionNr]->*adjustXYZCached)(*this, pixel);

return XYZUnprojected(pixel);
}
Expand Down
8 changes: 8 additions & 0 deletions src/App/LedLeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ struct PhysMap {

}; // 4 bytes

class Projection; //forward for cached virtual class methods!

class Leds {

public:
Expand All @@ -267,6 +269,12 @@ class Leds {

uint16_t fx = -1;
unsigned8 projectionNr = -1;

//using cached virtual class methods! 4 bytes each - thats for now the price we pay for speed
void (Projection::*adjustSizeAndPixelCached)(Coord3D &, Coord3D &, Coord3D &) = nullptr;
void (Projection::*adjustMappedCached)(Coord3D &, Coord3D, Coord3D, Coord3D) = nullptr;
void (Projection::*adjustXYZCached)(Leds &, Coord3D &) = nullptr;

unsigned8 effectDimension = -1;
unsigned8 projectionDimension = -1;

Expand Down
6 changes: 6 additions & 0 deletions src/App/LedModEffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ class LedModEffects:public SysModule {
if (proValue < fixture.projections.size()) {
Projection* projection = fixture.projections[proValue];

//setting cached virtual class methods! (By chatGPT so no source and don't understand how it works - scary!)
// (don't know how it works as it is not refering to derived classes, just to the base class but later it calls the derived class method)
leds->adjustMappedCached = &Projection::adjustMapped;
leds->adjustSizeAndPixelCached = &Projection::adjustSizeAndPixel;
leds->adjustXYZCached = &Projection::adjustXYZ;

mdl->varPreDetails(var, rowNr); //set all positive var N orders to negative
projection->controls(*leds, var);
mdl->varPostDetails(var, rowNr);
Expand Down

0 comments on commit 0843301

Please sign in to comment.