Not a bug Use global namespace version of functions to allow bytecode optimizations

Xon

Well-known member
Affected version
2.2.5
php supports converting a number of functions from function calls into specific bytecode as a fast-pass. This however requires the function be pulled from the global namespace, and in XF virtually call code is called from non-global namespace, which defeats this optimization.

A simple way would be to include something like this after the namespace declaration, in each file;
PHP:
use function strlen, is_null, is_bool, is_long, is_int, is_integer, is_float, is_double, is_string, is_array, is_object, is_resource, is_scalar, boolval, intval, floatval, doubleval, strval, defined, chr, ord, call_user_func_array, call_user_func, in_array, count, sizeof, get_class, get_called_class, gettype, func_num_args, func_get_args, array_slice, array_key_exists;
There doesn't appear to be any nice way to append this list into say an include file :( php just throws a "The use statement with non-compound name" error with php 7.x

Using this regex, there is about 2600 hits in the src/XF, some may be false positives.
Code:
[^\\>a-zA-Z_-](strlen|is_null|is_bool|is_long|is_int|is_integer|is_float|is_double|is_string|is_array|is_object|is_resource|is_scalar|boolval|intval|floatval|doubleval|strval|defined|chr|ord|call_user_func_array|call_user_func|in_array|count|sizeof|get_class|get_called_class|gettype|func_num_args|func_get_args|array_slice|array_key_exists)\s*\(
 
Ironically, this is another thing we're already aware of and been discussing internally and not currently tracking as a bug.

We actually had a pull request outstanding during the development of XF 2.2 which only 6 days ago we closed because we didn't progress it further and the PR would now be horrendously out of date.

It is, still, however on our radar and I imagine we will revisit it.

There was a discussion about using the global namespace across all native function invocations versus the specific whitelist. It seems there is a slight performance benefit approaching it like that for any native function rather than just the specific functions listed.

Thanks for the proposal re: the use function declaration. That may be an interesting approach if it could be generated automatically for only the function calls in each file.

For now, we'll close this as we're tracking it externally, but thanks again for bringing it up.
 
As of XF 2.2.7 we have a command which generates use function lines automatically with the functions listed being enumerated from usage in the class itself and within the whitelist of native functions where this should be of benefit.

PHP:
<?php

namespace XF\Entity;

use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;

use function count, is_array, is_int;

The command is: xf-dev:class-use-function <addon>, e.g. xf-dev:class-use-function XFMG though if you're running this on your own add-ons proceed with caution if you already use use function statements at all.

For example, we don't mess with existing use function lines which contain aliases, e.g.
Code:
use function in_array, preg_match AS pm;
So a new use function line gets added and therefore this may get turned into:
Code:
use function in_array, preg_match AS pm;
use function in_array;
Which is a fatal error.

On the whole, we're not necessarily expecting people to use this on their own add-ons, nor necessarily supporting that unless there are some obvious bugs to fix or improvements we can make and I suspect most people probably just inline them with the backslash prefix anyway.
 
Not a particular fan of the idea of patching vendor stuff. Hopefully over time (particularly as we bump our version requirements) more dependencies will handle that themselves.
 
Top Bottom