Problem/Motivation

=== NULL vs is_null, which one is better and recommended to use in Drupal core?

In PHP 7 (phpng), is_null is actually marginally faster than ===, although the performance difference between the two is far smaller.

PHP 5.5.9
is_null - float(2.2381200790405)
=== - float(1.0024659633636)
=== faster by ~100ns per call

PHP 7.0.0-dev (built: May 19 2015 10:16:06)
is_null - float(1.4121870994568)
=== - float(1.4577329158783)
is_null faster by ~5ns per call

See https://www.php.net/manual/en/function.is-null.php#117344

Proposed resolution

Remaining tasks

  • Do a benchmark
  • Choose which one to use
  • Replacing

User interface changes

API changes

Data model changes

Release notes snippet

Comments

jungle created an issue. See original summary.

cilefen’s picture

Component: php.module » base system
Issue tags: +Performance
init90’s picture

+1 to is_null, for me it more readable variant

jungle’s picture

StatusFileSize
new7.71 KB
new7.7 KB
new936 bytes

Used phpbench, https://phpbench.readthedocs.io/en/latest/index.html

Command used:

phpbench  run  CheckNullBenchmark.php --report=default

Reports and the CheckNullBenchmark.php file attached.


Tested against PHP 7.3.9 and 7.4.2. is_null seems slightly better. Or no noticeable difference.

personally, I prefer === NULL, as I prefer === than == when they two are interchangable under a context.

Would vote for won't fix.

jungle’s picture

Status: Active » Needs review
jungle’s picture

Or rescope this to replacing is_null($variable) to \is_null($variable)?

See a link @alexpott shared on slack to @catch https://twitter.com/Stof70/status/1251236840876761089 or Null checks are a performance bottleneck of this library #96

jungle’s picture

StatusFileSize
new11.18 KB
new1.15 KB

No obvious difference from the benchmark, among ===NULL, is_null() and \is_null()

Theoretically, under the namespaced context, \is_null() is better than is_null() probably,

  • to is_null(), looking up function is_null() at the current namespace first, not found, continue and found \is_null(), and executing,
  • to \is_null(), no extra lookup
andypost’s picture

I bet it expected behaviour of PHP since namespaces started to work - lookup in namespace first and then in global scope

Interesting to check performance impact of prefixing all "global" functions... Better to explore it in separate issue

alexpott’s picture

This is likely to only have an impact in functions that are called many many times - I don’t think a blanket replace is in order here - but many we could identify any low level global function calls where this is significant.

dqd’s picture

#7 - Look at this and this video (time set to the important point) demonstrating (in another case) how is_array vs \is_array() or is_null() vs. \is_null() can be a huge difference in performance.

init90’s picture

Here is a discussion about implementing a fully qualified function in Typo 3: Should we use fully qualified function (FQN) calls like \is_array in the core?

cilefen’s picture

@init90 I feel like there is such an issue for Drupal but I couldn't find it in a quick search.

catch’s picture

From one of those links:

Alternatively you can declare all PHP build in function used in the file like:
use function is_array;

This might be a nicer way to do it than \is_array() everywhere.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work

Think it would be nice to maybe have a phpcs check? Should this be a policy idea maybe?

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.