php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81090 Typed property performance degradation with .= operator
Submitted: 2021-05-30 12:41 UTC Modified: 2021-05-31 10:17 UTC
Votes:13
Avg. Score:4.8 ± 0.6
Reproduced:11 of 11 (100.0%)
Same Version:11 (100.0%)
Same OS:11 (100.0%)
From: dktapps at pmmp dot io Assigned:
Status: Closed Package: Performance problem
PHP Version: 7.4.19 OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dktapps at pmmp dot io
New email:
PHP Version: OS:

 

 [2021-05-30 12:41 UTC] dktapps at pmmp dot io
Description:
------------
There appears to be a change in the behaviour of .= with typed properties, as compared to typeless properties.


The below script demonstrates a significant performance degradation with typed properties (in the ballpark of 2 orders of magnitude).

Test script:
---------------
https://gist.github.com/dktapps/8a5d839f4c884f92d0b56f04c3b7d7d4

Expected result:
----------------
The time per operation should only be slightly slower for typed properties than typeless properties (like it is for other things, like plain assignments).

Actual result:
--------------
The time per operation is 2 orders of magnitude worse on the first loop than the second one.

On my machine, the script produces the following:
Typed property time: 8701.32 ns/op
Typeless property time: 211.446 ns/op

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-05-30 13:28 UTC] dktapps at pmmp dot io
Profiling shows that the vast majority of the CPU time is spent in concat_function(), but only for the typed property.

This bug also persists on PHP 8.0 with function JIT enabled.

The performance degradation is exponential with higher loop count, which leads me to believe that every .= on a typed property is triggering a full string copy for some reason, despite the string's refcount being 1.

With type (7m 30s, 1,000,000 loops): https://user-images.githubusercontent.com/14214667/120105960-05484200-c153-11eb-8540-00e8ea39e0a7.png
Without type (<1s, 1,000,000 loops): https://user-images.githubusercontent.com/14214667/120105988-29a41e80-c153-11eb-8337-ed91a41fe1fc.png
 [2021-05-31 10:17 UTC] nikic@php.net
-Status: Open +Status: Analyzed
 [2021-05-31 10:17 UTC] nikic@php.net
This happens because typed properties perform binary operations into a temporary first, so that the original value is retained if a TypeError occurs.
 [2021-05-31 10:34 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/82f6f6da67a265170fe533d97edb59520da0024f
Log: Fixed bug #81090
 [2021-05-31 10:34 UTC] git@php.net
-Status: Analyzed +Status: Closed
 [2021-05-31 10:40 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/445b6492280cdf9eec6a762d790fcbee964ec5f7
Log: Fix bug #81090 in JIT as well
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC