Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64549 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43692 invoked from network); 5 Jan 2013 13:39:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2013 13:39:24 -0000 Authentication-Results: pb1.pair.com header.from=tom@punkave.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tom@punkave.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain punkave.com designates 209.85.216.178 as permitted sender) X-PHP-List-Original-Sender: tom@punkave.com X-Host-Fingerprint: 209.85.216.178 mail-qc0-f178.google.com Received: from [209.85.216.178] ([209.85.216.178:57804] helo=mail-qc0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4A/20-38386-90D28E05 for ; Sat, 05 Jan 2013 08:39:22 -0500 Received: by mail-qc0-f178.google.com with SMTP id j34so9620427qco.23 for ; Sat, 05 Jan 2013 05:39:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:references:in-reply-to:mime-version :content-transfer-encoding:content-type:message-id:cc:x-mailer:from :subject:date:to:x-gm-message-state; bh=pz6aSWB+7gnLlPknABKHpOORODXAONJ1sPAomRtP8Cw=; b=lpVTAhrrhceGuBxLjBLR993c6Qy7c/DYlBb+I8Pt+opxSgPcuhtOoON01KBkIFO2YV osjAppRQSWOxTRVt32WccgtFS5+O6e2SlABuPGJURqupMDuNBXOgw0ByLIGDaPuBTrEO +uGJCckDj0bmIrx1dPnGKcgFRB6/FrCoySgDYstrd0vVMAkaZu6+ounBM3HVENmN/nP/ 5o1630RjH0JHE/h0XDVhm4FzrJJCYXdD7fSKerl/beYbsxUAgAO9rQski7mcGun+evYd xrUwIheMBittERH27h0BAvuQ0iEwf1XDbSfE3yShDc2jrXBQDNWvjGbQcXKiNBY9/VFU VuVA== X-Received: by 10.224.177.204 with SMTP id bj12mr23137490qab.12.1357393158884; Sat, 05 Jan 2013 05:39:18 -0800 (PST) Received: from [10.0.1.5] (c-69-139-83-97.hsd1.pa.comcast.net. [69.139.83.97]) by mx.google.com with ESMTPS id eg9sm17500996qab.7.2013.01.05.05.39.17 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 05 Jan 2013 05:39:18 -0800 (PST) References: <50E80E51.7080704@hoa-project.net> In-Reply-To: <50E80E51.7080704@hoa-project.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Message-ID: Cc: "internals@lists.php.net" X-Mailer: iPhone Mail (9B206) Date: Sat, 5 Jan 2013 08:39:15 -0500 To: "ivan.enderlin@hoa-project.net" X-Gm-Message-State: ALoCoQm5eOwqm/ZCRApiI5LAPdZ/vcKvkDr+5VaUvo+wv4rePzYaarCb1ZbuwyBEPa1/MQ1S8osr Subject: Re: [PHP-DEV] Ruby's symbols From: tom@punkave.com (Tom Boutell) If symbols could use the sign bit or otherwise distinguish from typical inte= ger keys one would have some hope of meaningful debugging output. I don't th= ink it makes sense to mix integer keys and symbol keys but being able to dif= ferentiate them for debugging purposes would be great. However since php arr= ays are sparse anyway is there much of a win from integer keys in the first p= lace? Sent from my iPhone On Jan 5, 2013, at 6:28 AM, "Ivan Enderlin @ Hoa" wrote: > Hi :-), >=20 > Nice proposal but I have few questions and remarks. >=20 > My first question is how to delete symbols? I don't know if it is useful o= r not. We should discuss about that. Is `unset(:foo)` enough? How are they h= andled by the GC? >=20 > Another question is: where are we able to use symbols? Is it allowed to wr= ite `const FOO =3D :bar` for example? Or in a default argument value: `publi= c function f ( $x =3D :foo ) { =E2=80=A6 }`? >=20 > What kind of (arithmetical) operations are allowed on symbols? >=20 > You proposed to represent symbols as integers. Could it create conflicts? = For example: `$foo =3D [0 =3D> 'bar', :baz =3D> 'qux']`. What happens if `:b= az` is set to 0? >=20 > Finally, if my memory is good, a few months ago, some patches were updatin= g =E2=80=9Cconstant strings=E2=80=9D performances by representing them as =E2= =80=9Cbuffers=E2=80=9D/=E2=80=9Cconstants=E2=80=9D internally, no? Maybe I'm= wrong, but it could be a middle-way solution if it is not yet implemented (= especially since we have strings dereferencing, implementation could be ease= ). >=20 > My two cents :-). > Best regards. >=20 >=20 >=20 >=20 > On 05/01/13 15:07, Nikita Nefedov wrote: >> I know I shouldn't write "Ruby" in the subject of a letter for php-intern= als ML, but... Just wanted to ask, is anybody interested in this feature in P= HP? >> You can read about it here: http://www.randomhacks.net/articles/2007/01/2= 0/13-ways-of-looking-at-a-ruby-symbol >>=20 >> It can be implemented in PHP as a HashTable where key would be strings an= d values would be unique identifiers (just incrementing long, can be tracked= with nNextFreeElement). >>=20 >> How it would work in userland? >>=20 >> If it would be proposal, I would divide it on two different (competitive)= proposals: >>=20 >> First, we could implement it so symbol expression would return an associa= ted integer. >> (note that symbols are most often used as hash keys in ruby so my example= s are relying on it) >> Example: >> $foo =3D array(); >> $foo[:bar] =3D "qwerty"; >>=20 >> var_dump($foo); >> // array(1) { >> // [0] =3D> >> // string(6) "qwerty" >> //} >>=20 >> var_dump(:bar); // int(0) >>=20 >> var_dump(:some_new_symbol); // int(1) >>=20 >> This is the easiest way, and I can implement it. But, it would be hard to= debug. >>=20 >> The second way - we can implement it as a new variable type, and the main= thing is that arrays will be able to take variables of symbol type as keys,= I know it sounds scary but it's not so hard actually. >> I will drop the part about new variable type, the main thing is how HashT= ables could work with symbols as keys. >> We could change the type of nKeyLength (in Bucket) to signed integer (now= it's unsigned, I don't think we will miss something from that) and use it a= s a flag: if it's -1 (less than zero) - then we know the symbol was used as a= key, there will be filled bucket.h member with a symbol identifier. >> That way the above code will evaluate so: >> $foo =3D array(); >> $foo[:bar] =3D "qwerty"; >>=20 >> var_dump($foo); >> // array(1) { >> // [:bar] =3D> >> // string(6) "qwerty" >> //} >>=20 >> var_dump(:bar); // symbol(:bar) >>=20 >> var_dump(:some_new_symbol); // symbol(:some_new_symbol) >>=20 >> To make it clear, when retrieving an element by symbol from array, these s= teps will be needed: >> 1. Get symbol identifier (ulong) >> 2. Get elements from array where bucket.h equals to this identifier >> 3. Iterate over those elements and find the one that has nKeyLength =3D=3D= -1 >> (actually we will iterate over pNext/pLast elements) >>=20 >>=20 >> What symbols can give: >> 1. More convenient way to use it almost everywhere as a replacement for s= trings and sometimes for constants. There's a lot of code that uses arrays a= s a parameter-stores. For example, here's how you usually define a form in S= ymfony2: >> $builder >> ->add('title', 'text', array( >> 'label' =3D> 'Album title' >> )) >> ->add('title_alias', 'text', array( >> 'label' =3D> 'Album alias', >> 'required' =3D> false, >> 'property_path' =3D> 'properties.titleAlias' >> )) >> ->add('comment', 'text', array( >> 'label' =3D> 'Comment', >> 'required' =3D> false, >> 'property_path' =3D> 'properties.comment' >> )) >> ->add('labels', 'text', array( >> 'label' =3D> 'Musical labels', >> 'required' =3D> false, >> 'property_path' =3D> 'properties.labels' >> )) >> ->add('language', 'text', array( >> 'required' =3D> false, >> 'property_path' =3D> 'properties.language' >> )) >> It could be improved this way: >> $builder >> ->add('title', :text, array( >> :label =3D> 'Album title' >> )) >> ->add('title_alias', :text, array( >> :label =3D> 'Album alias', >> :required =3D> false, >> :property_path =3D> 'properties.titleAlias' >> )) >> ->add('comment', :text, array( >> :label =3D> 'Comment', >> :required =3D> false, >> :property_path =3D> 'properties.comment' >> )) >> ->add('labels', :text, array( >> :label =3D> 'Musical labels', >> :required =3D> false, >> :property_path =3D> 'properties.labels' >> )) >> ->add('language', :text, array( >> :required =3D> false, >> :property_path =3D> 'properties.language' >> )) >> 2. Memory usage reduction. AFAIK, there's a lot of cases like the above a= nd the use of symbols would affect on memory usage significantly. >> 3. Memory leaks. Symbols can't be just garbage collected as, for example z= vals, it's not possible. But we can do it for every request, so I don't thin= k it would be problem. >> 4. Autocompletion from IDEs. >>=20 >> PS I don't want to call it "proposal", despite the fact that this is actu= ally a proposal, I'm just not sure it can go as a proposal. >>=20 >=20 > --=20 > Ivan Enderlin > Developer of Hoa > http://hoa.42/ or http://hoa-project.net/ >=20 > PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) > http://disc.univ-fcomte.fr/ and http://www.inria.fr/ >=20 > Member of HTML and WebApps Working Group of W3C > http://w3.org/ >=20 >=20 >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >=20