Wish Faster RaiseEvent

Informatix

Expert
Licensed User
Longtime User
When a Java lib calls a sub with raiseEvent or raiseEvent2, there's a significant overhead compared to a direct call to a function in Java, that's why I write more and more parts of my games in Java to avoid calling B4A subs, especially in loops. The speed gain can attain 100%. In a lib like libGDX where some events are called every frame, the difference is easily noticeable. Is there any chance that the speed of calls to B4A subs is improved in future versions? Is it possible to give a higher priority to some calls?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is currently a possible solution. However it only works with classes.
It allows you to call B4A subs without reflection.

B4A classes implement an interface named BA.SubDelegator.
Your library could accept an object of this type and use it to raise the events (pass a class instance from B4A).

In the B4A code you need to list the callable methods. It is done with this code:
B4X:
Public Sub Initialize
   If False Then
     CallSub(Me, "test") 'number of parameters is not important. This code will never be called.
     CallSub(Me, "Test_2")
   End If
End Sub

Sub test (i As Int)

End Sub

Sub Test_2

End Sub

Compile your app in release mode and you will see that it creates a method named callSub with the relevant subs.

In your java code you can use this callSub method (part of the interface) instead of raiseEvent.
 

Informatix

Expert
Licensed User
Longtime User
There is currently a possible solution. However it only works with classes.
It allows you to call B4A subs without reflection.

B4A classes implement an interface named BA.SubDelegator.
Your library could accept an object of this type and use it to raise the events (pass a class instance from B4A).

In the B4A code you need to list the callable methods. It is done with this code:
B4X:
Public Sub Initialize
   If False Then
     CallSub(Me, "test") 'number of parameters is not important. This code will never be called.
     CallSub(Me, "Test_2")
   End If
End Sub

Sub test (i As Int)

End Sub

Sub Test_2

End Sub

Compile your app in release mode and you will see that it creates a method named callSub with the relevant subs.

In your java code you can use this callSub method (part of the interface) instead of raiseEvent.
Interesting to know. Unfortunately, the important events are in Main so it does not really help (unless there's a mean to add automatically the callSub method with its code for all public methods of the main class).
 

Informatix

Expert
Licensed User
Longtime User
I wanted a solution that is not too cumbersome for B4A users, e.g. to improve the speed of libGDX when a lot of events are raised. But the current solution is not suitable for most users so I will wait for an improvement in future versions. For my own code, I can write parts in Java so I'm not 100% concerned.
 
Top