Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64545 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32303 invoked from network); 5 Jan 2013 11:28:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Jan 2013 11:28:24 -0000 Authentication-Results: pb1.pair.com smtp.mail=ivan.enderlin@hoa-project.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=ivan.enderlin@hoa-project.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain hoa-project.net from 95.130.12.24 cause and error) X-PHP-List-Original-Sender: ivan.enderlin@hoa-project.net X-Host-Fingerprint: 95.130.12.24 host1.trois-doubles.net Linux 2.6 Received: from [95.130.12.24] ([95.130.12.24:50276] helo=host1.trois-doubles.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 94/4E-38386-55E08E05 for ; Sat, 05 Jan 2013 06:28:23 -0500 Received: from Hwhost2.local (41-164.79-83.cust.bluewin.ch [83.79.164.41]) by host1.trois-doubles.net (Postfix) with ESMTPSA id B4F2E20150C for ; Sat, 5 Jan 2013 12:28:17 +0100 (CET) Message-ID: <50E80E51.7080704@hoa-project.net> Date: Sat, 05 Jan 2013 12:28:17 +0100 Reply-To: ivan.enderlin@hoa-project.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20130103 Thunderbird/19.0a2 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Ruby's symbols From: ivan.enderlin@hoa-project.net ("Ivan Enderlin @ Hoa") Hi :-), Nice proposal but I have few questions and remarks. My first question is how to delete symbols? I don't know if it is useful = or not. We should discuss about that. Is `unset(:foo)` enough? How are=20 they handled by the GC? Another question is: where are we able to use symbols? Is it allowed to=20 write `const FOO =3D :bar` for example? Or in a default argument value:=20 `public function f ( $x =3D :foo ) { =E2=80=A6 }`? What kind of (arithmetical) operations are allowed on symbols? You proposed to represent symbols as integers. Could it create=20 conflicts? For example: `$foo =3D [0 =3D> 'bar', :baz =3D> 'qux']`. What = happens if `:baz` is set to 0? Finally, if my memory is good, a few months ago, some patches were=20 updating =E2=80=9Cconstant strings=E2=80=9D performances by representing = them as=20 =E2=80=9Cbuffers=E2=80=9D/=E2=80=9Cconstants=E2=80=9D internally, no? May= be I'm wrong, but it could be a=20 middle-way solution if it is not yet implemented (especially since we=20 have strings dereferencing, implementation could be ease). My two cents :-). Best regards. On 05/01/13 15:07, Nikita Nefedov wrote: > I know I shouldn't write "Ruby" in the subject of a letter for=20 > php-internals ML, but... Just wanted to ask, is anybody interested in=20 > this feature in PHP? > You can read about it here:=20 > http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-= ruby-symbol > > It can be implemented in PHP as a HashTable where key would be strings = > and values would be unique identifiers (just incrementing long, can be = > tracked with nNextFreeElement). > > How it would work in userland? > > If it would be proposal, I would divide it on two different=20 > (competitive) proposals: > > First, we could implement it so symbol expression would return an=20 > associated integer. > (note that symbols are most often used as hash keys in ruby so my=20 > examples are relying on it) > Example: > $foo =3D array(); > $foo[:bar] =3D "qwerty"; > > var_dump($foo); > // array(1) { > // [0] =3D> > // string(6) "qwerty" > //} > > var_dump(:bar); // int(0) > > var_dump(:some_new_symbol); // int(1) > > This is the easiest way, and I can implement it. But, it would be hard = > to debug. > > The second way - we can implement it as a new variable type, and the=20 > main thing is that arrays will be able to take variables of symbol=20 > 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=20 > HashTables could work with symbols as keys. > We could change the type of nKeyLength (in Bucket) to signed integer=20 > (now it's unsigned, I don't think we will miss something from that)=20 > and use it as a flag: if it's -1 (less than zero) - then we know the=20 > symbol was used as a key, there will be filled bucket.h member with a=20 > symbol identifier. > That way the above code will evaluate so: > $foo =3D array(); > $foo[:bar] =3D "qwerty"; > > var_dump($foo); > // array(1) { > // [:bar] =3D> > // string(6) "qwerty" > //} > > var_dump(:bar); // symbol(:bar) > > var_dump(:some_new_symbol); // symbol(:some_new_symbol) > > To make it clear, when retrieving an element by symbol from array,=20 > these steps 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) > > > What symbols can give: > 1. More convenient way to use it almost everywhere as a replacement=20 > for strings and sometimes for constants. There's a lot of code that=20 > uses arrays as a parameter-stores. For example, here's how you usually = > define a form in Symfony2: > $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=20 > above and the use of symbols would affect on memory usage significantly= =2E > 3. Memory leaks. Symbols can't be just garbage collected as, for=20 > example zvals, it's not possible. But we can do it for every request,=20 > so I don't think it would be problem. > 4. Autocompletion from IDEs. > > PS I don't want to call it "proposal", despite the fact that this is=20 > actually a proposal, I'm just not sure it can go as a proposal. > --=20 Ivan Enderlin Developer of Hoa http://hoa.42/ or http://hoa-project.net/ PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) http://disc.univ-fcomte.fr/ and http://www.inria.fr/ Member of HTML and WebApps Working Group of W3C http://w3.org/