[futurebasic] Re: [FB] Function calls with Globals as parameters

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2009 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Fri, 16 Oct 2009 21:39:10 +1300
Jay Reeve wrote:

> Obviously the simpler should be faster. You should even be able to  
> do something like this, omitting one variable transfer:
>
> local fn example#
> '...
> end fn = fn process#(gtestparameter#)

That may have saved a nanosecond or so in FB4. Modern optimizing  
compilers work out what your code means, then implement it in the most  
efficient manner.  This pleasing fact usually leaves the programmer  
free to write his code in the way that seems clearest and easiest to  
debug and maintain, rather than struggling for tiny optimizations.

Call1(), Call2() and Call3() below produce identical assembler when  
compiled 'Fastest, smallest', despite having 2, 1 and 0 temporary  
variables in the source.

Robert P.


'--------------------
local fn Test( d as double ) as double
end fn = sqr( d )

local fn Call1 as double
dim as double  temp, result
temp = gSomeGlobal
result = fn Test( temp )
end fn = result

local fn Call2 as double
dim as double  result
result = fn Test( gSomeGlobal )
end fn = result

local fn Call3 as double
end fn = fn Test( gSomeGlobal )
'-------------------------

.globl _Call1
_Call1:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	movl	L_gSomeGlobal$non_lazy_ptr, %eax
	sqrtsd	(%eax), %xmm0
	movsd	%xmm0, -8(%ebp)
	fldl	-8(%ebp)
	leave
	ret
.globl _Call2
_Call2:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	movl	L_gSomeGlobal$non_lazy_ptr, %eax
	sqrtsd	(%eax), %xmm0
	movsd	%xmm0, -8(%ebp)
	fldl	-8(%ebp)
	leave
	ret
.globl _Call3
_Call3:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	movl	L_gSomeGlobal$non_lazy_ptr, %eax
	sqrtsd	(%eax), %xmm0
	movsd	%xmm0, -8(%ebp)
	fldl	-8(%ebp)
	leave
	ret