www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - COW with scratch in std.string

OK, here's another thread related to COW and std.string. Currently when a 
function in std.string needs to copy (the C in COW) it allocates the new 
string from the GC. How about adding an optional trailing parameter for a 
scratch char[] for the result if it fits in the scratch? Basically what 
typically happens is that instead of a line line "result = new char[len]" 
you instead initialize result to the scratch array and then say 
"result.length = len;". For example, currently std.string.join looks rougly 
like
char[] join(char[][] words, char[] sep)
{
  char[] result;
  if (words.length)
  {
    [ compute length of result in len ]
    result = new char[len];
    [ fill result ]
  }
  return result;
}

and instead if join took an optional trailing scratch array it could be 
written as

char[] join(char[][] words, char[] sep, char[] scratch = null)
{
  char[] result;
  if (words.length)
  {
    [ compute length of result in len ]
    result = scratch;
    result.length = len;
    [ fill result ]
  }
  return result;
}

I haven't done any actual performance testing of such a modified std.string. 
The function tolower etc also can take a scratch array to reduce the GC load 
but those would still suffer from the performance hit of copying the strings 
to the scratch (or whereever the result ends up).
Aug 30 2005