[fpc-pascal] with statement using mulltiple objects

Tomas Hajny XHajT03 at hajny.biz
Mon Sep 15 09:05:51 CEST 2014


On Mon, September 15, 2014 07:55, Sven Barth wrote:
> On 14.09.2014 18:05, Philippe wrote:
>> someone wrote about a better performance using "with". is that true?
>> even with a simple pointer as in:
 .
 .
> This is the relevant code generated for TestWith:
>
> === asm begin ===
>
> # [28] with p^ do begin
>          movl    -4(%ebp),%ebx
> # [29] Prop1 := 42;
>          movl    %ebx,%eax
>          movw    $42,%dx
>          call    P$TWITHTEST_TTEST_$__SETPROP1$SMALLINT
> # [30] Prop2 := 21;
>          movl    %ebx,%eax
>          movw    $21,%dx
>          call    P$TWITHTEST_TTEST_$__SETPROP2$SMALLINT
>
> === asm end ===
>
> and this for TestWithout:
>
> === asm begin ===
>
> # [42] p^.Prop1 := 42;
>          movl    -4(%ebp),%eax
>          movw    $42,%dx
>          call    P$TWITHTEST_TTEST_$__SETPROP1$SMALLINT
> # [43] p^.Prop2 := 21;
>          movl    -4(%ebp),%eax
>          movw    $21,%dx
>          call    P$TWITHTEST_TTEST_$__SETPROP2$SMALLINT
>
> === asm end ===
>
> As you can see the expression p^ is only evaluated once in the TestWith
> case while it's evaluated twice in the TestWithout one. So it's only
> minimally faster in this example (one less memory access), but if you
> use enough members of TTest it a more or less tight loop it might even
> be noticeable.

Have you tried to enable optimizations? Diff of the two code fragments
generated with 2.6.4 and -O3:

--- t_with_o3.s	Mon Sep 15 08:59:07 2014
+++ t_without_o3.s	Mon Sep 15 08:59:32 2014
@@ -1,18 +1,14 @@
-# [26] New(p);
+# [40] New(p);
 	movl	$0,%eax
 	call	fpc_getmem
 	movl	%eax,%ebx
-# [28] with p^ do begin
-	movl	%ebx,%esi
-# [29] Prop1 := 42;
-	movl	%esi,%eax
+# [42] p^.Prop1 := 42;
 	movw	$42,%dx
 	call	P$TWITHTEST_TTEST_$__SETPROP1$SMALLINT
-	movl	%esi,%eax
-# [t.pas]
-# [30] Prop2 := 21;
+# [43] p^.Prop2 := 21;
+	movl	%ebx,%eax
 	movw	$21,%dx
 	call	P$TWITHTEST_TTEST_$__SETPROP2$SMALLINT
-# [33] Dispose(p);
+# [45] Dispose(p);
 	movl	%ebx,%eax
 	call	fpc_freemem

Note that there are actually more instructions generated for version using
'with' in this case (obviously depending on the context). In other words,
the remark about micro-optimizations (which may not necessarily lead to
expected results) applies here...

Tomas





More information about the fpc-pascal mailing list