Specifying @nogc on structs seems to have no effect

Craig Black craigblack1234 at gmail.com
Tue Sep 19 14:22:21 UTC 2017

On Tuesday, 19 September 2017 at 13:59:27 UTC, Jonathan M Davis 
> On Tuesday, September 19, 2017 13:11:03 Craig Black via 
> Digitalmars-d wrote:
>> I've recently tried coding in D again after some years.  One 
>> of my earlier concerns was the ability to code without the GC, 
>> which seemed difficult to pull off.  To be clear, I want my 
>> programs to be garbage collected, but I want to use the GC 
>> sparingly so that the mark and sweep collections will be fast.
>>  So I want guarantees that certain sections of code and 
>> certain structs will not require the GC in any way.
>> I realize that you can allocate on the non-GC heap using 
>> malloc and free and emplace, but I find it troubling that you 
>> still need to tell the GC to scan your allocation. What I 
>> would like is, for example, to be able to write a @nogc 
>> templated struct that guarantees that none of its members 
>> require GC scanning.  Thus:
>> @nogc struct Array(T)
>> {
>>    ...
>> }
>> class GarbageCollectedClass
>> {
>> }
>> void main()
>> {
>>    Array!int intArray; // fine
>> }
> @nogc is a function attribute. It has no effect on types except 
> on their member functions. All it does is guarantee that a 
> function marked with @nogc cannot call any function which is 
> not @nogc and cannot do any operation which is not considered 
> @nogc. It's to guarantee that a function does not use the GC 
> and has nothing more to do with types than attributes like 
> @safe or nothrow do.
> - Jonathan M Davis

Thank you for your response.  The @nogc attribute is good, but in 
my opinion it is incomplete if all types still require scanning.  
The purpose of not employing GC in certain sections of code is 
performance, and we are sacrificing performance with every 
allocation unit that is needlessly scanned.


More information about the Digitalmars-d mailing list