Hi all,
to experiment with the optimization capabilities of the Jabaco compiler, I played around with Jabaco using the following:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
Option Explicit
Public Sub main(ByJava args() As String)
Dim a As Integer
Dim s As String
Dim b As Boolean
a = a
a = 0 + a
a = a + 0
a = 1 * a
a = a * 1
a = 3 * 2
s = s
s = s & ""
s = "" & s
s = "a" & "b"
If False And b Then
b = False And b
b = b And False
b = True Or b
b = b Or True
End If
' resulting java byte code
' does not show any optimization
' int a = 0;
' String s = "";
' a = a;
' a = 0 + a;
' a += 0;
' a = 1 * a;
' a *= 1;
' a = 3 * 2;
' s = s;
' s = VBString.appendStrings(s, "");
' s = VBString.appendStrings("", s);
' s = VBString.appendStrings("a", "b");
' If(False & b)
' {
' b = False & b;
' b &= False;
' b = True | b;
' b |= True;
' }
' for comparison:
' result of a Java compilation (JDK 1.5)
' a = a;
' a = 0 + a;
' a = a + 0;
' a = 1 * a;
' a = a * 1;
' a = 6; // 2 * 3 folded to 6; VB6 does the same
' s = s;
' s = (New StringBuilder()).append(s).append("").toString();
' s = (New StringBuilder()).append("").append(s).toString();
' s = "ab"; // "a" + "b" folded to "ab"
' If(False & b)
' {
' b = False & b;
' b &= False;
' b = True | b;
' b |= True;
' }
End Sub
|
The result:
Jabaco does not perform any (?) optimization during compilation. It should be noted, however, that neither the Javac compiler of JDK 1.5 nor the original VB6 compiler do perform optimization. There are small exceptions: Javac as well as VB do constant folding, i.e. they translate experessions like "2*3" to "6" at compile time. Javac resolves concatenation of string literals at compile time.
Optimization may become important when it comes to evaluation of boolean expressions in if .. then .. end if constructs. Jabaco does not support "shut cut" mode. The full boolean expression with all constituent parts is evaluated even if the false/true result can be determined early. A "c" program would not evaluate "b()" in expression "a() && b()" unless "a()" returns a non-zero TRUE value. Jabaco evaluate "a()" and "b()" in expression "a() AND b()" regardless of the return value of "a()".
Happy optimizing and experimenting!
A1880