Avoid deallocate empty arrays?

Ali Çehreli acehreli at yahoo.com
Thu Dec 17 16:48:29 UTC 2020


On 12/17/20 8:11 AM, IGotD- wrote:

 > It's common using arrays for buffering, that means constantly adding
 > elements and empty the elements.

I show an example of this at the following point in a DConf presentation:

   https://youtu.be/dRORNQIB2wA?t=791

The following code:

   int[] outer;

   while (a) {
     int[] inner;

     while (b) {
       inner ~= e;
     }

     outer ~= bar(inner);
   }

Can be turned into this:

   // Remember: These are thread-local
   static Appender!(int[]) outer;
   static Appender!(int[]) inner;

   outer.clear();    // Clear state from last call

   while (a) {
     inner.clear();  // Clear state from last iteration

     while (b) {
       inner ~= e;
     }

     outer ~= bar(inner.data);
   }

There is also assumeUnique(), which does not appear in the presentation, 
which may be useful in some cases.

Ali



More information about the Digitalmars-d-learn mailing list