Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128944 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id C30F71A00BC for ; Fri, 24 Oct 2025 07:30:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1761291041; bh=AdkhB4j0/5hZaNzB15t0jqiqkzm3Fh81tJt9BH5tbSs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=HB95MjfShreS7izl+qeqgsSKkzTX937x2g04sTFxRrDYOOfF4WREgMeSArur/lO5T xlP/LtDit3GNgC35R3gaG3/W7exaygsf1uPzlwHTuyGb+PkL8Huil69202FqqH54Eo dPrYrA8J+/a1N8TOHmaYTrJccJsguTjL+4E+Rj5HxBk7GnEkovX7eoZ6AMnHyBhkiE ginsjiZkj6m/gyw9/XNiK8QAdpL9AECtewqEp195mMspCKfG8QQPDfKXxKXqQIJsUi P65Z1YJRKw8w0EeTdgB6+j6bEzUs84a36DJCi5HHBK8qHPMYDSDNgVueKFdP3wlUMx Hd2mwEcJTd/Mw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5AFAC180084 for ; Fri, 24 Oct 2025 07:30:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-vs1-f50.google.com (mail-vs1-f50.google.com [209.85.217.50]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 24 Oct 2025 07:30:40 +0000 (UTC) Received: by mail-vs1-f50.google.com with SMTP id ada2fe7eead31-5db37a935f1so567760137.1 for ; Fri, 24 Oct 2025 00:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761291034; x=1761895834; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=AdkhB4j0/5hZaNzB15t0jqiqkzm3Fh81tJt9BH5tbSs=; b=Z7IOsHQFtABGoH4cLKwQNEP1sQ1X9kaz5KwIIiStYldqGjLMtkLnhuwsuh3LlsLqvu kQAJ+oUekfAmhG2LQAisPn1pU88AcRwl29bqYwbUQRvMECyBq4yNvh8M8fzF1Xu7Hw3P sV84qkM/G+qCgMWRum2x2VMnZ+3jT7r07WeYUNJJm2PlD/S8BEmxv0Z8OY5pTIvr94Zg GwJEzwpNnkHrXxlTR4zMyjyllzGAqY4Foir46/Bo7i2eGus6h5utQoP+DmcjQB3r/coo 37T8lyGDbPKVTDD7aR/3AMo9Lr0bhDfRSyKTN9VSZS0KMlCDc7cobfltosnJ2C0xS0Rr lFwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761291034; x=1761895834; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AdkhB4j0/5hZaNzB15t0jqiqkzm3Fh81tJt9BH5tbSs=; b=ZjKGIk+9qTqKRj7sjqmCNapjSqEMTWFl9TVBYNp8jSeN1XWvG2m2Ku4KN2KQqFu/xJ OBmDaeFKWsltW9LWleGZFdZXFOxAseaTGe6VsiZvuhE+3kKiSj7nrvHdGwsfHS2mrAlg FcWODQGt0vKfuWORiaXNQgr4Cm15Zu5diN5c0cBB0r9wpf2kFEoro8VJkvQw29tRxkTn PqixhX6H+pJ0oxBH3IBqQmJwJIMhzc9Nj1fFAW1ULlwhnykvsGikLUkJhxzmptKDdVgH gw2ezypvA5w5Dxsuffqy3s5mAAh7oIqM0qVDbA4R9NgsryLLzmVWlGA0Rngw0PjaH1LC 3Cbg== X-Gm-Message-State: AOJu0Yz4jcgdfagkzdRfJsEWYwPRj09yqngbCifcuaqhRQJq36JiCwkY 6cl8ysYi3Bda9vbg5uzAbWL4aQIc6Q0+lu2nIgEkycVseKkVBMFelty3QeBHWpOmNQ/TNIvHsHf MewpDQi9+CaNoHMGf22vcUF+5Ii4vTGI= X-Gm-Gg: ASbGnct28NLyb0tDDMvoIKihV9f4o2e3U422mAm6swF4TdtKnpxBpeNE7b0i29wH/rp p2lwyI/RePxKzM/T3l7e7Vaf2VNIka5IwmQMUpE9e1nVg58rShVNt0t4yUGEwM4zQnhU2fuuUCO 95P04n6xca5yMICl3sh5F3g+b8n8DpoiWzrKDxPMgYRAIvv/1OyRAvq2bdW3fdhM+GS41bC7wQW 7ORI19XupkUy9demFj+lclxVnzx5hnmJRDxtepf/E4j5JG/o8ata0nAb/Mz+Y40wc5Y+G8Id/pQ xaN+YWIhddX9AHC7cAXyII3Obrsp X-Google-Smtp-Source: AGHT+IGfgx5cad0JWvcAwsHFFe/Bm8jQ8YdF/Uqeg4A4jaSZrCJEgB2Mf2c0Fcxlx8Lb5WKSSYoB3MtqHC0L0w8aZbU= X-Received: by 2002:a05:6102:b16:b0:5d5:f6ae:38d2 with SMTP id ada2fe7eead31-5db3fa79c2fmr265335137.39.1761291034362; Fri, 24 Oct 2025 00:30:34 -0700 (PDT) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Oct 2025 10:30:23 +0300 X-Gm-Features: AWmQ_bnqLgJDe7gmBQ5mehmKJ3VSTJJK-sX9D_jBwc6qId3RfbBTO2SHAeFGLS0 Message-ID: Subject: [PHP-DEV] Re: PHP and parallelism To: Rob Landers Cc: php internals Content-Type: multipart/alternative; boundary="000000000000730e760641e28691" From: edmond.ht@gmail.com (Edmond Dantes) --000000000000730e760641e28691 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable But it's not a parallelism. =D0=BF=D1=82, 24 =D0=BE=D0=BA=D1=82. 2025=E2=80=AF=D0=B3., 09:43 Rob Lander= s : > > > On Fri, Oct 24, 2025, at 08:34, Edmond Dantes wrote: > > Hello everyone, > > In the TrueAsync RFC 4 thread, John Bafford raised a question about > parallelism. > I would like to share my thoughts and highlight this branch of the > discussion. > > # What is parallelism in the context of coroutines? > Parallelism for coroutines means the ability to execute a coroutine in > one thread or another (but not simultaneously). > In this case, the Scheduler not only manages the execution order of > coroutines but also decides which thread from the thread pool should > be assigned to a coroutine. > > # What is the problem? > > If two different coroutines run in separate threads and write to the > same memory region =E2=80=94 that is, to the same PHP object =E2=80=94 it= will lead to > unpredictable consequences. > > The issue is that PHP does not control memory access in such cases and > effectively allows the programmer to shoot themselves in the head. > > How can this be solved? > > ### Object transfer policy > > The object transfer policy defines how a PHP object can be safely > passed to another coroutine. > Possible transfer methods: > 1. Via parameters > 2. Via context > 3. Via channels > > All these methods can be viewed as variations of passing objects > through channels (similar to the Erlang model =E2=80=94 no direct sharing= ). > > The same policy applies to all methods. > > An interface is defined to guarantee an object=E2=80=99s =E2=80=9Cthread = safety.=E2=80=9D For > example, if a coroutine receives an object parameter without this > interface, an error occurs. > > In PHP, there are several possible ways to achieve thread safety: > 1. **Ownership transfer**, as in C++/Rust =E2=80=94 the object leaves the > current scope and becomes owned by the coroutine. > 2. **Proxy-based access**, using a special constructor that creates a > proxy object for access to the real one (similar to actors in Swift). > 3. **Shared access** with a built-in mutex. > > This approach requires minimal changes to the language syntax =E2=80=94 i= n > fact, none at all. > From a performance standpoint, it is perfectly acceptable for PHP to > validate function parameters, so no significant overhead should be > expected. > > The main challenge of parallelism lies in making PHP=E2=80=99s internal s= tate, > garbage collector, and memory manager thread-safe. > > # How does this relate to TrueAsync? > > At the moment, TrueAsync does not perform additional checks when > passing parameters to coroutines. > This is a problem =E2=80=94 because once PHP introduces concurrency, > developers will start freely using references (&reference) and shared > objects. > > When multitasking is later added to coroutines, it will break existing > PHP code that has already been written. > > Therefore, if PHP truly aims to become multithreaded, its memory > ownership model must be carefully designed today. > > --- > Best Regards, Ed > > > I=E2=80=99m not sure this is the place to discuss it, nor the time. But C= # is > cooperative multitasking on top of a VM which is preemptive multitasking. > This basically allows you to run multithreaded without worrying about > =E2=80=9Cshooting yourself in the foot=E2=80=9D by accessing the same mem= ory, most of the > time. In other words, there is plenty of prior art out there and ways to > solve this problem. It doesn=E2=80=99t mean we need to solve it today. Tr= ying to > solve a problem that doesn=E2=80=99t currently exist and =E2=80=9Cmight= =E2=80=9D be a problem later > is exactly how you end up with overengineered solutions that never actual= ly > solve the problem. > > =E2=80=94 Rob > --000000000000730e760641e28691 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
But it's not a parallelism.=C2=A0

=D0=BF=D1=82, 24 =D0=BE=D0=BA=D1=82. 2025=E2=80=AF=D0=B3., 09:43 Rob Land= ers <rob@bottled.codes>:
<= /u>


On Fri, Oct 24, 2025, at 08:34, = Edmond Dantes wrote:
Hello everyone,

In the TrueAsync RFC= 4 thread, John Bafford raised a question about
parallelism.
I would like to share my thoughts and highlight this branch of the di= scussion.

# What is parallelism in the context of = coroutines?
Parallelism for coroutines means the ability to execu= te a coroutine in
one thread or another (but not simultaneously).=
In this case, the Scheduler not only manages the execution order= of
coroutines but also decides which thread from the thread pool= should
be assigned to a coroutine.

# Wh= at is the problem?

If two different coroutines run= in separate threads and write to the
same memory region =E2=80= =94 that is, to the same PHP object =E2=80=94 it will lead to
unp= redictable consequences.

The issue is that PHP doe= s not control memory access in such cases and
effectively allows = the programmer to shoot themselves in the head.

Ho= w can this be solved?

### Object transfer policy

The object transfer policy defines how a PHP object= can be safely
passed to another coroutine.
Possible tr= ansfer methods:
1. Via parameters
2. Via context
<= div>3. Via channels

All these methods can be viewe= d as variations of passing objects
through channels (similar to t= he Erlang model =E2=80=94 no direct sharing).

The = same policy applies to all methods.

An interface i= s defined to guarantee an object=E2=80=99s =E2=80=9Cthread safety.=E2=80=9D= For
example, if a coroutine receives an object parameter without= this
interface, an error occurs.

In PHP= , there are several possible ways to achieve thread safety:
1. **= Ownership transfer**, as in C++/Rust =E2=80=94 the object leaves the
<= div>current scope and becomes owned by the coroutine.
2. **Proxy-= based access**, using a special constructor that creates a
proxy = object for access to the real one (similar to actors in Swift).
3= . **Shared access** with a built-in mutex.

This ap= proach requires minimal changes to the language syntax =E2=80=94 in
fact, none at all.
From a performance standpoint, it is perfec= tly acceptable for PHP to
validate function parameters, so no sig= nificant overhead should be
expected.

Th= e main challenge of parallelism lies in making PHP=E2=80=99s internal state= ,
garbage collector, and memory manager thread-safe.
# How does this relate to TrueAsync?

= At the moment, TrueAsync does not perform additional checks when
= passing parameters to coroutines.
This is a problem =E2=80=94 bec= ause once PHP introduces concurrency,
developers will start freel= y using references (&reference) and shared
objects.

When multitasking is later added to coroutines, it will bre= ak existing
PHP code that has already been written.
Therefore, if PHP truly aims to become multithreaded, its memor= y
ownership model must be carefully designed today.
---
Best Regards, Ed


I=E2=80=99m not sure this is the place to discuss it, = nor the time. But C# is cooperative multitasking on top of a VM which is pr= eemptive multitasking. This basically allows you to run multithreaded witho= ut worrying about =E2=80=9Cshooting yourself in the foot=E2=80=9D by access= ing the same memory, most of the time. In other words, there is plenty of p= rior art out there and ways to solve this problem. It doesn=E2=80=99t mean = we need to solve it today. Trying to solve a problem that doesn=E2=80=99t c= urrently exist and =E2=80=9Cmight=E2=80=9D be a problem later is exactly ho= w you end up with overengineered solutions that never actually solve the pr= oblem.=C2=A0

=E2=80=94 Rob
--000000000000730e760641e28691--