# Using a dylib function is really slow

**URL:** <https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772>\
**Category:** General Programming\
**Created:** [5 January 2024 11:34 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772 "2024-01-05T11:34:24Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [5 January 2024 11:34 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/1 "2024-01-05T11:34:24Z")

</div>

Hello,

I’m working again on a project that was put on hold for lack of time. It involves porting an Apple II emulator originally written in Java. I know, not very useful as there are already emulators that work properly. This is not a commercial project just for fun. It’s starting to work. It boots, the famous -151 call works and Applesoft programming also works. There’s still a lot to do, but I’m starting the optimization phase because Xojo isn’t exactly the right tool to do that. I’m using 2023r4. I’ve started by making a dylib to try to optimize the BitwiseAnd, BitwiseOr and others logical functions, which are called hundreds of thousands of times in just 1mn of operation. I just realized that going through my library was 10 times slower than using Xojo’s functions. When I disassemble the two, my function is much simpler and shorter than calling the Xojo framework. I deduce that it’s the call and return of the dylib function that creates the delay.

My question is: is there a way to speed things up?

Thanks in advance for your lights and advices

```auto
UInt16 xBitAnd(UInt16 v1, UInt16 v2)
{
     return v1 & v2;
}

```

```auto
       ; ================ B E G I N N I N G O F P R O C E D U R E ================

                     _xBitAnd:
0000000000003e50 push rbp
0000000000003e51 mov rbp, rsp
0000000000003e54 mov rax, rdi
0000000000003e57 and rax, rsi
0000000000003e5a pop rbp
0000000000003e5b ret
                        ; endp
0000000000003e5c align 32

```

 ![Screenshot 2024-01-05 at 12.25.11](https://ifnotnil.com/uploads/default/original/2X/5/5e8d84a57916465fa421e2f9f0f1d9e02c9d5ecd.png)

---

<div class="post-metadata">

**Author:** ![s7g2vp2](https://ifnotnil.com/letter_avatar_proxy/v4/letter/s/e9c0ed/32.png) [@s7g2vp2](https://ifnotnil.com/u/s7g2vp2)\
**Post date:** [5 January 2024 11:56 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/2 "2024-01-05T11:56:16Z")

</div>

You’ve probably ran into a similar problem that Christian Schmitz reported about plugin calls having lots of unnecessary overhead.

[https://tracker.xojo.com/xojoinc/xojo/-/issues/62010](https://tracker.xojo.com/xojoinc/xojo/-/issues/62010)

My gut feeling is that you won’t be able to do anything about it.

---

<div class="post-metadata">

**Author:** ![einhugur](https://ifnotnil.com/user_avatar/ifnotnil.com/einhugur/32/5207_2.png) [@einhugur](https://ifnotnil.com/u/einhugur)\
**Post date:** [5 January 2024 12:18 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/3 "2024-01-05T12:18:26Z")

</div>

> [@Ventus](#):
>
> commercial project just for fun. It’s starting to work. It boots, the famous -151 call works and Applesoft programming also works. There’s still a lot to do, but I’m starting the optimization phase because Xojo isn’t exactly the right tool to do that. I’m using 2023r4. I’ve started by making a dylib to try to optimize the BitwiseAnd, BitwiseOr and others log

Calling external function will never make sense to “optimize” small calls like a simple bitwise operation.

---

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [5 January 2024 12:40 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/4 "2024-01-05T12:40:30Z")

</div>

Probably. Innocently I thought after counting the number of cpu cycles taken by one or the other function that I was going to save a lot of time because for the moment the emulator is far from working at a correct speed.

---

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [5 January 2024 12:49 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/5 "2024-01-05T12:49:12Z")

</div>

If we had to rely on Xojo’s responsiveness … Another way would be to put the code directly into a memoryblock. I’ve seen this trick somewhere, maybe here.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [5 January 2024 14:51 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/6 "2024-01-05T14:51:53Z")

</div>

> [@Ventus](#):
>
> My question is: is there a way to speed things up?

Manually inline it since Xojo’s compiler wont do that for you.  
DONT use a function for this & you’ll avoid the call overhead.

Compilers normally will do a lot of things like compute invariants in loops & hoist them out side the loop, inline functions, and unroll loops  
You’ll have to do those for yourself

And make sure you turn off stack overflow checking, background tasks, nil object checks & bounds checking in the built version

---

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [5 January 2024 15:14 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/7 "2024-01-05T15:14:28Z")

</div>

> [@npalardy](#):
>
> DONT use a function for this & you’ll avoid the call overhead.

Maybe I misunderstood but I don’t see how I could make logical operations without calling functions native or custom.

> [@npalardy](#):
>
> Manually inline it since Xojo’s compiler wont do that for you.

I can actually save a little time by doing this. Too bad there isn’t a directive to automatically inline a function each time it’s called.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [5 January 2024 16:31 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/8 "2024-01-05T16:31:32Z")

</div>

For bitwise AND you and just use [AND](https://documentation.xojo.com/api/language/and.html)

```auto
    dim v as integer = 1 and 3

```

Same for [OR](https://documentation.xojo.com/api/language/or.html) and [XOR](https://documentation.xojo.com/api/language/xor.html#xor)

> [@Ventus](#):
>
> I can actually save a little time by doing this. Too bad there isn’t a directive to automatically inline a function each time it’s called.

There are requests for it  
Have been for some time  
Lets just say that I dont expect this will happen any time soon without significant compiler work

---

<div class="post-metadata">

**Author:** ![JeffT](https://ifnotnil.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@JeffT](https://ifnotnil.com/u/JeffT)\
**Post date:** [6 January 2024 12:04 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/9 "2024-01-06T12:04:27Z")

</div>

even ordinary methods add a lot of overhead when called in a tight loop.  
I just went through some of my code with a pragmatic eye…

Some variables had been set to computed - it was an easy way to ‘set some environmental stuff’ when a value was changed.  
eg - change a property, 3 other properties get set to suitable values in the Set()

But that means that the Get () was a method, rather than using a simple property.  
And it carries overheads… just whacking stuff in and out of the stack takes time.  
I changed the property back to a normal property, and created a SetValue method for those times when I wanted the extra stuff to happen.  
In some areas, that has halved the execution time of some tight loops.

---

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [6 January 2024 13:59 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/10 "2024-01-06T13:59:27Z")

</div>

> [@npalardy](#):
>
> For bitwise AND you and just use [AND](https://documentation.xojo.com/api/language/and.html)
> 
> ```auto
> dim v as integer = 1 and 3
> 
> ```
> 
> Same for [OR](https://documentation.xojo.com/api/language/or.html) and [XOR](https://documentation.xojo.com/api/language/xor.html#xor)

Norman,

AND, OR and XOR don’t do the job. They return a boolean instead a number. Emulating a processor requires, among other things, the ability to perform logical operations at the bit level.

I’m still looking for the trick of putting opcodes in a memory block and then invoking this code. I think I saw it on the official forum during a discussion about an x86 assembler made with Xojo. If anyone has a clue … at least to give a try.

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [6 January 2024 14:25 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/11 "2024-01-06T14:25:03Z")

</div>

I have one question: why not implementing it with java??? I can’t understand why there is a need for xojo especially while it is slow using dylibs. Using it with xojo Isa bad way to implement. Using Java will provide you the entire functionality of it. And you can write software around without problems

---

<div class="post-metadata">

**Author:** ![Joachim](https://ifnotnil.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@Joachim](https://ifnotnil.com/u/Joachim)\
**Post date:** [6 January 2024 14:41 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/12 "2024-01-06T14:41:16Z")

</div>

try

```auto
Var r, a, b As Integer
Var s As String 

a = &b1011011
b = &b1101001

r = a Or b
s = "a Or b" + EndOfLine + EndOfLine
s = s + a.ToBinary +" = " + a.ToString + EndOfLine
s = s + b.ToBinary +" = " + b.ToString + EndOfLine + EndOfLine
s = s + r.ToBinary +" = " + r.ToString + EndOfLine
MsgBox s

r = a And b
s = "a And b" + EndOfLine + EndOfLine
s = s + a.ToBinary +" = " + a.ToString + EndOfLine
s = s + b.ToBinary +" = " + b.ToString + EndOfLine + EndOfLine
s = s + r.ToBinary +" = " + r.ToString + EndOfLine
MsgBox s

r = a Xor b
s = "a Xor b" + EndOfLine + EndOfLine
s = s + a.ToBinary +" = " + a.ToString + EndOfLine
s = s + b.ToBinary +" = " + b.ToString + EndOfLine + EndOfLine
s = s + r.ToBinary +" = " + r.ToString + EndOfLine
MsgBox s

```

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [6 January 2024 15:32 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/13 "2024-01-06T15:32:40Z")

</div>

> [@Ventus](#):
>
> AND, OR and XOR don’t do the job. They return a boolean instead a number. Emulating a processor requires, among other things, the ability to perform logical operations at the bit level.

AND OR XOR etc on NUMERICAL values do BITWISE AND OR NOT with NO function call overhead  
Try it

---

<div class="post-metadata">

**Author:** ![Ventus](https://ifnotnil.com/letter_avatar_proxy/v4/letter/v/f17d59/32.png) [@Ventus](https://ifnotnil.com/u/Ventus)\
**Post date:** [6 January 2024 16:37 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/14 "2024-01-06T16:37:10Z")

</div>

> [@thorstenstueker](#):
>
> I have one question: why not implementing it with java??? I can’t understand why there is a need for xojo especially while it is slow using dylibs. Using it with xojo Isa bad way to implement. Using Java will provide you the entire functionality of it. And you can write software around without problems

As mentioned in the first post this emulator is already written in Java.

> **[GitHub - sicklittlemonkey/AppleIIGo: Apple //e emulator Java applet](https://github.com/sicklittlemonkey/AppleIIGo)**
>
> Apple //e emulator Java applet

I know like others people here that Xojo is far being the best tool to do this kind of thing but see that as a kind of challenge. To make a long story short, I’m retired. I spent over 24 years of my life at Apple, first as a technical support, then a programmer, then a network engineer and finally an IT manager. It’s just nostalgia and I like doing things that have no meaning or use and keep my mind busy. Anyway thanks for the suggestion 😉

---

<div class="post-metadata">

**Author:** ![Karen](https://ifnotnil.com/letter_avatar_proxy/v4/letter/k/7feea3/32.png) [@Karen](https://ifnotnil.com/u/Karen)\
**Post date:** [7 January 2024 01:45 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/15 "2024-01-07T01:45:08Z")

</div>

> [@npalardy](#):
>
> AND OR XOR etc on NUMERICAL values do BITWISE AND OR NOT with NO function call overhead

So why even have the functions or ever use them?

- Karen

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [7 January 2024 01:59 UTC](https://ifnotnil.com/t/using-a-dylib-function-is-really-slow/3772/16 "2024-01-07T01:59:21Z")

</div>

I dont recall when the AND OR NOT XOR got added to handle numerics as well

So for a long time you had no choice but to use the functions

EDIT  
Looks like the first big compiler overhaul in 2006 ish might have changed this  
LR from 2007 suggests AND had been modified to handle booleans & integer AND by then  
So its not THAT new 😛  
And there were still shift left & right operators that had no overload of something like AND OR NOT XOR etc do
