Cleaned up C++

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 24 05:59:17 PDT 2015


On Friday, 24 April 2015 at 12:34:19 UTC, ponce wrote:
> On Friday, 24 April 2015 at 08:16:40 UTC, Walter Bright wrote:
>> On 4/24/2015 12:23 AM, John Colvin wrote:
>>> Except of course that alloca is a lot cheaper than 
>>> malloc/free.
>>
>> That's not necessarily true. But in any case, go ahead and use 
>> it if you like. Just prepare to benchmark and be disappointed 
>> :-)
>
> Do you have a guess for why and when it could not be faster 
> than malloc in times?
> I have some difficulty imagining a reason (yet I have sometimes 
> found malloc faster than aligned_malloc which is another odd 
> thing).

one reason why it might be faster is that e.g. gcc can produce 
code like this:

#include<alloca.h>

void bar(char* a);

void foo(unsigned int n)
{
   char *a = (char*)alloca(n);
   bar(a);
}

foo:
	movl	%edi, %eax
	pushq	%rbp
	addq	$46, %rax
	movq	%rsp, %rbp
	shrq	$4, %rax
	salq	$4, %rax
	subq	%rax, %rsp
	leaq	31(%rsp), %rdi
	andq	$-32, %rdi
	call	bar
	leave
	ret

which is neat. Now of course a push-the-pointer malloc/free 
implementation could perhaps be (in theory) optimised to be as 
small as this, but is that ever actually the case?


More information about the Digitalmars-d mailing list