Re: [PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-13 Thread mike
On Thu, Mar 12, 2009 at 10:35 PM, Robert Cummings rob...@interjinn.com wrote:

 The only thing that should defeat the usefulness of a bytecode cache is
 the use of eval since the cache has no reference point upon which to
 determine if the eval'd code has been previously compiled and has
 changed since.

I guess I considered something like

call_user_func($some variables) was somewhat equivalent of an eval...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-12 Thread mike
For templating ideas ...

Would bytecode caches (APC) be able to work properly with this:

function print_something($args, $output = 'html') {
  switch($output) {
   'iphone':
   print_something_iphone($args);
   break;
   default:
   print_something_html($args);
   break;
  }
}


or something like this:

function print_something($args, $output = 'html') {
  if(function_exists('print_something_'.$output)) {
   call_user_func('print_something_'.$output);
  }
}


On Wed, Mar 4, 2009 at 12:45 PM, mike mike...@gmail.com wrote:
 On Wed, Mar 4, 2009 at 4:01 AM, Jochem Maas joc...@iamjochem.com wrote:
 ..not an internals question me thinks ... redirecting to generals mailing 
 list

 Actually, I do think it is somewhat internals related.

 I want to know from the internals/experts angle if this is a good
 function to be relying on, or if it is one of those things like the
 @ operator which I've been told is expensive and to me is one of
 those things to stay away from.

 Now if this breaks opcode caches like APC, I will have to find another way.

 Also - I write procedural, not OOP. So that won't help here.

 Would creating functions such as

 output_foo_html()
 output_foo_rss()
 output_foo_json()

 Then depending on the output, using something like this? Would this be
 breaking opcode caches as well then?

 if(function_exists('output_foo_'.$format)) {
    call_user_func('output_foo_'.$format);
 } else {
    output_foo_html();
 }


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-12 Thread Robert Cummings
On Thu, 2009-03-12 at 21:55 -0700, mike wrote:
 For templating ideas ...
 
 Would bytecode caches (APC) be able to work properly with this:
 
 function print_something($args, $output = 'html') {
   switch($output) {
'iphone':
print_something_iphone($args);
break;
default:
print_something_html($args);
break;
   }
 }
 
 
 or something like this:
 
 function print_something($args, $output = 'html') {
   if(function_exists('print_something_'.$output)) {
call_user_func('print_something_'.$output);
   }
 }

I thought we covered a question just like this a week ago... yes this
will not pose a problem for a bytecode cache.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-12 Thread mike
On Thu, Mar 12, 2009 at 10:10 PM, Robert Cummings rob...@interjinn.com wrote:

 I thought we covered a question just like this a week ago... yes this
 will not pose a problem for a bytecode cache.

Apologies. I saw an example of some OOP thing from internals before it
moved to -general.

So even using call_user_func() with dynamic functions won't break the
usefulness of APC?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-12 Thread Robert Cummings
On Thu, 2009-03-12 at 22:27 -0700, mike wrote:
 On Thu, Mar 12, 2009 at 10:10 PM, Robert Cummings rob...@interjinn.com 
 wrote:
 
  I thought we covered a question just like this a week ago... yes this
  will not pose a problem for a bytecode cache.
 
 Apologies. I saw an example of some OOP thing from internals before it
 moved to -general.
 
 So even using call_user_func() with dynamic functions won't break the
 usefulness of APC?

The only thing that should defeat the usefulness of a bytecode cache is
the use of eval since the cache has no reference point upon which to
determine if the eval'd code has been previously compiled and has
changed since.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-04 Thread Jochem Maas
..not an internals question me thinks ... redirecting to generals mailing list

mike schreef:
 I am trying to figure out a strategy for multiple output formats on a
 site, and it seems like I can have functions defined by default, but
 have them defined -after- I've included the targetted format first.
 
 However that would require
 
 file1.php:
 
 function foo() {}
 
 file2.php
 
 if(!function_exists('foo')) {
function foo() {}
 }
 
 Is this a very expensive thing to do? There would be a good number of
 functions that would be leveraging this. Is it a no-no? or is it
 pretty cheap?

it busts opcode caches. so I wouldn't recommend it at all,
don't do conditional includes or function/class definitions if you
can help it.

idea: use some kind of registry pattern to register outputters,
the the code requiring an outputter can request it from the register.

1. I would think about using a class for each outputter (instead of
a function) ... chances are you will have need for more than just 'foo'

2. register all the default outputters

3. register some optional, overriding outputters dependent on the context.

?php

abstract class OutputRegistry
{
private $objs;

static function register($key, $obj) {
self::$objs[$key] = $obj;
}

static function get($key) {
return self::$objs[$key];
}
}

// default
OutputRegistry::register('renderer', new HTMLRenderer);

// overriding (in some other file) to allow creating JSON output
OutputRegistry::register('renderer', new JSONRenderer);


 
 Thanks
 - mike
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-04 Thread mike
On Wed, Mar 4, 2009 at 4:01 AM, Jochem Maas joc...@iamjochem.com wrote:
 ..not an internals question me thinks ... redirecting to generals mailing list

Actually, I do think it is somewhat internals related.

I want to know from the internals/experts angle if this is a good
function to be relying on, or if it is one of those things like the
@ operator which I've been told is expensive and to me is one of
those things to stay away from.

Now if this breaks opcode caches like APC, I will have to find another way.

Also - I write procedural, not OOP. So that won't help here.

Would creating functions such as

output_foo_html()
output_foo_rss()
output_foo_json()

Then depending on the output, using something like this? Would this be
breaking opcode caches as well then?

if(function_exists('output_foo_'.$format)) {
call_user_func('output_foo_'.$format);
} else {
output_foo_html();
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-04 Thread Robert Cummings
On Wed, 2009-03-04 at 11:45 -0800, mike wrote:
 On Wed, Mar 4, 2009 at 4:01 AM, Jochem Maas joc...@iamjochem.com wrote:
  ..not an internals question me thinks ... redirecting to generals mailing 
  list
 
 Actually, I do think it is somewhat internals related.
 
 I want to know from the internals/experts angle if this is a good
 function to be relying on, or if it is one of those things like the
 @ operator which I've been told is expensive and to me is one of
 those things to stay away from.
 
 Now if this breaks opcode caches like APC, I will have to find another way.
 
 Also - I write procedural, not OOP. So that won't help here.
 
 Would creating functions such as
 
 output_foo_html()
 output_foo_rss()
 output_foo_json()
 
 Then depending on the output, using something like this? Would this be
 breaking opcode caches as well then?
 
 if(function_exists('output_foo_'.$format)) {
 call_user_func('output_foo_'.$format);
 } else {
 output_foo_html();
 }

Perfectly fine. Shouldn't break an opcode cache. the opcode cache will
store the opcodes to do what you've done above and also will store
opcodes for the function wherever it was declared. Then when it runs off
it will go. For what it's worth... functions have global scope... all
functions are store in some kind of lookup table. Associative arrays
have O( lg n ) lookup time. Let's imagine you had 1 million defined
functions... We're talking at most 19 node traversals on a balanced
binary tree (yes I know it's a hashing algorithm). A billion declared
functions 29 steps. Is it fast? Yes.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: [PHP-DEV] How expensive are function_exists() calls?

2009-03-04 Thread Jochem Maas
mike schreef:
 On Wed, Mar 4, 2009 at 4:01 AM, Jochem Maas joc...@iamjochem.com wrote:
 ..not an internals question me thinks ... redirecting to generals mailing 
 list
 
 Actually, I do think it is somewhat internals related.

internals is about engine development. always ask on the generals list
first, you can always escalate the question if no-one there can offer
any help.

 
 I want to know from the internals/experts angle if this is a good
 function to be relying on, or if it is one of those things like the
 @ operator which I've been told is expensive and to me is one of
 those things to stay away from.
 
 Now if this breaks opcode caches like APC, I will have to find another way.
 
 Also - I write procedural, not OOP. So that won't help here.

whatever. sounds like a limited way of looking at things, nonetheless
it's perfectly feasable to write the registry pattern using
functions a/some global var(s).

 Would creating functions such as
 
 output_foo_html()
 output_foo_rss()
 output_foo_json()
 
 Then depending on the output, using something like this? Would this be
 breaking opcode caches as well then?

no, the thing that breaks the opcode cache is when you declare a function
conditionally because the function declaration has to be deferred until
runtime. there are plenty of detailed posts on the internals list
about this (try searching for 'APC' will likely turn up quite alot).

 if(function_exists('output_foo_'.$format)) {
 call_user_func('output_foo_'.$format);
 } else {
 output_foo_html();
 }

you don't need call_user_func() here (which is very handy but also slow):

$func = 'output_foo_'.$format;
if (function_exists($func))
$func();
else
output_foo_html();



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php