Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83112 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27586 invoked from network); 18 Feb 2015 20:00:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Feb 2015 20:00:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.47 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 74.125.82.47 mail-wg0-f47.google.com Received: from [74.125.82.47] ([74.125.82.47:53094] helo=mail-wg0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 09/17-25021-F4FE4E45 for ; Wed, 18 Feb 2015 15:00:16 -0500 Received: by mail-wg0-f47.google.com with SMTP id x12so3274488wgg.6 for ; Wed, 18 Feb 2015 12:00:12 -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=quAPavxrjzD/1cg05cunJgoMVEAJdLvy+tpINjwsoz8=; b=lHK1sHA7zHBwR8zjPNcO0Hf3q0Jo/LkmW6y8t4d0fjp0iSnlSSCIadejjZSVbeCCu5 MeurZnRGXLnjnQdmCDZc7U470/C0Mq+eLGgiLaySoRBgmQDTouUwO9Bd7guOITp87XDc uCyCbDb0wiygJ2i0x1TD8XlD+PcDoIHIF4Ius4KIDUjsO478OgWLxCvmQNZqMSKdXXJl vpY3x174tSp/tZMeqLbL351bUffi2gva30hNIVQYI1j2tqilvqK/WVaLuWTpqRf4Iepb H09K34pVEPbZdmalgWVPyZF1BtPAKlRssGo8+UQcbVJ/SS7hfyy/h2o2H6BiUxWRCnK8 t5xQ== MIME-Version: 1.0 X-Received: by 10.180.198.240 with SMTP id jf16mr3407041wic.27.1424289610795; Wed, 18 Feb 2015 12:00:10 -0800 (PST) Received: by 10.27.10.168 with HTTP; Wed, 18 Feb 2015 12:00:10 -0800 (PST) In-Reply-To: References: Date: Wed, 18 Feb 2015 21:00:10 +0100 Message-ID: To: Dmitry Stogov Cc: Alexander Lisachenko , Nikita Popov , PHP internals list Content-Type: multipart/alternative; boundary=047d7b66f6db4f0cf4050f6244ee Subject: Re: [PHP-DEV] [RFC][Discussion] Parser extension API From: nikita.ppv@gmail.com (Nikita Popov) --047d7b66f6db4f0cf4050f6244ee Content-Type: text/plain; charset=UTF-8 On Wed, Feb 18, 2015 at 8:22 PM, Dmitry Stogov wrote: > I think the AST API shouldn't use "public" properties. > Using it, we will have to construct the whole tree of objects, duplicating > information from AST. > I would propose SimpleXML approach instead - construct object only for > node(s) we currently access. > > So at first we will construct just single object referring to AST root. > Then traversing it we will create and destroy objects for necessary nodes. > > To access children I would propose to implementing ArrayAccess interface. > > > $ast = \php\ast\parse($string); > foreach ($ast as $child) { > echo "\t" . $child->getKindName() . "\n"; > foreach ($child as $grandchild) { > echo "\t" . $child->getKindName() . "\n"; > > } > } > > Thanks. Dmitry. > I've considered using this approach, but decided against it because it introduces a lot of "magic" for unclear gains. Lazily creating the objects means we have to keep around the entire AST arena while at least one node referencing it is alive. So if you're analyzing a large project with a few thousand files and keep around a few nodes in some cases, you end up not just with the memory usage of those nodes, but with the memory usage of the entire ASTs. Furthermore in many cases you'll just traverse the entire AST, in which case you'll need to instantiate all nodes anyway and the lazy instantiation will only hurt. In any case, performance of constructing a full AST is pretty good - I don't remember the exact numbers, but ast\parse_code() is only slightly slower than doing token_get_all() on the same code. Nikita --047d7b66f6db4f0cf4050f6244ee--