I know there was a topic about compiler code optimisation a while back but I can’t find it so I start a new…
When working on the Curses! code I now wanted to refactor some of the machine generated code done by Reform. I got the brilliant idea that I make changes and then make a diff of the compiled binaries to check that I don’t break something. Well…
When I refactor this routine:
[ AngelDevilConsultTopic tbl w ndx i len flag ;
ndx = 0;
while( 1 == 1 ) {
len = 1;
flag = false;
if( tbl-->ndx == -1 ) {
rfalse;
}
if( tbl-->ndx < 20 && tbl-->ndx >= 0 ) {
len = tbl-->ndx;
++ndx;
}
i = 0;
while( i < len ) {
if (w == tbl-->ndx) {
flag = true;
}
++i;
++ndx;
}
if( flag == true ) {
return tbl-->ndx;
}
++ndx;
}
];
to:
[ AngelDevilConsultTopic tbl w ndx i len flag ;
ndx = 0;
while( 1 == 1 ) {
len = 1;
flag = false;
if( tbl-->ndx == -1 ) rfalse;
if( tbl-->ndx < 20 && tbl-->ndx >= 0 ) {
len = tbl-->ndx;
++ndx;
}
i = 0;
while( i < len ) {
if (w == tbl-->ndx) flag = true;
++i;
++ndx;
}
if( flag == true ) return tbl-->ndx;
++ndx;
}
];
The resulting binaries differ!
Doing a check of the output in TXD reveals this:
--- org.txt
+++ new.txt
@@ -17237,27 +17237,26 @@
19a40: STORE L04,#01
19a43: STORE L05,#00
19a46: LOADW L00,L02 -> -(SP)
-19a4a: JE (SP)+,#ffff [FALSE] 19a51
-19a50: RFALSE
-19a51: LOADW L00,L02 -> -(SP)
-19a55: JL (SP)+,#14 [FALSE] 19a67
-19a59: LOADW L00,L02 -> -(SP)
-19a5d: JL (SP)+,#00 [TRUE] 19a67
-19a61: LOADW L00,L02 -> L04
-19a65: INC L02
-19a67: STORE L03,#00
-19a6a: JL L03,L04 [FALSE] 19a80
-19a6e: LOADW L00,L02 -> -(SP)
-19a72: JE L01,(SP)+ [FALSE] 19a79
-19a76: STORE L05,#01
-19a79: INC L03
-19a7b: INC L02
-19a7d: JUMP 19a6a
-19a80: JE L05,#01 [FALSE] 19a89
-19a84: LOADW L00,L02 -> -(SP)
-19a88: RET_POPPED
-19a89: INC L02
-19a8b: JUMP 19a40
+19a4a: JE (SP)+,#ffff [TRUE] RFALSE
+19a50: LOADW L00,L02 -> -(SP)
+19a54: JL (SP)+,#14 [FALSE] 19a66
+19a58: LOADW L00,L02 -> -(SP)
+19a5c: JL (SP)+,#00 [TRUE] 19a66
+19a60: LOADW L00,L02 -> L04
+19a64: INC L02
+19a66: STORE L03,#00
+19a69: JL L03,L04 [FALSE] 19a7f
+19a6d: LOADW L00,L02 -> -(SP)
+19a71: JE L01,(SP)+ [FALSE] 19a78
+19a75: STORE L05,#01
+19a78: INC L03
+19a7a: INC L02
+19a7c: JUMP 19a69
+19a7f: JE L05,#01 [FALSE] 19a88
+19a83: LOADW L00,L02 -> -(SP)
+19a87: RET_POPPED
+19a88: INC L02
+19a8a: JUMP 19a40
This indicates that the compiler is better at optimise if you leave out the curly brackets.
if( tbl-->ndx == -1 ) {
rfalse;
}
===>
19a4a: JE (SP)+,#ffff [FALSE] 19a51
19a50: RFALSE
vs
if( tbl-->ndx == -1 ) rfalse;
===>
19a4a: JE (SP)+,#ffff [TRUE] RFALSE
Not a big thing but I found it funny (and my idea to check if I mess up don’t work…).