Hidden allocations (Was: Array literals REALLY should be immutable )

dsimcha dsimcha at yahoo.com
Thu Nov 12 09:40:26 PST 2009


== Quote from Bill Baxter (wbaxter at gmail.com)'s article
> 2009/11/12 Denis Koroskin <2korden at gmail.com>:
> > // untested
> > void mkdirRecurse(string path) {
> >    char* buffer = alloca(path.length);
> >    memcpy(buffer, path);
> >
> >    foreach (i, c; buffer[0..path.length]) {
> >        if (c == '/') {
> >            buffer[i] = 0;
> >            mkdir(buffer);
> >            buffer[i] = '/';
> >        }
> >    }
> > }
> >
> > There are a lot of functions that allocate without a clear reason.)
> I'm pretty sure the reason is that it means library code that's easier
> to write, understand and maintain.
> But yeh, if you give me the choice of two different functions, one
> that allocates and one that doesn't, otherwise identical, I'll pick
> the non-allocating version.
> --bb

I don't understand this attitude.  There are definitely times when readability and
maintainability count more than performance, but  library code that will be used
in hundreds of different places isn't one of them.  Knuth says we should forget
about small efficiencies about 97% of the time.  He's right.  However, when you
are writing this kind of generic library code, the odds are pretty good that at
least one place where it's used is going to be in the 3%.



More information about the Digitalmars-d mailing list