Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123758 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 qa.php.net (Postfix) with ESMTPS id 6874D1A009C for ; Sat, 22 Jun 2024 20:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1719087411; bh=lZZ1jRGcizMwiNdUQHNHB2ih6RWWU2YirrkOV+COfZ8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=G1Q6l/B/3mKv1ipzHnYiw9KG//AiuR1Nv3kiYnv3Tsz0wKB8MOwXTzVmjIGQrxPf4 rrFQOU0XkPjVWBzh3hhsY5m/GuPykNmx+Qps5suoCw3wm009ucyEtbbrM9jDOYqwhA ZrTlrTBxfCV9sNNHYkt2Q21m0/CyXvFFr9MYVGrMdSCJ48bTAfOHC0FWkav8RPeAt1 nLBafyh09ygmK5TgyT7Sg/ZyeEQWyCBsErkQEdRQnlY9TIV8WRvRLCOwZeaUz9qsCm 2pPCd7lAxHz37GgyZPFjiULkkrH6rol9CXUwtv6PBPMaHZQiX0nN60MMXPUSPfF63l ueD86uy7JgZ+Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 566921810A5 for ; Sat, 22 Jun 2024 20:16:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) 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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 ; Sat, 22 Jun 2024 20:16:46 +0000 (UTC) Received: by mail-lf1-f48.google.com with SMTP id 2adb3069b0e04-52c819f6146so3937111e87.1 for ; Sat, 22 Jun 2024 13:15:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719087330; x=1719692130; 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=lZZ1jRGcizMwiNdUQHNHB2ih6RWWU2YirrkOV+COfZ8=; b=DfNHUKaNuoVrkazFM+ufy73/tD6vGdNoMKmQYYw1hAwu9sGFhQNjDnGwa9fJ60clOe fzw8YsLl8we6nVVZ6GCYv2oSVyEN27tLFulQnwqfSKGgowuwjC98fOfgM5frIKJRg/iB GcOQ9BDL0LGCes0AFKdnRfOHM3ZvC+FgNXpXTbt09DmlBozfzRfZvevNg48crc7l09kF zsG8Kd5yrdBAhRSIQgyLVg918vMIJKG3ZgZPdDfF+Jm78Wgnu1eCvX5xzs7UZyzn+4AD yO/5rmwr9mAmPhC8Q0K+Q176dGt6xH4YZZn9xAMHVKMQ2xy1pf78LMjW6k9fmiwlSVSV ow0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719087330; x=1719692130; 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=lZZ1jRGcizMwiNdUQHNHB2ih6RWWU2YirrkOV+COfZ8=; b=t8VAWveFZsyO5VFFJ1Vwf4sEgvwp2l7I+otbxzjN+fr5X+WuBKehT2y9g9q3UOxSXa X5tU9mOhjbmu76lYafMfSVsvEG2nwLbl1I8exETCXOdVlWoMYEWbrLiShuuIOYr9x910 G2nHZpJm3tPjXdhWYf/lVlCRVA+fivpQBlsDvrJ37yVwUXHaE/2lGoMPA6EJNu0/xlLt diLdmpA7rcg3G4w1tRwwisnd9/bgEarqqUWCRe5dFt0jIAHMAaqYhngVrmbZrwR4pEVW m02WtgERQdDX/QvrEjKzbLThrAiOMxkPJ7KF+uYgbExGOr9AZX9K6OBha+E099bAE2Oi buUw== X-Forwarded-Encrypted: i=1; AJvYcCWcIsAc8dSb+Rng2W8yAUfVr5d4vuyMaDDDA6am4/+/HlLHZ3uHBqu0d9f9QIRTJuqArCwnAw0NFVsRzdHB6qvs+5zyGxzW/g== X-Gm-Message-State: AOJu0YxQpuZhqMOnhu4bGlgCLSf0U5rH4Y60AZFaL4MVQpa8iQUheTc1 LUkZpTiVFQ360ERD4rYgDSZZpfeG2hZ4eUXXZGRzgo2dN6cRUPMHxmwNn0VY6CgPUyzhXAK7jos cHav59ER8VfFJ17AEMN2AnQtU+xE= X-Google-Smtp-Source: AGHT+IH84xedib9gemjrskJBdE08T2OTbOyFQ24hgNaplJHCLOvyVnepszLSaAy4Zmy1D0fdT9AmasAlfmz87ji2zjY= X-Received: by 2002:a05:6512:3ba9:b0:52c:df4d:bb9e with SMTP id 2adb3069b0e04-52ce183b7e5mr475894e87.41.1719087329471; Sat, 22 Jun 2024 13:15:29 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <02ee8831-43a0-4857-886e-7f54fb42a99d@varteg.nz> <7d825b1d-e584-4916-9435-3561b9c54c26@gmail.com> In-Reply-To: Date: Sat, 22 Jun 2024 22:15:17 +0200 Message-ID: Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: Arnaud Le Blanc Cc: Niels Dossche , internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Sat, Jun 22, 2024 at 8:59=E2=80=AFPM Arnaud Le Blanc wrote: > > On Fri, Jun 21, 2024 at 7:20=E2=80=AFPM Robert Landers wrote: > > > > I'm always surprised why arrays can't keep track of their internal > > > > types. Every time an item is added to the map, just chuck in the ty= pe > > > > and a count, then if it is removed, decrement the counter, and if > > > > zero, remove the type. Thus checking if an array is `array` > > > > should be a near O(1) operation. Memory usage might be an issue (a > > > > couple bytes per type in the array), but not terrible.... but then > > > > again, I've been digging into the type system quite a bit over the > > > > last few months. > > > > > > And every time a modification happens, directly or indirectly, you'll > > > have to modify the counts too. Given how much arrays / hash tables ar= e > > > used within the PHP codebase, this will eventually add up to a lot of > > > overhead. A lot of internal functions that work with arrays will need > > > to be audited and updated too. Lots of potential for introducing bugs= . > > > It's (unfortunately) not a matter of "just" adding some counts. > > > > Well, of course, nothing in software is "just" anything. > > Counters are not cheap as we need one slot for each type in the array so = we need a dynamic buffer and an indirection, plus absolutely every mutation= needs to update a counter, including writes to references. It is possible = to remove the counters and to maintain an optimistic upper bound of the typ= e (computing the type more precisely when type checking fails), but I feel = this would not work well with pattern matching. To me, that sounds kinda silly. PHP does reference counting and while there is an overhead, it doesn't prevent us from using it... Anyway, while you make a good point for the pathological case, I suspect the impact to the popular case (mostly homogenous arrays), the performance impact will be negligible compared to the productivity impact of being able to type-check arrays. > > Also, a few things complicate this: > - Nested writes like $a[0][0][0]=3D1 need to backtrack to update the type= of all parent arrays after the element is added/updated Nesting could be forbidden, at least at first. I think saying "array>>" is forbidden is totally fine. > - Supporting references to properties whose type is a typed array, or dim= ensions of these properties, is very hard Also, depends on the defined behavior? If you have a typed property that is array and you try to add an int to it ... it could fail. Thus not being hard at all. > > Fixed-type arrays may be easier to support but there are important drawba= cks in usability IMHO. This does not play well with CoW semantics. > > Best Regards, > Arnaud Robert Landers Software Engineer Utrecht NL