You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A very large amount of pthreads' code is dedicated solely to copying code from the origin thread.
Right now, class copying takes place on 3 occasions:
When a thread starts, all classes, functions and constants loaded on the parent thread (depending on PTHREADS_INHERIT_* flags, default is ALL) are copied to the child thread.
When a thread starts, the thread's own class is copied from the parent (if it wasn't already). This always happens, regardless of the INHERIT flags used.
When a task is run on a Worker, the task's class is copied from the parent. This always happens, regardless of the INHERIT flags used.
When a Threaded object assigned by thread1 is read by thread2, the Threaded object's class is copied from thread1 to thread2. This always happens, regardless of the INHERIT flags used.
Reasons to stop doing this:
It's extremely costly (adding significant delays to thread start)
It can't make clean or complete copies. Defaults for stuff like static properties are unavailable after those properties are modified, and may contain data types that can't be easily copied (e.g. objects, arrays, resources). (no longer applicable to PHP 8.1)
It's a massive maintenance burden, accounting for the vast majority of compatibility issues that arise on any new PHP version.
It wastes a ton of memory - the copied classes might not be needed by the target thread anyway.
PocketMine-MP makes little to no usage of this code, preferring to rely on autoloading for better reliability, predictability, performance and memory usage (especially, opcache keeps memory usage low with cached code, if autoloading is used).
It worsens stability - there's so many things that can and do go wrong due to small undocumented changes in php-src.
Reasons to keep it:
Anonymous Threaded classes can't be supported without it, since they can't be autoloaded. Dropping code copying means we would no longer be able to have anonymous Threaded classes. I don't consider this to be a big enough reason to keep the gigantic maintenance burden of class copying around.
Obstacles
Point 2 above is a problem, since there's currently no other way for the Thread class to get into the actual thread right now other than having it copied from the parent. This could be solved by implementing something like Introduced Thread::setAutoloadFile() #23 .
Compatibility
Since PocketMine-MP doesn't currently use code inheritance at all (PTHREADS_INHERIT_NONE or PTHREADS_INHERIT_INI is typically used), it currently relies almost entirely on the autoloader anyway, so this change would have basically zero impact on compatibility.
The text was updated successfully, but these errors were encountered:
It turns out there's another reason this is a problem: it becomes impossible to use threads within the scope of a single file (script), since the classes declared in the script won't be autoloadable without re-running the code that led to the creation of the thread in the first place.
ext-parallel might suck for a number of reasons, but one thing it really got right is the closure-based execution units.
A very large amount of pthreads' code is dedicated solely to copying code from the origin thread.
Right now, class copying takes place on 3 occasions:
Reasons to stop doing this:
It can't make clean or complete copies. Defaults for stuff like static properties are unavailable after those properties are modified, and may contain data types that can't be easily copied (e.g. objects, arrays, resources).(no longer applicable to PHP 8.1)Reasons to keep it:
Obstacles
Compatibility
Since PocketMine-MP doesn't currently use code inheritance at all (PTHREADS_INHERIT_NONE or PTHREADS_INHERIT_INI is typically used), it currently relies almost entirely on the autoloader anyway, so this change would have basically zero impact on compatibility.
The text was updated successfully, but these errors were encountered: