[D-runtime] foreach (dchar c; s) is too slow

Jacob Carlborg doob at me.com
Tue Oct 1 10:47:41 PDT 2013


On 30 sep 2013, at 20:47, Martin Nowak <dawg at dawgfoto.de> wrote:

> The runtime support for string foreach with decoding has some serious performance issues.
> It's currently implemented by creating a delegate for the foreach body and calling _aApply*.
> I recently submitted this pull to vibe.d https://github.com/rejectedsoftware/vibe.d/pull/327.
> Using the explicit for (auto tmp = s; !tmp.empty; tmp.popFront()) { auto c = tmp.front; /*CODE*/ }
> is about 3 times faster.
> I'd like to avoid such hidden performance pitfalls. Any ideas how to transition to templated library code?
> For dchar iteration the compiler could simply prefer the range interface, but that wouldn't work for wchar (or char with w/dstrings).
> Is it something that should only be added to phobos, e.g. foreach (c; s.byChar!dchar())?
> Or should this be a combination of compiler and library support, i.e. when an explicit char type is given the compiler
> instantiates a template with that char type.


I don't see why "byChar" should be added. It's better to fix the current implementation. If I recall correctly, also "foreach(c ; s)" is the same as foreach over dchar. Which is an even more hidden pitfall.

-- 
/Jacob Carlborg

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/d-runtime/attachments/20131001/0dea62ca/attachment.html>


More information about the D-runtime mailing list