ClassData() in loop

I’m writing my first PlugIn for RB/Xojo and I have a question about the use of ClassData() in a loop.

There seems to be a big difference between the following code:

Case 1:

Plugin code in c++

static UInt32 GetGreen(REALobject instance, UInt32 index) {
    ClassData(ABPictureDefinition, instance, ABPictureData, me);
    return (int)me->mBitPointer[index*3+1];
}

static void SetGreen(REALobject instance, UInt32 index, UInt32 value) {
    ClassData(ABPictureDefinition, instance, ABPictureData, me);
    me->mBitPointer[index*3+1]=value;
}

static UInt32 GetBlue(REALobject instance, UInt32 index) {
    ClassData(ABPictureDefinition, instance, ABPictureData, me);
    return (int)me->mBitPointer[index*3];
}

static void SetBlue(REALobject instance, UInt32 index, UInt32 value) {
    ClassData(ABPictureDefinition, instance, ABPictureData, me);
    me->mBitPointer[index*3]=value;
}

Xojo code:

  dim Start as Double
  dim Stop as Double
  dim i as integer
  dim Size as Integer
  
  Start = Microseconds
  
  dim ab as new ABPicture()
  ab.SetPicture(p, Horse.Width, Horse.Height)
  
  size = ab.OneColorTabelSize - 1
  
  for i = 0 to Size
    ab.SetBlue(i,ab.GetBlue(i)/2)
    ab.SetGreen(i,ab.GetGreen(i)/2)
  next
  
  p =ab.GetPicture
  
  Stop = (Microseconds-Start)/1000000

Time to run: 0,2247669745750427

Case 2:

Plugin code in c++

static void TestFunction(REALobject instance) {
    ClassData(ABPictureDefinition, instance, ABPictureData, me);
    for (int i=0;i<me->mOneColorTableSize;i++) {
        me->mBitPointer[i*3]=me->mBitPointer[i*3]/2;
        me->mBitPointer[i*3+1]=me->mBitPointer[i*3+1]/2;
    }
}

Xojo code:

  dim Start as Double
  dim Stop as Double
  dim i as integer
  dim Size as Integer
  
  Start = Microseconds
  
  dim ab as new ABPicture()
  ab.SetPicture(p, Horse.Width, Horse.Height)
  
  ab.TestFunction
  
  p =ab.GetPicture
  
  Stop = (Microseconds-Start)/1000000

Time to run: 0,0073340326805115

Case 2 is over 30 times faster than Case 1. My Question finally: :slight_smile:

Is there a posiiblity to use Case 1, but avoiding the call ClassData() every time I enter the SetBlue/GetBlue/SetGreen/GetGreen functions in the plugin? Is there a way to do this only once for a Class and then all additional function calls use this ‘me’?

No, ClassData is necessary. But ClassData() is not a function, so it is not a function call - it is macro and the C/C++ preprocessor will do a replacement at compile time. You’ll have to look at the plugin header files what it exactly does, also to see if that is really the place where it is slow.

Calling functions has an overhead. In first case you call 4 functions per entry. On second call you call only 1 function at all.
And ClassData is not the problem here.

Yes, I know I comparing apples and pears here, but I did not suspect this big an overhead. I was hoping to write as little ‘logic’ code as possible in the plugin so I could use Xojo for that. But no problem, I’ll just write it in C++. I need the speed to do blob detection and native Xojo is to slow to do this. Thanks for the tips anyway!