php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33418 FEATURE: Alternative to eval() to effectively optimize scripts
Submitted: 2005-06-21 11:13 UTC Modified: 2005-06-21 13:45 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: alex-spam at rdc dot ru Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.11 OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-06-21 11:13 UTC] alex-spam at rdc dot ru
Description:
------------
From: Alex Fortuna

My proposal, if accepted and implemented, will allow for ever tougher optimization of processing-intensive PHP scripts.

I think it would be great if, in addition to existing eval() function, a pair of functions were implemented, like:

	int eval_prepare (string $code);
	bool eval_exec (int $handle);

eval_prepare() compiles a piece of code and stores it in engine's memory under an integer handle. You call it once outside the processing loop.

eval_exec() executes the given handle's code. You call it INSIDE the processing loop.

The reason is practical: sometimes the programmer KNOWS how to optimize a piece of code, but he CAN'T, since certain parts of it are dynamic. For example, actual string token values may differ from call to call, though processing logic is always the same.

The possibility to separately compile and execute script-generated PHP code will give programmers a unique chance to achieve ever greater script performance, the demand for which is continuosly growing.

I've made some prototype performance benchmarks on a script which parses syntax. The results were amazing: about 2 to 3 times performance increase without extra script complexity.

If you need a working example, please write via e-mail.

Thank you for your time.

Best regards,
Alex


Reproduce code:
---------------
.

Expected result:
----------------
.

Actual result:
--------------
.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-21 11:36 UTC] johannes@php.net
You could use create_function() to create your dynamic 
code and execute the returned function. No need for a new 
function. 
 [2005-06-21 12:43 UTC] alex-spam at rdc dot ru
create_function() will create a _function_. When we're talking tough optimization, every extra function call is an issue.

On 10k calls strcmp() is MUCH slower than '==' operator on strings.

Function calls are quite expensive in PHP. The context is switched and caller's variables become invisible.

Precompiled eval()s would allow to unroll small loops without changing the context, which is also important. What do you think?
 [2005-06-21 12:54 UTC] derick@php.net
New OpArrays (like your eval thing returns) can only be executing by calling a function (or something that resembles it very closely)... so you're not gaining anything here.
 [2005-06-21 13:03 UTC] alex-spam at rdc dot ru
I'm not a PHP internals expert so I can't get into detail on how 'precompiled evals' can be implemented.

No matter how it's named or implemented, the desired functionality would allow a programmer to:
- build a piece of code in CURRENT context and prepare it for execution (compile, bind all variables, functions etc.)
- execute this code using a lightweight language construct

'Preparation' process may have limitations, e.g. all referenced variables must already be created, etc. In this case eval_prepare() might just return FALSE with an appropriate error message.

Creating anonymous functions or using eval() are great by themselves, but not for optimization purposes. I hope you've got my idea.
 [2005-06-21 13:28 UTC] derick@php.net
Yes, I get what you want... but it's impossible to implement. You can't insert compiled code INTO the current scope - that's just not possible with the Zend Engine. (and it would require way too much work to do this.
 [2005-06-21 13:45 UTC] alex-spam at rdc dot ru
Hope that may become possible in future versions of Zend Engine.

Thank you for your comments.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 03 10:01:29 2024 UTC