www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18629] New: std.algorithm.iteration.subsitute is slow


          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 puremagic.com
          Reporter: b2.temp 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`.


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

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


[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.

Mar 18 2018