Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122114 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54462 invoked from network); 5 Jan 2024 06:55:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2024 06:55:49 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1704437780; bh=GGXf6FD76UwzLTsqz+Yi7H1P2OI7p/hJgtlMkNFwn3c=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=OnZ3dZ5Fqu+h0+0UxBs+j00hUcZ4F8OaV1izUMqLrG6sRuKc9axnIxa6AZD7lkk4w vOEXLVfvrfArhhNYr+2pupAidkhYzwI/zIzFNFFRp827wDSH4m/C4rXITahRrTp5IM Tsc6Dssmplw/CYaFTCAwUYlzaUcTkV/8vBdhnWWjSZl6SNAQPkPrKexAanRPQPNkhD tyfDGHLrbRal1t5jFsS5PD/ACPrlDdMb7+r6ks2v3K1brVHCz0CHAnAAKLB6kdLdO8 4vno8RW6o93tyaepNUx2jR8KH4zxwhnzZW9IGk8euQatgYuq3hXz8lRJa5ByfgHsg3 gmUNh0r2vaz7w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0CCEF180059 for ; Thu, 4 Jan 2024 22:56:20 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 22:56:19 -0800 (PST) Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-6dc1fdc19b6so812494a34.0 for ; Thu, 04 Jan 2024 22:55:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704437747; x=1705042547; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GGXf6FD76UwzLTsqz+Yi7H1P2OI7p/hJgtlMkNFwn3c=; b=XplgSEt75yx4/+Nh6S+kk+VlUj2z7Gg3TLZ5zNU5UGm6nTkP81ddKfA07Dedudk/G0 IHcJGbOsoGcq3DIMiXv47u/MC8nmmmdOkJmTBq/t7Uu+KMsHJJdfTrI4gKI3jKXD+t6s f8j8RpTgAPV6mBA0VRilw65vPmsP11krKVUSLAUdU6BfNMvsGqvRmclfg3wnepURKS9I sa8uZl9mVn8MRktwhJXwEALny35+Epp7r95pXdZE4uiD0pDADF0Fr//jPLjiH8288693 o9y6+jN56LMmT23Egt3cwMJCpL94OfIqxPjWbyAl5F/x+JIWrpSyxTrxyJN5vfZsoQ/e ntuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704437747; x=1705042547; h=content-transfer-encoding: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=GGXf6FD76UwzLTsqz+Yi7H1P2OI7p/hJgtlMkNFwn3c=; b=frDvBwPX1XzhZPGH3DwPNmrwZhqHRMZ0LMNDFe+2EcpzOJGVc6W8Epzn3esiLnNFBQ vR0nUFft1ZIgpUWAhfql7xDQDyTLzpHhbb6TOZM7u7ntaeykBHCURLfgv4qHJxC5bJXZ hfNvvjRoAEWQA+lwWx6m/w0vLB1lquwrBjbb0DmuRlwbEMYn+V58q6M3Jt4Wfi5SdHu8 rC2NOihcNnz8q9mVaCfagSik0ma08ZAjV/8ew98HRUGr/jeP2AorUD6hyWoghLJISp5L hI9payEy1B01DX3Yyk/I5hsluo8Zhcrh0Co7+EXo84UFZbG2sVkNfHRWRQXmiGCgIjMc tQrQ== X-Gm-Message-State: AOJu0YxzTAm74AkJCyKWWKLst0b3cZ3yfKhi095Hf6q3zOvUoYod3Oma pKkUQ4KptO3QCjPDgvfLaisz7mRgSZ/z/gr5UTi4QdyVMifAVg== X-Google-Smtp-Source: AGHT+IEB87zce6dPn8QmdPrLhOGd+3kNe/dhLhOtLTSxnpAU3UwA7p113pLeK3+c0Z167PmH24xuQ0+z4+7O8Gvx/rM= X-Received: by 2002:a05:6830:18d1:b0:6d8:74e2:c092 with SMTP id v17-20020a05683018d100b006d874e2c092mr1909367ote.68.1704437746997; Thu, 04 Jan 2024 22:55:46 -0800 (PST) MIME-Version: 1.0 References: <3828a559-5dae-44d1-96ce-dff1a8b07c18@gmail.com> In-Reply-To: <3828a559-5dae-44d1-96ce-dff1a8b07c18@gmail.com> Date: Fri, 5 Jan 2024 07:55:34 +0100 Message-ID: To: Daniil Gentili Cc: =?UTF-8?Q?K=C3=A9vin_Dunglas?= , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC proposal: worker mode primitives for SAPIs From: landers.robert@gmail.com (Robert Landers) On Fri, Jan 5, 2024 at 2:56=E2=80=AFAM Daniil Gentili wrote: > > Hi, > > I've been lurking around in the discussion for a while, and I'd like to c= hime in now since the main, glaring issue with this proposal has only been = explicitly mentioned by Rowan Tomminis, with that subthread quickly degener= ating with misguided replies. > > PHP as a language absolutely needs a persistent worker mode like in all o= ther 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 bas= ed on the usual PHP superglobals is fundamentally incompatible with concurr= ency (aka async, aka swoole, amphp, fiber-based concurrent runtimes in gene= ral), 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/f0e9103e6a7e1bcec1b92c411a3c460= 7 > > > > 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 a= pplications is I/O-intensive, not CPU-intensive. > > > Implementing a worker API the way it is proposed is fundamentally incompa= tible with userland cooperative schedulers aka event loops like amphp or re= actphp, and each request would be handled, just like in php-fpm, in a separ= ate OS thread, not in a much more efficient userland thread. > > Cooperative schedulers implemented outside of PHP in the form of an exten= sion such as swoole would also not be able to easily make use of the initia= lly proposed API to handle requests in separate coroutines, even if the han= dle_request function was intercepted by swoole to return control to the swo= ole event loop (which would already make it superfluous as a PHP API), sinc= e the proposal involves messing around with the usual superglobals, which a= s mentioned by Rowan would require manual copying of the superglobals befor= e using them in a newly spawned swoole coroutine. > > I believe that a much, much better way of implementing a worker mode in P= HP 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 t= o how all native PHP I/O functions should behave in an upcoming Native Even= t Loop RFC I'm planning to make. > > The idea here is to make all I/O related functions, including this new wa= it_request function, completely async in a manner that is compatible with f= ibers 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 filesys= tem 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 nativ= e) 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 even= t 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 userlan= d 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 in= side of PHP itself, perhaps even using Rust and tokio as well. > > I feel that the current direction of the discussion of PHP worker modes i= s wrong, as it is mostly ignoring the presence of a perfectly usable concur= rency API in PHP (fibers): this has potential to cause a big split in the P= HP 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 n= ative 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 s= uspension 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 no= t integrated with any php event loop, and thus does not know to return cont= rol to any eventual php fiber when needed, and will just block all PHP fibe= rs until that single go I/O operation is done (unlike in php-tokio, where t= he rust tokio event loop is fully integrated with the php event loop (curre= ntly 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 a= ctually lend a hand if needed...) Hey Daniil, I already said this, but to reiterate: I, personally, hear what you are saying and largely agree with you; however, before we can really have any kind of discussion on concurrent servers, we HAVE to address the underlying issues that are missing from PHP. In PHP-src, there are no such things as request objects. There are no such things as event loops. There are fibers, but absolutely no std-library i/o functions are using them, making it pointless to build on fibers except when using user-land libraries that reimplemented everything (like amphp) in pure PHP or an extension like swoole. That is the unfortunate point we are at and it doesn't make sense to address concurrent servers at this moment, or passing request objects. We have a long way to go before those will be real things that we can have a proper conversation about in the context of php-src. Robert Landers Software Engineer Utrecht NL