Major performance problem with std.array.front()

Walter Bright newshound2 at digitalmars.com
Fri Mar 7 16:18:18 PST 2014


On 3/7/2014 11:59 AM, Andrei Alexandrescu wrote:
> On 3/6/14, 7:55 PM, Walter Bright wrote:
>> On 3/6/2014 7:22 PM, bearophile wrote:
>>> One advantage of your change is that this code will work:
>>>
>>> auto s = "hello".dup;
>>> s.sort();
>>
>> Yes, I hadn't thought of that.
>>
>> The auto-decoding front() introduces all kinds of asymmetry in how
>> ranges work, and asymmetry is bad as it negatively impacts composability.
>
> There's no asymmetry, and decoding helps composability as I demonstrated.

Here's one asymmetry:
-----------------------------
alias int T;     // compiles
//alias char T;  // fails to compile

struct Input(T) { T front(); bool empty(); void popFront(); }
struct Output(T) { void put(T); }

import std.array;

void copy(F,T)(F f, T t) {
     while (!f.empty) {
         t.put(f.front);
         f.popFront();
     }
}

void main() {
     immutable(T)[] from;
     Output!T to;
     from.copy(to);
}
-------------------------------



More information about the Digitalmars-d mailing list