Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122113 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39933 invoked from network); 5 Jan 2024 01:56:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2024 01:56:13 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1704419804; bh=4sykGDcL02zbJfSaT8rC0YobGo4Z+K2BAKOKUvcTYUg=; h=Date:From:To:In-Reply-To:References:Subject:From; b=nEthbvFOk6QDgZ+XxwXEcI/2PsjWFhfHf7mmerrodZ+88cWGdRaCoiMacy/xhYz9/ I2vZ547x0N1yhfeEbNOVQ1ojx7+UDzHYUEkpN80SS9H8aw7RSBkKACiXEvMwyy25pA vEYUFjyBAyWEuEyLALv4alr9e7lFWPvmY7G5LtNKJvm3GUDl9VX2XnVT7GtPyhtU/m zNDiCFd1k9AJyAblpRQMa0v4Kp0lG9RdRgqEOsu6RF5rA+3GYZ0ebebxuQCoLSmSBZ 7BKS7/atKTerj+KcJv9VmpS1IGbfcdHVs//lKOvkX+7+dfOhVmbEs/qLQ6xI1mo95e CGmiEoVHyy1dg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E04CE180054 for ; Thu, 4 Jan 2024 17:56:43 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,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.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 ; Thu, 4 Jan 2024 17:56:43 -0800 (PST) Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40d41555f9dso10593825e9.2 for ; Thu, 04 Jan 2024 17:56:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704419770; x=1705024570; darn=lists.php.net; h=mime-version:subject:references:in-reply-to:message-id:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=Z9bn9wYpqwUl+s262CQCXHZYpjgtfiLAsyFAPYWQlVY=; b=Nwnwk6kptPAx1Z/mpQrkap9lBzpypm7ndfPZoIvRRRANh764/uUczbZUEuMlQyHcUy 653nEgqP2x4/NmovqeOVFmsp2lFXt8vg+IJlTpku8gDBOhB8vtGF7hTtNK+p9pxsSWpD mCUxih8ifZbo8F0RFGdwvLCYxEmpyfvjIj7frhT7HHi66KcGQLVGezgHpoQhXwWFsTNO Z6ALwRAZvynMixyBn4uZOfdw0JKFUpbpOp+N5lEuyhH8l8Qd/k5anrpkL1/1g5eIb6Pp sIoyMO5OJlmlhHU3rTCZpQDJN0OEU1GQY+9JonYgeBdZ3/k3f1hjo3k1QNKAn0NqkkTO +Ptw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704419770; x=1705024570; h=mime-version:subject:references:in-reply-to:message-id:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Z9bn9wYpqwUl+s262CQCXHZYpjgtfiLAsyFAPYWQlVY=; b=AcqNgoKLbq7CxmZauWv99UMVDVWlEHxK0VlsBpBCKil9HOAOQFAcROL12N9gjv90QN H7PHofgpGFu9HFxQ3KruFCiuFeeX6WshyJDY7uTqOgSKTGF9J/9P7WVMC1OjHVV87EOG mcwPyInZ23D04a1dob4c7lmE1eLHjkRZptn9zBDenSnUpqManP4CSGekkSuOR2zE6XCM SR80SkwJCOpVmDSmmCEz/g2hroehUcY/JUbYeqe6/efxI7gnu2+YQRLA7VL4NMP0T3ob W5eOc9vDVWKtWMm5DOwCQzvErWCvioPblSvOxqNs+nuLoxTczgadosb6B4DfgTncYT5n ge/Q== X-Gm-Message-State: AOJu0YxwvbAVllBy4mwOAvSNfSAgEaC5qOnjviXgmYgod/jRD2kqY4Fv AipHD1pueNDqFtWho7YNf6fICxPbyxgZHw== X-Google-Smtp-Source: AGHT+IEDtg4LoMjhu7SHVKTeJwf6tuzz/KAE+9pN2WV8Eyw6TMRVCcx40P8/ugZb5qIzE0rIhATuIw== X-Received: by 2002:a05:600c:3acf:b0:40d:3112:a2d7 with SMTP id d15-20020a05600c3acf00b0040d3112a2d7mr749740wms.187.1704419770041; Thu, 04 Jan 2024 17:56:10 -0800 (PST) Received: from [127.0.0.1] ([128.116.205.77]) by smtp.gmail.com with ESMTPSA id n31-20020a05600c501f00b0040d579817b0sm26656wmr.15.2024.01.04.17.56.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Jan 2024 17:56:09 -0800 (PST) Date: Fri, 5 Jan 2024 02:56:07 +0100 (GMT+01:00) To: =?UTF-8?Q?K=C3=A9vin_Dunglas?= , internals@lists.php.net Message-ID: <3828a559-5dae-44d1-96ce-dff1a8b07c18@gmail.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_23_81118441.1704419767091" X-Correlation-ID: <3828a559-5dae-44d1-96ce-dff1a8b07c18@gmail.com> Subject: Re: [PHP-DEV] RFC proposal: worker mode primitives for SAPIs From: daniil.gentili@gmail.com (Daniil Gentili) ------=_Part_23_81118441.1704419767091 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, I've been lurking around in the discussion for a while, and I'd like to chime in now since the main, glaring issue with this proposal has only been explicitly mentioned by Rowan Tomminis, with that subthread quickly degenerating with misguided replies. PHP as a language absolutely needs a persistent worker mode like in all other backend languages, which will greatly improve performance by avoiding all overhead related to request initialization. However, the current proposal of adding support for worker mode SAPIs based on the usual PHP superglobals is fundamentally incompatible with concurrency (aka async, aka swoole, amphp, fiber-based concurrent runtimes in general), and I believe would steer the PHP ecosystem in the wrong direction. Rowan already offered a nice example of why earlier in the thread, but as an explanation to those that may not have understood that, so I whipped up a briefer at https://gist.github.com/danog/f0e9103e6a7e1bcec1b92c411a3c4607 So no, unlike what I've read in another message here, "The nodejs curse [...] where async and co may actually slow down your whole node." is simply false for the majority of modern usecases, because the majority of modern applications is I/O-intensive, not CPU-intensive. Implementing a worker API the way it is proposed is fundamentally incompatible with userland cooperative schedulers aka event loops like amphp or reactphp, and each request would be handled, just like in php-fpm, in a separate OS thread, not in a much more efficient userland thread. Cooperative schedulers implemented outside of PHP in the form of an extension such as swoole would also not be able to easily make use of the initially proposed API to handle requests in separate coroutines, even if the handle_request function was intercepted by swoole to return control to the swoole event loop (which would already make it superfluous as a PHP API), since the proposal involves messing around with the usual superglobals, which as mentioned by Rowan would require manual copying of the superglobals before using them in a newly spawned swoole coroutine. I believe that a much, much better way of implementing a worker mode in PHP core would be to provide a fiber-aware function/static method returning, upon receiving a request, a class implementing the PSR-7 RequestInterface. Fiber-aware here means that the function should behave in a way similar to how all native PHP I/O functions should behave in an upcoming Native Event Loop RFC I'm planning to make. The idea here is to make all I/O related functions, including this new wait_request function, completely async in a manner that is compatible with fibers and pure PHP event loops like amphp. It would essentially work as follows: an event loop like uv is integrated within PHP itself at least for the native stream wrapper (I.e. all filesystem and network functions, file_get_contents and so on). Extensions will be provided an API to interact with the event loop. I'm currently finalizing the details, and might probably make a first RFC in the coming weeks, suffice to say that everything will work in a manner very similar to how async works in both the ext-pq and php-tokio extensions, albeit with fiber integration: a separate event loop (let's call it native) is started in another OS thread (like with ext-pq or php-tokio, WITHOUT the need for ZTS, as no PHP data structures are modified in the native event loop), events are sent via a single descriptor from the native event loop to the userland event loop (I.e. amphp). If running inside a fiber, tasks are queued to the native event loop and the fiber is suspended with a NativeSuspension, then resumed by the userland event loop. If not running inside a fiber, tasks are queued to the native event loop and control is returned to the userland event loop. Essentially, my proposal is almost exactly what is already being done in https://github.com/danog/php-tokio, but library-agonstic and implemented inside of PHP itself, perhaps even using Rust and tokio as well. I feel that the current direction of the discussion of PHP worker modes is wrong, as it is mostly ignoring the presence of a perfectly usable concurrency API in PHP (fibers): this has potential to cause a big split in the PHP ecosystem, unless PHP finally fully embraces concurrency and async with a native event loop proposal, like the one I intend to present soon. I hope that further discussion of a worker mode continues with an eye to the future with native concurrency :) Regards, Daniil Gentili. P. S. Just in case to avoid confusion for some, frankenphp does NOT add native concurrency to php using goroutines, as control to the go event loop is only returned when calling functions like header or echo which cause a suspension of the goroutine because an I/O operation is made on the go side, but 1) That's incompatible with fibers, and not just because of the minor go compatibility bug which is being fixed, but because the go event loop is not integrated with any php event loop, and thus does not know to return control to any eventual php fiber when needed, and will just block all PHP fibers until that single go I/O operation is done (unlike in php-tokio, where the rust tokio event loop is fully integrated with the php event loop (currently just revolt)). 2) It doesn't even replace the native stream wrapper with go-backed async I/O like swoole does (though both this and php event loop integration like in php-tokio is fully possible in frankenphp with a bit of work, I could actually lend a hand if needed...) ------=_Part_23_81118441.1704419767091--