Wish Inline Java In Situ

cimperia

Active Member
Licensed User
Longtime User
Having the ability to write inline java code and its simple access mechanism, via JavaObject, is a wonderful addition to the language.

However, whatever code you have within the hash tags [#If Java ... #End if] is added to the bottom end of the class in the Java generated source.

I would like to see the ability to add java code in situ, ie the code would stay where it is by using hash tags like these, for example.

#If JavaInSitu

#End if

BA4 would ignore the code within those tags. They would be removed by the pre-processor (as it is now for #If Java tags) and therefore the Java code would be then part of the source and compiled by the Java compiler.

It would be a tremendous help to be able to mix this way, within the same sub, b4a code and Java.
 

cimperia

Active Member
Licensed User
Longtime User
Am I the only to think it would be a good feature to have?:(

It would be dead easy to implement too.
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Have you come across scenarios where you needed this functionality and using JavaObject, Reflection or Inline Java wouldn't cut it? Can you give an example? It seems to me that one- or two-line snippets can be handled with JavaObject or Reflection (albeit somewhat awkwardly). Longer snippets can be handled with Inline Java. Much larger snippets and class-size snippets are probably better off put in a Java class and compiled to a library using SLC.
 

cimperia

Active Member
Licensed User
Longtime User
Yes, there are many times when I would have needed to use In situ inline java.

Here's a quick example.

I wanted to check the performances of two different bits of code, identical functionally.
These instructions are called a great many times within my code.

There were cases where scenario 2 was better than 1 (1.5 plus rapide) and I wanted to test these within my application.

However the Java source code produced by BA4 is tremendously slower than the hand-coded one, compared to what I had when I tested from Android Studio. I could not compare performance within my B4A app.

Java code
B4X:
' dx, dxa, dy, dya are integers
' in essence, assign the absolute value of dx to dxa, but without using ABS() which is very slow.

  'scenario 1
  dxa = dx < 0 ? -dx : dx;
'vs
  'scenario 2
  dxa = (dx ^ (dx >> 31)) - (dx >> 31);

B4A code
B4X:
'1
If dx < 0 Then dxa = -dx Else dxa = dx
'vs
'2
dxa = Bit.Xor(dx, (Bit.ShiftRight(dx,31)) - Bit.ShiftRight(dx, 31))
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
Yes, there are many times when I would have needed to use In situ inline java.

Here's a quick example.

I wanted to check the performances of two different bits of code, identical functionally.
These instructions are called a great many times within my code.

There were cases where scenario 2 was better than 1 (1.5 plus rapide) and I wanted to test these within my application.

However the Java source code produced by BA4 is tremendously slower than the hand-coded one, compared to what I had when I tested from Android Studio. I could not compare performance within my B4A app.

Java code
B4X:
' dx, dxa, dy, dya are integers
' in essence, assign the absolute value of dx to dxa, but without using ABS() which is very slow.

  'scenario 1
  dxa = dx < 0 ? -dx : dx;
'vs
  'scenario 2
  dxa = (dx ^ (dx >> 31)) - (dx >> 31);

B4A code
B4X:
'1
If dx < 0 Then dxa = -dx Else dxa = dx
'vs
'2
dxa = Bit.Xor(dx, (Bit.ShiftRight(dx,31)) - Bit.ShiftRight(dx, 31))
I completely agree with Cimperia. Inline Java, as it works currently, is very slow. Having the possibility to embed Java lines directly in a B4A sub, with no translation (or maybe a conversion of the variable names; MyVar -> _myvar) would be a great feature and would be very useful to get the best performance.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Inline Java, as it works currently, is very slow
I don't agree with this general statement.

I did a quick test and it shows that a single call takes about 0.012 milliseconds (12 microseconds). 0.012 milliseconds means that it is insignificant.

B4X:
'always test performance in release mode
Sub Activity_Click
   Dim n As Long = DateTime.Now
   Dim x As Int
   For i = 1 To 10000
     x = x + nativeMe.RunMethod("Test", Array(1, 2))
   Next
   Log((DateTime.Now - n) / 10000)
End Sub

#If JAVA

public int Test(int a, int b) {
   return a + b;
}
#End If
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
I don't agree with this general statement.

I did a quick test and it shows that a single call takes about 0.012 milliseconds (12 microseconds). 0.012 milliseconds means that it is insignificant.

B4X:
'always test performance in release mode
Sub Activity_Click
   Dim n As Long = DateTime.Now
   Dim x As Int
   For i = 1 To 10000
     x = x + nativeMe.RunMethod("Test", Array(1, 2))
   Next
   Log((DateTime.Now - n) / 10000)
End Sub

#If JAVA

public int Test(int a, int b) {
   return a + b;
}
#End If
The Hue_Saturation example of my SpecialFX library was made with inline java initially because I took the code from one of my Java libs. I realized that I could code it entirely with B4A so I published a new version with no inline java. The converted code is the same (no optimization done), but now it runs 2x faster.
In my current games, I had to create Java libs for specific needs because the same inline code was too slow. I'm going to post a concrete example.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
B4X:
x = x + nativeMe.RunMethod("Test", Array(1, 2))

Is that a new Array syntax?
 

Informatix

Expert
Licensed User
Longtime User
I reused the Erel's code above and created the same function in B4A:
B4X:
Sub Process_Globals
   Private nativeMe As JavaObject
End Sub

Sub Globals
    Dim Const Repeat As Int = 1000000
    Dim Nano As NanoTime
    Dim nativeMe As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     nativeMe.InitializeContext
   End If
End Sub

#If JAVA
public int Test(int a, int b) {
   return a + b;
}
#End If

Sub Test(a As Int, b As Int) As Int
    Return a + b
End Sub

Sub Activity_Resume
    Dim t As Long = Nano.NanoTime
    Dim x As Int
    For R = 0 To Repeat - 1
        x = x + nativeMe.RunMethod("Test", Array(1, 2))
    Next
    Dim ResultIJ As Double = Nano.NanoTime
    ResultIJ = (ResultIJ - t) / R / 1000000
    Log("Inline Java=" & ResultIJ & " ms")

    Dim t As Long = Nano.NanoTime
    Dim x As Int
    For R = 0 To Repeat - 1
        x = x + Test(1, 2)
    Next
    Dim ResultBS As Double = Nano.NanoTime
    ResultBS = (ResultBS - t) / R / 1000000
    Log("B4A Sub=" & ResultBS & " ms")

    Log("Ratio=" & (ResultIJ/ResultBS))

    Activity.Finish
End Sub

The result on my Moto G needs no comment:
Inline Java=0.015834311245 ms
B4A Sub=7.2492708E-5 ms
Ratio=218.42626219729576
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The results are similar to my results.
It is good that the B4A code is faster.

The overhead of an inline java call is 0.01 millisecond. It is very small overhead and in 99.99% of the cases it is insignificant.

When you write:
Inline Java, as it works currently, is very slow
Then many developers will think that they should avoid using this feature (when needed). This is not the case.
 

Informatix

Expert
Licensed User
Longtime User
The results are similar to my results.
It is good that the B4A code is faster.

The overhead of an inline java call is 0.01 millisecond. It is very small overhead and in 99.99% of the cases it is insignificant.

When you write:

Then many developers will think that they should avoid using this feature (when needed). This is not the case.
When you call 200 times the same sub in a render thread in a game, any subtle difference becomes so huge that you have to find another solution. It's what happened to me more than once in my recent developments. What is insignificant for you is not insignificant at all for people that really need performance (200x0.01 ms = 2ms, which is not insignificant at all in a game). In this very simple example, inline Java is 200 times slower than pure Java and for me that means two things : "a lot" and "too much". BUT I'm the first to say that inline Java is a great feature and I'm very glad to be able to place a few lines of Java here and there, when needed, instead of using the Reflection or JavaObject library.
 

cimperia

Active Member
Licensed User
Longtime User
I am glad that you picked that up Informatix, as it raised some reaction from Erel.

Personally, I don't think that B4x will become a widely adopted alternative developing platform until one can get the performance required in some critical code sections. It means embedded java.

The only issue I can see is that the debugger would need changing.
 

Informatix

Expert
Licensed User
Longtime User
I am glad that you picked that up Informatix, as it raised some reaction from Erel.

Personally, I don't think that B4x will become a widely adopted alternative developing platform until one can get the performance required in some critical code sections. It means embedded java.

The only issue I can see is that the debugger would need changing.
I don't think that B4X aims the professional market. And there are a lot of professional domains where speed does not matter. To create an application to take orders in a restaurant, for example, B4A or B4J are absolutely perfect (B4I could be too but I wouldn't buy iPhone to take orders).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Personally, I don't think that B4x will become a widely adopted alternative developing platform until one can get the performance required in some critical code sections. It means embedded java.
In most cases the B4A code performance is similar to Java performance. In the very rare case where you do need to turn to java then you can use the inline java feature. The overhead will be 0.01 millisecond. This is insignificant in almost all cases. If you need to make many java calls then you can either move more code to the inline java (and reduce the number of calls required) or create a small library.

I don't think that B4X aims the professional market.
B4X aims to all markets. It is a simple and powerful RAD tool. Many professionals use B4X for many kinds of projects.
 
Top