Faster Command Line Tools in D

Steven Schveighoffer via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu May 25 05:46:17 PDT 2017


On 5/25/17 6:27 AM, Wulfklaue wrote:

> - Also wondering why one needed std.algorithm splitter, when you expect
> string split to be the fasted. Even the fact that you need to import
> std.array to split a string simply felt  strange.

Because split allocates on every call. The key, in many cases in D, to 
increasing performance is avoiding allocations. Has been that way for as 
long as I can remember.

Another possibility to "fix" this problem is to simply use an allocator 
with split that allocates on some predefined stack space. This is very 
similar to what v3 does with the Appender. Unfortunately, allocator is 
still experimental, and so split doesn't support using it.

> - So much effort for relative little gain ( after v2 splitter ). The
> time spend on finding a faster solution is in business sense not worth
> it. But not finding a faster way is simply wasting performance, just on
> this simple function.

The answer is always "it depends". If you're processing hundreds of 
these files in tight loops, it probably makes sense to optimize the 
code. If not, then it may make sense to focus efforts elsewhere. The 
point of the article is, this is how to do it if you need performance there.

> - Started to wonder if Python its PyPy is so optimized that without any
> effort, your even faster then D. What other D idiomatic functions are slow?

split didn't actually seem that slow. I'll note that you could opt for 
just the AA optimization (the converting char[] to string only when 
storing a new hash lookup is big, and not that cumbersome) and leave the 
code for split alone, and you probably still could beat the Python code.

> Off-topic:
>
> Yesterday i was struggling with split but for a whole different reason.
> Take in account that i am new at D.
>
> Needed to split a string. Simple right? Search Google for "split string
> dlang". Get on the https://dlang.org/phobos/std_string.html page.
>
> After seeing the splitLines and start experimenting with it. Half a hour
> later i realize that the wrong function was used and needed to import
> std.array split function.
>
> Call it a issue with the documentation or my own stupidity. But the fact
> that Split was only listed as a imported function, in this mass of text,
> totally send me on the wrong direction.
>
> As stated above, i expected split to be part of the std.string, because
> i am manipulating a string, not that i needed to import std.array what
> is the end result.

std.string, std.array, and std.algorithm all have cross-polination when 
it comes to array operations. It has to do with the history of when the 
modules were introduced.

> I simply find the documentation confusing with the wall of text. When i
> search for string split, you expect to arrive on the string.split page.
> Not only that, the split example are using split as a separate keyword,
> when i was looking for variable.split().

There is a search field on the top, which helps to narrow down what 
choices are available.

> Veteran D programmers are probably going to laughing at me for this but
> one does feel a bit salty after that.

I understand your pain. I work with Swift often, and sometimes it's very 
frustrating trying to find the right tool for the job, as I'm not 
thoroughly immersed in Apple's SDK on a day-to-day basis. I don't know 
that any programming language gets this perfect.

-Steve


More information about the Digitalmars-d-announce mailing list