[Issue 18629] New: std.algorithm.iteration.subsitute is slow

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 18 21:56:35 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18629

          Issue ID: 18629
           Summary: std.algorithm.iteration.subsitute is slow
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: b2.temp at gmx.com

In this benchmark i cast items as ubyte[] to prevent autodecoding. Still 2.5X
slower than a naive equivalent that uses cascaded `replace`.

```
#!dmd -O -release -inline -boundscheck=off
module runnable;

import std.stdio, std.algorithm,std.array, std.conv;

string v1(string message)
{
    auto s = "{filepath}({line}:{column})[{type}]: {message}";
    const string fileName = "/home/folder/folter/source.d";
    const int line = 42;
    const int column = 13;

    s = s.replace("{filepath}", fileName);
    s = s.replace("{line}", to!string(line));
    s = s.replace("{column}", to!string(column));
    s = s.replace("{type}", "warn");
    s = s.replace("{message}", message);

    return s;
}

string v3(string message)
{
    auto s = "{filepath}({line}:{column})[{type}]: {message}";
    const string fileName = "/home/folder/folter/source.d";
    const int line = 42;
    const int column = 13;
    const string isError = "warn";

    return cast(string) substitute(cast(ubyte[])s,
        cast(ubyte[])"{filepath}", cast(ubyte[])fileName,
        cast(ubyte[])"{line}", cast(ubyte[])to!string(column),
        cast(ubyte[])"{column}", cast(ubyte[])to!string(column),
        cast(ubyte[])"{type}", cast(ubyte[])isError,
        cast(ubyte[])"{message}", cast(ubyte[])message
    ).array;
}

void main()
{
    import std.datetime.stopwatch;
    benchmark!((){v1("frst version");})(1_000_000).writeln;
    benchmark!((){v3("thrd version");})(1_000_000).writeln;
}
```

gives:

[1 sec, 669 ms, 590 μs, and 8 hnsecs]
[3 secs, 5 ms, 266 μs, and 2 hnsecs]

which does not encourage to write idiomatic D code.

--


More information about the Digitalmars-d-bugs mailing list