Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99699 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5688 invoked from network); 3 Jul 2017 07:09:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jul 2017 07:09:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.160 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.160 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.160] ([81.169.146.160:19077] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6B/18-60825-CBDE9595 for ; Mon, 03 Jul 2017 03:09:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1499065785; l=7014; s=domk; d=kelunik.com; h=Content-Type:Cc:To:Subject:Date:From:References:In-Reply-To: MIME-Version; bh=GSA6JWYRNOXQ3qXvyX3/bEeAkk07BQ+8Yu6xC5E4Ilo=; b=sRynswGKVdmAeqcrmjVnLOgpEPHM4GG+wRMBS264BWY+1dzLqIm525LnkdLPBQTHFw yQsMrPA3kR/3F+zWcw8L4geDc3ayBRFGbd/wnqxQFCHLNrzMgSZU4si9Am4hk2RcOxFx 9IFw83SBjDI80E4Huv7KjTxq48ETT9NDo/cno= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLDup6E67mzuoNHBqT73Q== X-RZG-CLASS-ID: mo00 Received: by mail-oi0-f53.google.com with SMTP id x187so16232998oig.3 for ; Mon, 03 Jul 2017 00:09:45 -0700 (PDT) X-Gm-Message-State: AIVw113jzjknnjhlseveNQ7gwO9kvGTkh/8z0gmcZlkw7ETUSxh6k3Nl w+hVlQCeQoALINYUAe4hDw5+NZJZhw== X-Received: by 10.202.206.212 with SMTP id e203mr12109120oig.168.1499065784792; Mon, 03 Jul 2017 00:09:44 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.81.135 with HTTP; Mon, 3 Jul 2017 00:09:44 -0700 (PDT) In-Reply-To: References: Date: Mon, 3 Jul 2017 09:09:44 +0200 X-Gmail-Original-Message-ID: Message-ID: To: Andreas Hennings Cc: Sara Golemon , PHP internals Content-Type: multipart/alternative; boundary="001a113adef0992e5805536473c0" Subject: Re: [PHP-DEV] "Reader" as alternative to Iterator From: me@kelunik.com (Niklas Keller) --001a113adef0992e5805536473c0 Content-Type: text/plain; charset="UTF-8" 2017-07-03 4:49 GMT+02:00 Andreas Hennings : > Well, on a current project I made an attempt to write different > adapters in userland. > I finally decided that the clutter is not worth. > So for this project I wrote everything as "readers", and not as iterators. > > With a native solution, one could do this: > > function generate() { > yield 'a'; > yield 'b'; > yield 'c'; > } > > $it = generate(); > > while (FALSE !== $v = $it->read()) { > print $v . "\n"; > } > > With a userland adapter, the code would read like this: > > function generate() { > yield 'a'; > yield 'b'; > yield 'c'; > } > > $it = generate(); > > // This is the added line. > $reader = new IteratorReaderAdapter($it); > > while (FALSE !== $v = $reader->read()) { > print $v . "\n"; > } > > > This does add clutter and performance overhead. > In this example I'd say this is acceptable / survivable. > > In the project I was working on, I have multiple layers of "readers": > - One layer to read raw data from a CSV file. > - One layer to re-key the rows by column labels. > - One layer to turn each row into an object, with structured > properties instead of just string cells. > > With the "reader" approach, for each layer I would have one "reader > provider" class and one "reader" class per layer. > (In fact I have much more, but let's keep it simple) > Generator syntax would allow to have only one class per layer. > But with the additional adapter, we again increase the number of classes. > (I wanted dedicated reader classes for different return types, e.g. > one "RowReader", one "AssocReader", one "ObjectReader". So here I > would need one adapter class per type. But let's focus on the simple > case, where you can use the same reader class.) > > In addition to more classes and more function calls (performance), the > adapters also make stack traces heavier to look at. > > Overall, for this project, I decided that it was not worth it. > > The main purpose of the generator syntax is to simplify the code. The > need for userland adapters defeated this purpose. > Native readers would have made generators worthwhile for my use case. > > The idea of "dedicated reader types" e.g. Reader could be > added to "Open questions".. Hey Andreas, what you're trying to do here seems to be premature optimization. While you save a bunch of method calls, your I/O will be the actual bottleneck there. It's entirely fine to implement such logic in userland. Amp has a similar interface for its streams, but those have only string|null as types. If you want to allow all values, you either need a second method or need to wrap all values in an object. http://amphp.org/byte-stream/#inputstream + http://amphp.org/amp/iterators/#iterator-consumption Regards, Niklas --001a113adef0992e5805536473c0--