Hi everybody,
I have an application where a small file is included at multiple places.
So far so good.
The problem is that this include consists in a small piece of code which is
inside a multi-level loop.
The include is done about an average of 100.000 times .
When I manually replace the include with the code which is inside, the
performance boost is about a 50-100 factor..
(with opcache enabled, it is far worst without opcache).
The problem is that if I make a change within the code, I do have to not
forget to make changes a dozen of places...
Could it be possible to add a directive such as :
inline < filename >
That includes the file inline within the compiled pseudo code (at compile
time) ...
(it does not have a big importance for me that the target pseudo-code sets
the line number of all inlined instructions to the line number of the inline
directive)
Am I the only people needing this feature ?
Best regards,
Pascal KISSIAN
I have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really
should write a function, include it only once, and then simply call the
function instead of repeatedly including the file. Calling a function is
a lot faster than including a file.
--
Lauri Kenttä
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really should write a function, include it only once, and then simply call the function instead of repeatedly >including the file. Calling a function is a lot faster than including a file.
--
Lauri Kenttä
Function is not appropriate when the "inlined code" has to share 20-30 local variables ...
Having a function with 30 args, or having to build and access an array is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local variables/arrays needed for the computing.
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really should write a function, include it only once, and then simply call the function instead of repeatedly >including the file. Calling a function is a lot faster than including a file.
--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30 local variables ...
Having a function with 30 args, or having to build and access an array is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local variables/arrays needed for the computing.
Your code definitely needs refactoring then. A class sounds more
appropriate in this case. You needed to refactor back around 3 nested
for-loops. 6 nested for-loops is a disaster. What you've described
here is indicative of code that needs to be completely scrapped and
rebuilt from the ground-up.
--
Thomas Hruska
CubicleSoft President
I've got great, time saving software that you will find useful.
Am 4.10.2016 um 16:33 schrieb Pascal KISSIAN php-mailing-list@lool.fr:
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really should write a function, include it only once, and then simply call the function instead of repeatedly >including the file. Calling a function is a lot faster than including a file.
--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30 local variables ...
Having a function with 30 args, or having to build and access an array is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local variables/arrays needed for the computing.
Hey,
why do you think are your includes so slow?
Apart from the include itself (which in itself is fairly slow due to filesystem access; or in case with opcache, at the very least copying the op_array etc.), it does exactly this: building an array and importing it into the included files scope.
Also, 20-30 local variables? That sounds a bit like your code has way too many responsibilities in one same place. Perhaps you should restructure your code instead, but the way you describe it, no.
If your code is so "hot", that also even function calls would be quite significant then you should probably really inline your code there as a perf optimization. Anyway, code with that many variables almost always can meaningfully refactored.
It is not the languages task to optimize insane code ...
Bob
2016-10-04 17:39 GMT+02:00 Bob Weinand bobwei9@hotmail.com:
Am 4.10.2016 um 16:33 schrieb Pascal KISSIAN php-mailing-list@lool.fr:
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really
should write a function, include it only once, and then simply call the
function instead of repeatedly >including the file. Calling a function is a
lot faster than including a file.--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30
local variables ...
Having a function with 30 args, or having to build and access an array
is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local
variables/arrays needed for the computing.
6-level nested loop and 20-30 local variables in scope has enought
complexity to refactor. This stuff definitelly needs refactor.
I wouldn't allow any developer to commit such code, it woudn't pass code
review.
There was time I also have such code but it wasn't maintainable, once
written was never beeing understood by any other developer.
I think you should read about CleanCode ->
https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
Hey,
why do you think are your includes so slow?
Apart from the include itself (which in itself is fairly slow due to
filesystem access; or in case with opcache, at the very least copying the
op_array etc.), it does exactly this: building an array and importing it
into the included files scope.Also, 20-30 local variables? That sounds a bit like your code has way too
many responsibilities in one same place. Perhaps you should restructure
your code instead, but the way you describe it, no.If your code is so "hot", that also even function calls would be quite
significant then you should probably really inline your code there as a
perf optimization. Anyway, code with that many variables almost always can
meaningfully refactored.It is not the languages task to optimize insane code ...
Bob
--
regards / pozdrawiam,
Michał Brzuchalski
brzuchalski.com
« tell me what you need, and I would tell you how to do without it » is a nonsense answer for me…
I just told you about one among several cases where I found the need for such a feature.
I can also tell you, by example, of a specific grammar driven php writen parser where about 100 functions have their 2 first lines and 3 ending lines exactly the same…
Another use would be to have an « inlined code » containing the begining of a loop structure, and another one the ending of that loop structure….
(that is no more possible with include in new php versions but it was possible in older ones, not considering performance issues)
And don’t tell me that it is bad coding, or rewrote whole php source code without macros before.
As an example : ZEND_HASH_FOREACH_VAL and ZEND_HASH_FOREACH_END are very usefull for php itself.
Why do not allow php to have such kind of functionnalities?
Before thinking about macros, the 1st little step, not so difficult to implement, would be, according to me, to have an inline statement that does the same as include, but at compile time instead of at runtime…
And my only question is :
“Am I the only people needing this kind of feature?”
If I am the only one, just do as if I never posted to this forum, but if many people think it could be usefull, perhaps it would make sense to reconsider the question.
Best regards,
Pascal KISSIAN
De : Michał Brzuchalski [mailto:michal@brzuchalski.com]
Envoyé : mardi 4 octobre 2016 17:52
À : Bob Weinand
Cc : Pascal KISSIAN; PHP internals list
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instruction
2016-10-04 17:39 GMT+02:00 Bob Weinand bobwei9@hotmail.com:
Am 4.10.2016 um 16:33 schrieb Pascal KISSIAN php-mailing-list@lool.fr:
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really should write a function, include it only once, and then simply call the function instead of repeatedly >including the file. Calling a function is a lot faster than including a file.
--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30 local variables ...
Having a function with 30 args, or having to build and access an array is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local variables/arrays needed for the computing.
6-level nested loop and 20-30 local variables in scope has enought complexity to refactor. This stuff definitelly needs refactor.
I wouldn't allow any developer to commit such code, it woudn't pass code review.
There was time I also have such code but it wasn't maintainable, once written was never beeing understood by any other developer.
I think you should read about CleanCode -> https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882
Hey,
why do you think are your includes so slow?
Apart from the include itself (which in itself is fairly slow due to filesystem access; or in case with opcache, at the very least copying the op_array etc.), it does exactly this: building an array and importing it into the included files scope.
Also, 20-30 local variables? That sounds a bit like your code has way too many responsibilities in one same place. Perhaps you should restructure your code instead, but the way you describe it, no.
If your code is so "hot", that also even function calls would be quite significant then you should probably really inline your code there as a perf optimization. Anyway, code with that many variables almost always can meaningfully refactored.
It is not the languages task to optimize insane code ...
Bob
--
regards / pozdrawiam,
--
Michał Brzuchalski
brzuchalski.com <https://brzuchalski.com/
On Tuesday, October 4, 2016, Pascal KISSIAN <php-mailing-list@lool.fr
javascript:_e(%7B%7D,'cvml','php-mailing-list@lool.fr');> wrote:
« tell me what you need, and I would tell you how to do without it » is a
nonsense answer for me…I just told you about one among several cases where I found the need for
such a feature.
And you've been told that functions and/or classes would do a better job.
Without seeing the code it's impossible to be sure, but it's a one in a
million shot you've hit a situation that classes and functions won't work.
In the long run it will be less expensive to take the lessons learned in
building this program and apply them to making a new one. Or you can throw
your time and money away nursing this old project along.
On Tue, Oct 4, 2016 at 8:33 AM, Pascal KISSIAN php-mailing-list@lool.fr
wrote:
-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really should
write a function, include it only once, and then simply call the function
instead of repeatedly >including the file. Calling a function is a lot
faster than including a file.--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30
local variables ...
Having a function with 30 args, or having to build and access an array is
not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local
variables/arrays needed for the computing.
As many have said this may be a sign of a need to refactor. however, to say
that building an array and accessing it is not natural, consider that you
can use compact / extract.
function doLoops($vars) {
extract($vars);
// many for loops
return compact(implode(' ', array_keys($vars)));
}
// elsewhere set up a bunch of variables
extract(doLoops(compact('list of variable names goes here')));
Problem solved, now you are not including in a loop, but calling an already
compiled function, without adding TONS of lines of code to build such an
array. Its a short solution, to a problem that should probably lead to a
redesign of the functionality anyway.
On Tue, Oct 4, 2016 at 8:33 AM, Pascal KISSIAN php-mailing-list@lool.fr
wrote:-----Message d'origine-----
De : Lauri Kenttä [mailto:lauri.kentta@gmail.com]
Envoyé : mardi 4 octobre 2016 16:21
À : Pascal KISSIAN php-mailing-list@lool.fr
Cc : internals@lists.php.net
Objet : Re: [PHP-DEV] Feature Request: inline pseudo-instructionI have an application where a small file is included at multiple
places.The include is done about an average of 100.000 times .
I'm just wondering if you have ever heard of functions? You really
should write a function, include it only once, and then simply call the
function instead of repeatedly >including the file. Calling a function is a
lot faster than including a file.--
Lauri KenttäFunction is not appropriate when the "inlined code" has to share 20-30
local variables ...
Having a function with 30 args, or having to build and access an array
is not very efficient nor natural...
Local variables include 6 nested "for loop" indices and local
variables/arrays needed for the computing.As many have said this may be a sign of a need to refactor. however, to
say that building an array and accessing it is not natural, consider that
you can use compact / extract.function doLoops($vars) {
extract($vars);
// many for loops
return compact(implode(' ', array_keys($vars)));
}// elsewhere set up a bunch of variables
extract(doLoops(compact('list of variable names goes here')));Problem solved, now you are not including in a loop, but calling an
already compiled function, without adding TONS of lines of code to build
such an array. Its a short solution, to a problem that should probably lead
to a redesign of the functionality anyway.
Sorry that should be compact(array_keys($vars)), and same for the list
going in. Too many other languages lately!
Hi everybody,
I have an application where a small file is included at multiple places.
So far so good.
The problem is that this include consists in a small piece of code which is
inside a multi-level loop.The include is done about an average of 100.000 times .
When I manually replace the include with the code which is inside, the
performance boost is about a 50-100 factor..
(with opcache enabled, it is far worst without opcache).
I'm not surprised.
What's wrong with using PHP functions and/or classes? Use
'require_once' outside the loops, which brings in the file containing
the function/class, and then execute the function inside the loop.
Loading a file repeatedly incurs a lot of processing time to parse it
into opcodes, which, as you noticed, the opcode cache helps
significantly with. You are also already getting the performance
benefits of the file already being in RAM thanks to whatever OS level
in-memory file caching system your OS uses. Basically, your code is
(ab)using system caching as a performance crutch, which means it is time
to refactor your code.
Once you switch to using a function, there are additional techniques
that can be used to speed up the function itself - prefer PHP keywords
over PHP functions (e.g. array_key_exists()
is many times slower than
'isset'), find an alternate algorithm and/or shortcuts with the current
algorithm, write a PHP extension, etc.
The 'include' and 'require' keywords invite abuse similar to what you
just described and, when abused, will have performance metrics similar
to what you described. You aren't the first person to use those
keywords in a loop and likely won't be the last. The documentation
should be updated to reflect a preference for the 'require_once' and
'include_once' keywords, which would help to avoid the misunderstanding
that no one should use any of the code inclusion keywords in a loop.
Personally, I use 'require_once' exclusively for a wide variety of
reasons. I've rarely seen a need to use the other include/require
keywords. Most of the time I see 'include', I usually think, "That code
could be refactored to use 'require_once' and the author would be better
off for it."
--
Thomas Hruska
CubicleSoft President
I've got great, time saving software that you will find useful.
I have an application where a small file is included at multiple places.
So far so good.
The problem is that this include consists in a small piece of code which is
inside a multi-level loop.The include is done about an average of 100.000 times .
When I manually replace the include with the code which is inside, the
performance boost is about a 50-100 factor..
(with opcache enabled, it is far worst without opcache).The problem is that if I make a change within the code, I do have to not
forget to make changes a dozen of places...Could it be possible to add a directive such as :
inline < filename >
That includes the file inline within the compiled pseudo code (at compile
time) ...(it does not have a big importance for me that the target pseudo-code sets
the line number of all inlined instructions to the line number of the inline
directive)Am I the only people needing this feature ?
If you really need this feature, I suggest that you're using a
preprocessor to prepare the files before they're compiled by PHP. A
rather simple PHP script using preg_replace()
might already be sufficient.
--
Christoph M. Becker
Hi,
Hi everybody,
I have an application where a small file is included at multiple places.
So far so good.
The problem is that this include consists in a small piece of code which is
inside a multi-level loop.The include is done about an average of 100.000 times .
When I manually replace the include with the code which is inside, the
performance boost is about a 50-100 factor..
(with opcache enabled, it is far worst without opcache).
Despite the fact that your code really seems to be of bad quality as
others mentioned: Did you configure your opcache not to check the file
system each time if the file changed in the meantime?
Regards
Thomas