[cfe-dev] missing optimization opportunity for const std::vector compared to std::array

Marshall Clow mclow.lists at gmail.com
Sat May 31 07:14:34 PDT 2014


On May 31, 2014, at 4:17 AM, Dennis Luehring <dl.soluz at gmx.net> wrote:

> Am 30.05.2014 22:50, schrieb Richard Smith:
>>  We're not*permitted*  to
>> remove those operator new/operator delete calls by the standard, because
>> operator new and operator delete can be overridden by the user.
>> 
>> ....
>> 
>> So... we should add some way for std::allocator to say "allocate/deallocate
>> memory like a new-expression", and use it in libc++. I think we should add
>> 
>>   void *__builtin_operator_new(size_t)
>>   void __builtin_operator_delete(void*)
> 
> sounds like an good idea
> 
> but i do not understand why its so hard to track overridden allocations - or why
> they can't be removed - in the end there is an malloc/free or __builtin_operator_new/delete

Well, consider the following code:

	int * foo () { return new int; }

Does that call the default operator new?
Or some overridden one?

The compiler *can’t tell*.
In some other compilation unit, the user could have

#include <new>

char buffer[10240];
int offset = 0;

void * operator new (size_t sz) throw(std::bad_alloc) {
	void *p = buffer + offset;
	offset += sz;
	return p;
	}

and it’s up to the linker to figure that out.

— Marshall





More information about the cfe-dev mailing list