Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79797 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50941 invoked from network); 17 Dec 2014 22:39:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Dec 2014 22:39:44 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.174 mail-wi0-f174.google.com Received: from [209.85.212.174] ([209.85.212.174:62974] helo=mail-wi0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 06/21-43959-F2602945 for ; Wed, 17 Dec 2014 17:39:44 -0500 Received: by mail-wi0-f174.google.com with SMTP id h11so17400935wiw.1 for ; Wed, 17 Dec 2014 14:39:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=YkQVjeyLzgqgjPkNF8J8K6laqfb946oBALcWlo6ajnQ=; b=zy5sG8TwfrhbR8QjaRpqAovW8IzES2Ms9RcVaHPU4qJh4yyR1iFor2MRTSNukTeMt3 pgE1XRsinppRyId5rTAfwtyYw3QEk0hyAvPO0E2Mw2J1dsnWSu89PvkJbEhqr9Rba1SO YkMnYnhLPxtUwvn05qBWvf4FSedhnPIJi3nDoCfgpU0E1ucmj2c/PzOYlFUrQ9e/MZDG +B5f46YZy+YAZInRiJXhOY/bzDjq5+7FNKgHaEAFT2Vujbom48mQ/lfUvPfSlSejogVE yvih4aUgURleKPBMRFDpl7QKOswpt5E58VT6iExyUmRchDILoiQcCsE18vWt7N8ISyU/ 5yHg== X-Received: by 10.180.9.111 with SMTP id y15mr18104650wia.33.1418855980374; Wed, 17 Dec 2014 14:39:40 -0800 (PST) Received: from [192.168.0.2] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id l3sm6739827wje.12.2014.12.17.14.39.39 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 Dec 2014 14:39:39 -0800 (PST) Message-ID: <54920623.20207@gmail.com> Date: Wed, 17 Dec 2014 22:39:31 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: internals@lists.php.net References: <548FEE87.7020602@gmail.com> <5491B77A.4020004@gmail.com> <5491FE29.1090106@gmail.com> In-Reply-To: <5491FE29.1090106@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC][VOTE] Objects as Keys From: rowan.collins@gmail.com (Rowan Collins) 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]