Re: Code References
| From: | Zeev Suraski | Date: | Mon, 19 May 2003 15:29:32 +0000 |
| Subject: | Re: Code References | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-1721@lists.php.net to get a copy of this message | ||
At 16:56 19/05/2003, Sterling Hughes wrote:
On Mon, 2003-05-19 at 11:09, Zeev Suraski wrote: At 16:34 19/05/2003, Sterling Hughes wrote:?On Mon, 2003-05-19 at 10:55, Wez Furlong wrote:And so does PHP, through create_function, using PHPish syntax. create_function is not related in any way to call_user_func(), it's fairly fast, and will be faster. I don't see any additional things that are supported by this syntax, other than a tiny performance gain, that comes at the price of significantly reduced functionality (the code cannot contain anything dynamic, whereas with create_function, it can). That's why one has eval.use perl;Python, Ruby, LISP, Java, C, whatever, all support this in some form or another (whether it be function pointers or code references.)
Create_function() is a really hacky way of having closures/callbacks. Its not only inefficient, its ugly. create_function() is hardly ever used because its such an inelegant (and inefficient) way of doing things. From a syntax perspective anonymous functions are much clearer (the problem with single quotes is you need to then escape all single quotes).I can't really argue with this. For me the issues you raise here are really tiny nuances. Fact is that there's nothing your suggestion does that create_function doesn't; Whether or not your suggested syntax is clearer than that of create_function is definitely open for debate, but considering create_function is already in, the question on whether we should add something new is really a no brainer. Escaping with single quotes is really, just another tiny nuance. Single quotes are not that common at all.
(responding to a previous message re conditional compilation) What about when a compiler cache is introduced? Every high performance application most likely uses a compiler cache. Introducing a code reference, would remove that overhead, and would seriously simplify and improve callback code (I have a patch which optimizes nearly all of the callback code in php5, which I'll send shortly, but its cleaner if we had the concept of code references.)Then your suggestion would be negligibly faster, probably unmeasurable unless the only thing you do is call that anonymous function in a loop. That's my point, though. I'm not trying to convince you that create_function is better (even though I think it is). I'm trying to illustrate to you that it's at least as good as what you have in mind, and it's already there. Zeev