Using do_alloca() instead of emalloc()

php.internals

Andi Gutmans

22 years ago
Hey, I've been taking a look at the __autoload problem. During that time I saw that lots of places in the code (especially reflection API) use zend_str_tolower_dup(). In most of these cases it would be much more efficient to use do_alloca()/free_alloca() because that usually uses alloca() which allocates on the stack. Simplified example from reflection API code: lcname = zend_str_tolower_dup(name_str, name_len); if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) &property_info) == FAILURE) { efree(lcname); /* Error message */ return; } efree(lcname); This should really be: lcname = do_alloca(name_len+1); zend_str_tolower_copy(lcname, name_str, name_len+1); if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) &property_info) == FAILURE) { free_alloca(lcname); /* Error message */ return; } free_alloca(lcname); There are two times where you really shouldn't use do_alloca/free_alloca: a) If you're in code which requires a regular malloc() don't use them (the fallback is emalloc()). b) Only use them in functions which are about to return. They shouldn't be used in places like the main zend_execute loop because it would just make the stack grow more and more (in almost all cases where zend_str_tolower_dup() is being used, do_alloca/free_alloca are applicable). I need to go away for the weekend now but it be cool if people here would at least take care of this in their code (such as reflection API code). Thanks, Andi

Timm Friebe

22 years ago
On Thu, 2003-11-20 at 10:48, Andi Gutmans wrote:
> Hey, > > I've been taking a look at the __autoload problem. During that time I saw > that lots of places in the code (especially reflection API) use > zend_str_tolower_dup().
[...] Here's a patch for zend_reflection_api.c which uses do_alloca / free_alloca in all relevant places. - Timm

Marcus Börger

22 years ago
Hello Andi, Thursday, November 20, 2003, 10:48:57 AM, you wrote:
> Hey,
> I've been taking a look at the __autoload problem. During that time I saw > that lots of places in the code (especially reflection API) use > zend_str_tolower_dup(). > In most of these cases it would be much more efficient to use > do_alloca()/free_alloca() because that usually uses alloca() which > allocates on the stack.
Well Zeev and you told us to never it :-)
> Simplified example from reflection API code:
> lcname = zend_str_tolower_dup(name_str, name_len);
> if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) > &property_info) == FAILURE) { > efree(lcname); > /* Error message */ > return; > }
> efree(lcname);
> This should really be:
> lcname = do_alloca(name_len+1); > zend_str_tolower_copy(lcname, name_str, name_len+1);
> if (zend_hash_find(&ce->properties_info, lcname, name_len + 1, (void **) > &property_info) == FAILURE) { > free_alloca(lcname); > /* Error message */ > return; > }
> free_alloca(lcname);
> There are two times where you really shouldn't use do_alloca/free_alloca: > a) If you're in code which requires a regular malloc() don't use them (the > fallback is emalloc()). > b) Only use them in functions which are about to return. They shouldn't be > used in places like the main zend_execute loop because it would just make > the stack grow more and more (in almost all cases where > zend_str_tolower_dup() is being used, do_alloca/free_alloca are applicable).
> I need to go away for the weekend now but it be cool if people here would > at least take care of this in their code (such as reflection API code).
> Thanks,
> Andi
-- Best regards, Marcus mailto:helly@php.net

Andi Gutmans

22 years ago
Commited thanks. Marcus, what we meant was that you shouldn't use do_alloca() in the engine except for in the cases I mentioned, and possibly cases where the allocations would be quite large. In the case of reflection API the allocations are small *and* the function returns right away, thus not keeping stuff on the stack for too long. Andi At 11:09 PM 11/20/2003 +0100, Timm Friebe wrote:

Marcus Börger

22 years ago
Hello Andi, Sunday, November 23, 2003, 1:56:20 PM, you wrote:
> Commited thanks. > Marcus, what we meant was that you shouldn't use do_alloca() in the engine > except for in the cases I mentioned, and possibly cases where the > allocations would be quite large. In the case of reflection API the > allocations are small *and* the function returns right away, thus not > keeping stuff on the stack for too long.
Me has a big 'aha' effect :-)
-- Best regards, Marcus mailto:helly@php.net