Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80712 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23973 invoked from network); 18 Jan 2015 01:01:49 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Jan 2015 01:01:49 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.52 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.216.52 mail-qa0-f52.google.com Received: from [209.85.216.52] ([209.85.216.52:54424] helo=mail-qa0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 89/90-18613-BF50BB45 for ; Sat, 17 Jan 2015 20:01:47 -0500 Received: by mail-qa0-f52.google.com with SMTP id x12so20014614qac.11 for ; Sat, 17 Jan 2015 17:01:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=R/M3Eif+7eMeRMzW+gMumd67zRxKx6xKHtMmgbv4uxs=; b=y+YgrucxffSFLl8jvQfcY1uZ/hKubgXw64E/zBW/+mLKPGIQvtO1l/NxNGwOTZoNJ1 CL+gt7xxrSRRTgSPINL/aVb/vLyRk+ou9bEqGQ2mw/y8jBOiXZnUGNrqVHtYiy3qkzyp fyNMYRYtscGhEFot/bfXFGpy96lj2EJKXpomeroJGxvxs+0XyIlPXOytVHt6Jfp1ayrD y3j4FGflBv4MkpP4dTuwcjARBtq7sDrGkf3rQObxtwqOjBQKwsp2j+VPdfIbovElK3xH 8T4jSSD3XkWxjL7ouSFRQREzV+EAEyEStMeMoCXO3KqGqqo4ao4lvNUg/vlhSfEqSank vwYQ== X-Received: by 10.229.130.65 with SMTP id r1mr37386805qcs.16.1421542904604; Sat, 17 Jan 2015 17:01:44 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.93.70 with HTTP; Sat, 17 Jan 2015 17:01:04 -0800 (PST) In-Reply-To: References: <54B93AAD.6080308@gmail.com> Date: Sun, 18 Jan 2015 10:01:04 +0900 X-Google-Sender-Auth: GKi1mfBuWzEN437iPkegnpI-whI Message-ID: To: Rowan Collins Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1133d79adcb0ff050ce2bfdb Subject: Re: [PHP-DEV] Fixing strange foreach behavior. From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a1133d79adcb0ff050ce2bfdb Content-Type: text/plain; charset=UTF-8 Hi Rowan, On Sat, Jan 17, 2015 at 8:43 PM, Rowan Collins wrote: > My concern is, at what cost? Given how rarely used the internal pointer is, > are we carrying around a chunk of extra memory with every array just on the > off-chance that it will be used, or is there some magic that makes it > zero-width until it's needed? > > Reusing it for foreach makes perfect sense if it increases performance for > the majority of cases, and the only way to cover every edge-case like the > one at the top of this thread would be to ban that optimisation outright. > If there's some way of separating things such that the rarely used > constructs take the performance hit, I'm all for it, though. > External position pointer needs a little more memory for sure. However, external position pointer is used all over the place in standard and other modules. I agree that some benchmark should be taken before we proceed. Alternatively, we could achieve consistency the other way round, by making > foreach() reset the internal pointer even when it *doesn't* use it, and > documenting that fact like we do for certain array functions. Code relying > on the current behaviour when mixing them is probably buggy anyway, because > it is not well-defined, so the BC concern should be low. > IIRC, there were discussions when foreach is added that point out internal position pointer usage. Before foreach, everyone was using current()/next()/reset(). I think the reason foreach sets internal position pointer to the end was the code used in those days. It was not a technical/performance reasons, wasn't it? I don't remember well because it was more than 10 years ago. Anyone? IIRC, foreach was introduced by Andi. I think foreach used external position pointer and didn't touch internal position pointer at all with his first proposal. Please correct me if I'm wrong. As for nesting, I think PHP is doing the right thing for plain arrays and > non-rewindable integrators, because I would expect a for each loop to start > at the beginning, thus have an implicit reset/rewind. The rewindable > behaviour is awkward, though - intuitively, the iteraror needs to be able > to track multiple positions simultaneously, which isn't guaranteed by the > interface design. Maybe an error should be issued if the integrator is > already subject of a foreach loop? > Iterator is headache, I agree. Iterator nesting may be detected by a flag easily, but it may be better to generate independent iterators for each "foreach". Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a1133d79adcb0ff050ce2bfdb--