Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 19, 2017 Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
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 } |
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | On Tuesday, 19 September 2017 at 13:11:03 UTC, Craig Black 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
>
>
> }
I don't know why send this message out when I was just in the middle of typing it out: But the example should read like this:
@nogc struct Array(T)
{
...
}
class GarbageCollectedClass
{
}
void main()
{
Array!int intArray; // fine
Array!GarbageCollectedClass classArray; // Error: struct Array is @nogc
}
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | On Tuesday, 19 September 2017 at 13:13:48 UTC, Craig Black wrote:
> @nogc struct Array(T)
> {
> ...
> }
>
> class GarbageCollectedClass
> {
> }
>
> void main()
> {
> Array!int intArray; // fine
> Array!GarbageCollectedClass classArray; // Error: struct Array is @nogc
>
> }
If you want to enforce that behaviour I guess you can use reflection to check if T has attribute @nogc.
Andrea
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | On Tuesday, 19 September 2017 at 13:11:03 UTC, Craig Black 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
>
>
> }
struct Array(T)
{
@nogc:
...
}
?
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eugene Wissner | On Tuesday, 19 September 2017 at 13:32:59 UTC, Eugene Wissner wrote:
> On Tuesday, 19 September 2017 at 13:11:03 UTC, Craig Black 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
>>
>>
>> }
>
>
> struct Array(T)
> {
> @nogc:
> ...
> }
> ?
Thanks, I didn't know you could to that but it still doesn't give me the behavior that I want:
class Foo
{
}
struct MyStruct
{
@nogc:
public:
Foo foo; // This does not produce an error, but it still requires a GC scan
void Bar()
{
foo = new Foo; // This produces an error
}
}
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | 19.09.2017 16:46, Craig Black пишет: > class Foo > { > } > > struct MyStruct > { > @nogc: > public: > Foo foo; // This does not produce an error, but it still requires a GC scan > void Bar() > { > foo = new Foo; // This produces an error > } > } it produces an error for me https://run.dlang.io/is/PbZE5i |
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 19.09.2017 16:48, drug пишет: > 19.09.2017 16:46, Craig Black пишет: >> class Foo >> { >> } >> >> struct MyStruct >> { >> @nogc: >> public: >> Foo foo; // This does not produce an error, but it still requires a GC scan >> void Bar() >> { >> foo = new Foo; // This produces an error >> } >> } > > it produces an error for me https://run.dlang.io/is/PbZE5i sorry, wrong link, this one https://run.dlang.io/is/UwTCXu |
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 19.09.2017 16:49, drug пишет:
> 19.09.2017 16:48, drug пишет:
>> 19.09.2017 16:46, Craig Black пишет:
>>> class Foo
>>> {
>>> }
>>>
>>> struct MyStruct
>>> {
>>> @nogc:
>>> public:
>>> Foo foo; // This does not produce an error, but it still requires a GC scan
>>> void Bar()
>>> {
>>> foo = new Foo; // This produces an error
>>> }
>>> }
>>
>> it produces an error for me https://run.dlang.io/is/PbZE5i
> sorry, wrong link, this one
> https://run.dlang.io/is/UwTCXu
Oh no, today is not my day, sorry for noise)
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | On 09/19/2017 03:46 PM, Craig Black wrote:
> struct MyStruct
> {
> @nogc:
> public:
> Foo foo; // This does not produce an error, but it still requires a GC scan
@nogc is about GC allocations. `Foo foo;` doesn't cause a GC allocation.
@nogc doesn't control what memory is scanned by the GC.
|
September 19, 2017 Re: Specifying @nogc on structs seems to have no effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Black | 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
|
Copyright © 1999-2021 by the D Language Foundation