Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79799 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58030 invoked from network); 18 Dec 2014 00:05:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2014 00:05:33 -0000 Authentication-Results: pb1.pair.com header.from=guilhermeblanco@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=guilhermeblanco@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.177 as permitted sender) X-PHP-List-Original-Sender: guilhermeblanco@gmail.com X-Host-Fingerprint: 209.85.223.177 mail-ie0-f177.google.com Received: from [209.85.223.177] ([209.85.223.177:47786] helo=mail-ie0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 23/32-43959-C4A12945 for ; Wed, 17 Dec 2014 19:05:33 -0500 Received: by mail-ie0-f177.google.com with SMTP id rd18so135584iec.22 for ; Wed, 17 Dec 2014 16:05:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=SJi4SyWitScKnPINGVc7iYqSzaSDP5f3F7caspW6+MA=; b=n/c2Sn1q2vkRYMrlDK+uPqpMcis31fzqO3tlkd8Pdtarb1yPsYI15D5cYRcSvXYA/I W2aNab3Y2R8wHAF6dDkGfC+YfewJfsAJAGOap1zPhI7kTnCSp7ciVG+LjcJ7mDIBme9F CgQI/8yDFrb3Y3fO98YHxPqZWrWx/y+c4dqp32ujMF0CZ1q+I/eAWSXJ3JvdcVMoRp4V bOaXyFY7nU8yKimuxqZwzkPPfTPSqx0hAGmS5+N1fN6IH9fCtLci0AIsmIJVplrwNrCU 9pHjYh99pRM5bMD41Owi1FIhRR7MviPtK/3sEjFunS6D4Ua5zvxKRr33kfZRgJk1fNsr YsHw== MIME-Version: 1.0 X-Received: by 10.50.138.169 with SMTP id qr9mr10956638igb.17.1418861130134; Wed, 17 Dec 2014 16:05:30 -0800 (PST) Received: by 10.64.238.75 with HTTP; Wed, 17 Dec 2014 16:05:29 -0800 (PST) Received: by 10.64.238.75 with HTTP; Wed, 17 Dec 2014 16:05:29 -0800 (PST) In-Reply-To: <54920623.20207@gmail.com> References: <548FEE87.7020602@gmail.com> <5491B77A.4020004@gmail.com> <5491FE29.1090106@gmail.com> <54920623.20207@gmail.com> Date: Wed, 17 Dec 2014 22:05:29 -0200 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=089e010d9124a5baff050a7259a0 Subject: Re: [PHP-DEV] [RFC][VOTE] Objects as Keys From: guilhermeblanco@gmail.com ("guilhermeblanco@gmail.com") --089e010d9124a5baff050a7259a0 Content-Type: text/plain; charset=UTF-8 Hi, I originally considered you could retrieve the object key, but if it is not possible by this RFC, I will switch my vote to no. By storing hash one way it introduces a huge wtf to the language. Cheers, On Dec 17, 2014 8:40 PM, "Rowan Collins" wrote: > On 17/12/2014 22:05, Rowan Collins wrote: > >> For one thing, I think it might be interesting to explore whether real >> objects as keys is actually as difficult as some people are assuming. I can >> naively imagine a few ways it could be implemented that seem in my mind to >> have minimal impact on any array that doesn't use it, but haven't yet seen >> anyone seriously discuss any possible approaches. >> > > In fact, to expand on this, I'm going to stick my neck out and tell > everyone my crackpot theory, which someone who actually understands the > engine will probably demolish in 5 seconds flat, but makes sense in my > mind. :P > > The implementation of arrays could remain the same, with a single pointer > to a second "shadow" HashTable, which would only be initialised when an > object was actually added as a key. Thus the memory overhead would be > reserving a single pointer per array (not per item), and the execution > overhead (for any existing array usage) would be a single if(shadowPointer) > check in a few key places. > > The idea would be that the object would still be hashed - e.g. by default > with spl_object_hash, but with __hash if set - and that key would be stored > in the normal way. However, an extra entry would be added to the shadow > array, as though you had also called $shadow[ $obj->__hash() ] = $obj. > > We might want to do something tricksy to the key, like adding a null byte > at the beginning, to make it less likely for a scalar key and an object > hash to collide. This would also allow further short-cutting in lookups, > since if the first byte was not \0, there would be no chance of a match in > the shadow array. (If it was \0, there might still not be a shadow object, > because null-prefixed strings are actually valid scalar keys, but this > seems unlikely to be a common occurrence.) > > Whenever an array key is taken as *input* (e.g. $foo[$bar], > array_key_exists()), the hash function would be called (much as in the > current RFC), and the corresponding value could be returned directly - the > shadow table would not need to be touched, so performance would be > identical to that of the current proposal. > > A lookup in the shadow array would need to happen only when writing to the > array, and when a key was being given as *output* (e.g. foreach ( $a as $k > => $v ), array_search(), key()). This would happen at the speed of normal > random access, and scale with the number of objects used as keys, not the > number of items in the main array. > > Like I say, I'm sure this is full of holes, and I'll leave it there before > I get too far down a dead end, but I thought it might be a more productive > line of thought than "this will probably never happen". > > Regards, > > -- > Rowan Collins > [IMSoP] > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --089e010d9124a5baff050a7259a0--