Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112690 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24333 invoked from network); 30 Dec 2020 23:12:14 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Dec 2020 23:12:14 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E3E8F1804D4 for ; Wed, 30 Dec 2020 14:47:02 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-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,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 30 Dec 2020 14:47:02 -0800 (PST) Received: by mail-lf1-f49.google.com with SMTP id l11so40823891lfg.0 for ; Wed, 30 Dec 2020 14:47:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=1E0hadgSY96tMACunkf1KzlSfDSoHlaVMqzQlilnShs=; b=ekAxbFMfJtaXo4tpIG3NIJjZfrHX+46dtONBau1HkwEoOuelDrbnk8YfVWSwlxb8uc ZzSIv9L8FEgHaSuzoWKWf+23Odil46Et2IdJBN8CaNKw/xkoZai2Z7AJ7PSJROF5m6zL CUwBBqlRUMpu9HLDAFx48/j9zxJixxDGtac2TX9oiGArdUOWs+8FwzWzHdVF7cWXJ40e J3FN6fQUe/jm+fIafC2MfthbHVg8m4/5ZXrm0XQ3wIV23j2Qrd/gw0JSo0CT2M52mhjT F3ncsDQK9nBpezfutbVxrmmbpeBt0L/Cg1i81ldQtQgZ9KPCJQe/CeynRTMP/0GafMf/ 6eyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=1E0hadgSY96tMACunkf1KzlSfDSoHlaVMqzQlilnShs=; b=q5TbOyPCMstLgdiruaHLOGNmbknzkCCpaUyAeb40RLKXeZzsJDsuGFiGnZv9akSo85 k2X4d2xinMF03/pyKyzGHdYY8XBYO50bEy59KpJrXR9DYBQekSKLLRp8cDuWKTuQwGZG V2LjwTQqHw9wQxQQ2dFHHx4Q7w3N3Z3aoL3qs3MOXeoi3nGOf6M/BZk+fu9QgZv/79wJ pSHQo5wIY+JwVqhXRE5kymdOyHqsZCK3GX/UDewaEzsh81mpDVxIKJYkdnLan4uUqInx +Km6/x8I6FDmmvVVrVjeUpplngNhEr70OY0jP6GlXH11DbU879NjbdI29EoWWXVpORLe F/JQ== X-Gm-Message-State: AOAM530hZx0FQFeJKQtwXCbkCHEIcy63kcRDxy42V/IaJPnNeKOghcT6 W94KQIuPq3zHmH6q5LZOIGIEwAoQiwBC1LydGo/zmNgL0D8= X-Google-Smtp-Source: ABdhPJzongyEVfC6GE9FiwrnQlYdwp2I2giHIKZllYXpO6ujSLrATM7zxdWv5nsApmww8XY/toeVIpYywlMb20ya1Ek= X-Received: by 2002:a05:6512:34d3:: with SMTP id w19mr21663643lfr.418.1609368420713; Wed, 30 Dec 2020 14:47:00 -0800 (PST) MIME-Version: 1.0 Received: by 2002:ab3:7110:0:0:0:0:0 with HTTP; Wed, 30 Dec 2020 14:47:00 -0800 (PST) In-Reply-To: References: <1d0abb04-4987-43a9-85bc-bccc3bd6be9a@www.fastmail.com> <03108284-740a-4a5d-130f-15b2e67e9df9@mabe.berlin> <459d7ff7-e553-dce9-7d43-c3b1e772e572@gmail.com> <7f4fe9ca-1c20-6f69-cef0-a9718af742a3@gmail.com> <3e683903-66cb-4a1a-9ff8-22887dbb8dce@www.fastmail.com> <20df5745-f2e0-418c-94c1-46bd7712592a@www.fastmail.com> Date: Wed, 30 Dec 2020 22:47:00 +0000 Message-ID: To: Larry Garfield Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Analysis of property visibility, immutability, and cloning proposals From: olleharstedt@gmail.com (=?UTF-8?Q?Olle_H=C3=A4rstedt?=) 2020-12-30 21:27 GMT, Olle H=C3=A4rstedt : > 2020-12-30 20:37 GMT, Larry Garfield : >> On Wed, Dec 30, 2020, at 2:16 PM, Olle H=C3=A4rstedt wrote: >> >>> > Ok. You have a benchmark for this? I can make one otherwise, for the >>> > query >>> > example. >>> > >>> > It worries me a little that immutability is pushed into the ecosystem >>> > as a >>> > silver bullet. Main reason functional languages are using it is >>> > because >>> > ownership is a newer concept, so it hasn't been adapted as much. >>> >>> Tiny benchmark here: >>> https://gist.github.com/olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a >>> >>> Running this on my ARM machine with PHP 7.2 gives ~1.2s for the >>> immutable loop, ~0.35s for the mutable one, meaning immutability is >>> ~3x as expensive performance wise. And this is for a SMALL object - I >>> suspect the performance hit will grow the bigger the class gets (more >>> properties to clone). Correct me if I'm wrong. :/ >> >> >> That's not a valid benchmark; it's comparing cloning and method >> invocation >> against just property sets. The method calls are chewing up most of the >> time there. >> >> Here's a more fair comparison on my laptop: >> https://gist.github.com/Crell/848568124e25c8c83fc4da5455063bab >> >> Which is only ~20% slower. And that's when dealing with very small >> numbers, >> so in most cases you're unlikely to notice a difference unless you reall= y >> are iterating over something a million times. >> >> I also tossed some big string properties into the class, and while the >> total >> time went up a bit the ratio between the two stayed about the same. >> >> --Larry Garfield > > Oh yeah. Huh. Didn't realize method calling was expensive. :) Another > good reason to not use setters, lol. On my machine it's ~70% slower > with "with" now (running multiple runs, using bash "time"). > > The performance might matter when frameworks start to adapt patterns > like this. It's already in PSR, with the HTTP message interface etc. > Now I'm curious how OCaml and JS performs with a similar benchmark. > Since they both have tracing GC, and PHP uses ref counting... Benchmarks for JS and OCaml: https://gist.github.com/olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a In JS, it's 5x more expensive to use spread operator vs mutate the object fields directly (or maybe node is just not optimized well on the ARM CPU?). In OCaml, it's a ~10% difference between destructive update (mutable fields) and cloning. No idea what to take from that. :)