Re: [fpc-pascal] Nesting

2010-09-14 Thread Luiz Americo Pereira Camara

Juha Manninen (gmail) escreveu:

A new Lazarus review :
http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or-
failure/

has this comment about Lazarus source:
---
Abundant use of the Exit() command instead of nesting code in If/then/else. It 
has been proven (last time in Delphi Informant Magazine) that allowing a 
method to nest itself out makes faster code. It is also easier to read and 
study. Exit should of course be used (it must be used in many situations) but 
prudently.

---

Does nesting really create faster code?

  


If i understand right what you mean by nesting, the produced code is 
equal at least in my simple test. See attached.


I dont know for more complex situations

Luiz

program template;
{$Mode ObjFpc}

procedure DoIt;
begin
  WriteLn('DoIt');
end;  

procedure UseExit(i: Integer);
begin
  if i = 0 then Exit;
  DoIt;
end;  

procedure DontUseExit(i: Integer);
begin
  if i <> 0 then
DoIt;  
end;

begin //Main 
  UseExit(1);
  DontUseExit(1); 
end..file "asmExit.pas"

.section .text

.section .text
.balign 4
.balign 4
# [asmExit.pas]
# [5] begin
.globl  P$TEMPLATE_DOIT
P$TEMPLATE_DOIT:
# Temps allocated between ebp-4 and ebp+0
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
movl%ebx,-4(%ebp)
# [6] WriteLn('DoIt');
callfpc_get_output
movl%eax,%ebx
movl$_$PROGRAM$_L10,%ecx
movl%ebx,%edx
movl$0,%eax
callfpc_write_text_shortstr
callFPC_IOCHECK
movl%ebx,%eax
callfpc_writeln_end
callFPC_IOCHECK
# [7] end;
movl-4(%ebp),%ebx
leave
ret

.section .text
.balign 4
.balign 4
# [10] begin
.globl  P$TEMPLATE_USEEXIT$LONGINT
P$TEMPLATE_USEEXIT$LONGINT:
# Temps allocated between ebp-4 and ebp-4
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
# Var i located at ebp-4
movl%eax,-4(%ebp)
# [11] if i = 0 then Exit;
testl   %eax,%eax
je  .L22
# [12] DoIt;
callP$TEMPLATE_DOIT
# [13] DoIt;
callP$TEMPLATE_DOIT
# [14] DoIt;
callP$TEMPLATE_DOIT
# [15] DoIt;
callP$TEMPLATE_DOIT
# [16] DoIt;
callP$TEMPLATE_DOIT
# [17] DoIt;
callP$TEMPLATE_DOIT
# [18] DoIt;
callP$TEMPLATE_DOIT
# [19] DoIt;
callP$TEMPLATE_DOIT
# [20] DoIt;
callP$TEMPLATE_DOIT
# [21] DoIt;
callP$TEMPLATE_DOIT
.L22:
# [22] end;
leave
ret

.section .text
.balign 4
.balign 4
# [25] begin
.globl  P$TEMPLATE_DONTUSEEXIT$LONGINT
P$TEMPLATE_DONTUSEEXIT$LONGINT:
# Temps allocated between ebp-4 and ebp-4
pushl   %ebp
movl%esp,%ebp
subl$4,%esp
# Var i located at ebp-4
movl%eax,-4(%ebp)
# [26] if i <> 0 then
testl   %eax,%eax
je  .L34
# [28] DoIt;
callP$TEMPLATE_DOIT
# [29] DoIt;
callP$TEMPLATE_DOIT
# [30] DoIt;
callP$TEMPLATE_DOIT
# [31] DoIt;
callP$TEMPLATE_DOIT
# [32] DoIt;
callP$TEMPLATE_DOIT
# [33] DoIt;
callP$TEMPLATE_DOIT
# [34] DoIt;
callP$TEMPLATE_DOIT
# [35] DoIt;
callP$TEMPLATE_DOIT
# [36] DoIt;
callP$TEMPLATE_DOIT
# [37] DoIt;
callP$TEMPLATE_DOIT
.L34:
# [39] end;
leave
ret

.section .text
.balign 4
.balign 4
# [41] begin //Main
.globl  PASCALMAIN
PASCALMAIN:
.globl  _main
_main:
# Temps allocated between ebp+0 and ebp+0
pushl   %ebp
movl%esp,%ebp
callFPC_INITIALIZEUNITS
# [42] UseExit(1);
movl$1,%eax
callP$TEMPLATE_USEEXIT$LONGINT
# [43] DontUseExit(1);
movl$1,%eax
callP$TEMPLATE_DONTUSEEXIT$LONGINT
# [44] end.
callFPC_DO_EXIT
leave
ret
.balign 4

.section .data
.ascii  "FPC 2.0.4 [2006/08/21] for i386 - Win32"
.balign 16
.balign 16
.globl  THREADVARLIST_P$TEMPLATE
THREADVARLIST_P$TEMPLATE:
.long   0
.balign 4
.globl  FPC_THREADVARTABLES
FPC_THREADVARTABLES:
.long   3
.long   THREADVARLIST_SYSTEM
.long   THREADVARLIST_OBJPAS
.long   THREADVARLIST_P$TEMPLATE
.balign 4
.globl  FPC_RESOURCESTRINGTABLES
FPC_RESOURCESTRINGTABLES:
.long   0
.balign 4
.globl  INITFINAL
INITFINAL:
.long   2,0
.long   INIT$_SYSTEM
.long   FINALIZE$_SYSTEM
.long   INIT$_OBJPAS
.long   FINALIZE$_OBJPAS

.section .data
.balign 4
.globl  __stklen
__stklen:
.long   262144

.section .data
.balign 4
.globl  __heapsize
__heapsize:
.long   0

.section .data

.section .data
.balign 4
.globl  _$PROGRAM$_L10
_$PROGRAM$_L10:
.ascii  "\004DoIt\000"

.section .data

.section .data

.section .bss

__

Re: [fpc-pascal] Nesting

2010-09-13 Thread Dimitri Smits

- "Juha Manninen (gmail)"  schreef:

> A new Lazarus review :
> http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or-
> failure/
> 
> has this comment about Lazarus source:
> ---
> Abundant use of the Exit() command instead of nesting code in
> If/then/else. It 
> has been proven (last time in Delphi Informant Magazine) that allowing
> a 
> method to nest itself out makes faster code. It is also easier to read
> and 
> study. Exit should of course be used (it must be used in many
> situations) but 
> prudently.
> ---
> 
> Does nesting really create faster code?

not really tested, but think about the following:
1) with nesting: 
you validate an expression on being true/false. According to the result, you 
jump over a return statement of over a jmp statement to the cleanup block. (if 
not optimized by compiler) So, you ALWAYS jump/call.
2) without nesting:
you validate an expression on being true/false. According to the result, you 
jump to the clean-up block. Otherwise you continue.

I haven't studied processors that intimately on their branch-prediction beyond 
the original Pentium (586), but there it could have 'significant' repercussions 
with pipeline-stalling resulting in a few clock-cycles extra for a jump 
statement (regardless if it was taken).

> 
> For readability I like the nesting style, except when there are very
> many such 
> tests in one function.

that is a matter of taste. I find the *overuse* of exit somewhat bad 
programming. It is not really clear in a function/procedure all the time during 
debugging "why this piece of code is never visited" when you have a few of 
those blocks above the said "tested" statement. A remnant of C-style 
programmers? Ofcourse there are cases where you need to use it, but in most 
cases it is about writing the correct control-statement.

kind regards,
Dimitri Smits
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Nesting

2010-09-13 Thread Juha Manninen (gmail)
A new Lazarus review :
http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or-
failure/

has this comment about Lazarus source:
---
Abundant use of the Exit() command instead of nesting code in If/then/else. It 
has been proven (last time in Delphi Informant Magazine) that allowing a 
method to nest itself out makes faster code. It is also easier to read and 
study. Exit should of course be used (it must be used in many situations) but 
prudently.
---

Does nesting really create faster code?

For readability I like the nesting style, except when there are very many such 
tests in one function.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal