Free each thread's strtod Bigint pool - #21
Closed
EdmondDantes wants to merge 1 commit into
Closed
Conversation
Bfree recycles a Bigint onto a per-thread freelist instead of releasing
it, and zend_shutdown_strtod() only ever reaches the main thread's pool.
Under ZTS every other thread that formatted or parsed a double left its
Bigints behind when its storage went: measured as 68 bytes definitely
lost per worker thread for a single sprintf('%6.3f'), scaling linearly
with the number of threads.
The freelist teardown now takes the state to free, because TSRM runs a
thread's storage destructor from whichever thread performs the shutdown,
and EG() there belongs to somebody else.
Author
|
Closing: this is upstream php-src behaviour, not a fork concern, and this repository is not where we carry such patches. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was broken
Bfree()does not release aBigint; it pushes it ontofreelist[k], which under ZTS isEG(strtod_state).freelist— one pool per thread. The only thing that empties a pool isdestroy_freelist(), reachable solely throughzend_shutdown_strtod(), which runs once, on the main thread.executor_globals_ctorzeroes the state for every new thread andexecutor_globals_dtornever touched it.So every thread that formatted or parsed a double leaked its pooled Bigints when its TSRM storage went. Measured with a
ThreadPoolworker doing onesprintf('%6.3f', 1.5):— a 36-byte
Balloc(1)fromd2b()and a 32-byteBalloc(0)fromrv_alloc(). Four workers, one float each: 272 bytes in 8 blocks, exactly linear. It is small per thread and unbounded per process: a pool that rotates its workers pays it again on every rotation.The change
destroy_freelist()andfree_p5s()take the state to free instead of reaching throughEG(), andexecutor_globals_dtor()frees the departing thread's pool. The state has to travel as an argument: TSRM runs a thread's storage destructor from whichever thread performs the shutdown, soEG()there belongs to somebody else.zend_shutdown_strtod()keeps its signature and now goes through the same path for the main thread. Running it before the destructor is safe — the teardown NULLs both lists, so the second pass finds nothing.The two
#undefs before the teardown are needed becausefreelistandp5sare macros overEG(strtod_state)for the rest of the file, and they would otherwise swallow the field names instate->freelist.Measurements
Zend/tests: 5398 passed, 129 skipped, 0 failed.ext/standard/tests/math: 164 passed, 0 failed. Build is ZTS, debug.Found while chasing a leak in another project against this fork; the same code is in php-src master, so this is not fork-specific.