Using closure causes GC allocation

Vino.B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 11:59:30 PDT 2017


On Saturday, 2 September 2017 at 18:32:55 UTC, Moritz Maxeiner 
wrote:
> On Saturday, 2 September 2017 at 18:08:19 UTC, vino.b wrote:
>> On Saturday, 2 September 2017 at 18:02:06 UTC, Moritz Maxeiner 
>> wrote:
>>> On Saturday, 2 September 2017 at 17:43:08 UTC, Vino.B wrote:
>>>> [...]
>>>
>>> Line 25 happens because of `[a.name]`. You request a new 
>>> array: the memory for this has to be allocated (the reason 
>>> why the compiler says "may" is because sometimes, e.g. if the 
>>> array literal itself contains only literals, the allocations 
>>> needn't happen at runtime and no GC call is necessary). Since 
>>> you don't actually use the array, get rid of it:
>>>
>>> [...]
>>
>> Hi,
>>
>>   Thank you for your help and the DMD version that i am using 
>> is DMD 2.076.0 and yes I am on windows.
>
> Please post a compilable, minimal example including how that 
> function gets called that yields you that compiler output.

Hi,

  Please find the example code below,

import std.stdio: File,writeln;
import std.datetime.systime: Clock, days, SysTime;
import std.file: SpanMode, dirEntries, exists, isFile, mkdir, 
remove;
import std.typecons: tuple;
import std.algorithm:  filter, map, each;
import std.array: array;

void logClean (string[] Lglst, int LogAge) {
	if (!Lglst[0].exists) { mkdir(Lglst[0]); }
	auto ct1 = Clock.currTime();
	auto st1 = ct1 + days(-LogAge);
	auto dFiles = dirEntries(Lglst[0], SpanMode.shallow).filter!(a 
=> a.exists && a.isFile && a.timeCreated < st1).map!(a => 
tuple(a.name)).array;
	  dFiles.each!(a => a[0].remove);
}

void main () {
string[] LogDir = 
["C:\\Users\\bheev1\\Desktop\\Current\\Script\\D\\Logs"];
int  LogAge = 1;
logClean(LogDir,LogAge);
}

Another similar issue :
  I removed the [a.name] and the issue in line 25 has resolved, 
but for another function i am getting the same error

string[][] cleanFiles(string FFs, string Step) {
	auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a =>[a.name , a.timeCreated.toSimpleString[0 .. 
20]]).array;  -> Issue in this line
     if (Step == "run")
         dFiles.each!(a => a[0].remove);
		return dFiles;
}

if the replace the line in error as below then i am getting the 
error "Error: cannot implicitly convert expression dFiles of type 
Tuple!(string, string)[] to string[][]"

auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name, a.timeCreated.toSimpleString[0 
.. 20])).array;



More information about the Digitalmars-d-learn mailing list