named arguments, string interpolation, please stop.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jan 11 23:09:46 UTC 2024


On Thursday, January 11, 2024 3:02:16 PM MST Adam Wilson via Digitalmars-d 
wrote:
> On Thursday, 11 January 2024 at 21:37:01 UTC, Walter Bright wrote:
> > On 1/11/2024 1:18 PM, DrDread wrote:
> >> this is discarding the use case that you want to mark only
> >> parts of your codebase nogc, but enforce it'S really nogc. we
> >> do use some realtime threads, but the rest off the app is @gc.
> >
> > This subthread suggests to me that the problem with @nogc is it
> > works.
>
> @nogc works just fine. We recently spent a good chunk of time in
> Discord educating a newbie on what it *actually* does.
>
> What @nogc is specified to do: Prevent GC allocations from
> occurring. Fantastic.
> What people actually do with @nogc: Use it to selectively disable
> the GC without using GC.disable().
>
> The reason for this stems from a side-effect of how the *current*
> GC operates. Because allocations are the trigger for collections,
> by preventing allocations, collections are also prevented. And
> what people really want to do is disable collections because they
> don't like the collection pauses. They don't *actually* care
> about the allocations per se because that is generally as fast as
> a malloc and they are going to have to allocate at some point
> anyways.
>
> So @nogc works exactly as specified, but because of an
> unspecified implementation side-effect, that is not guaranteed to
> hold true in the future, the @nogc crowd writes their code as if
> @nogc does something else entirely.  And we end up here in this
> thread.

@nogc doesn't even prevent collections running while a function is called.
It just prevents that specific function from triggering the collection
(thanks to no allocations triggering it). Another thread could trigger a
collection while that function is running. So, yes, in a single-threaded
program, it's equivalent to calling GC.disable, but in the general case, it
isn't.

So, anyone actually relying on @nogc preventing collections is arguably
asking for trouble even with the current GC - though it seems like the kind
of folks who like to use @nogc are often the kind of folks who are likely to
avoid the GC entirely, so if @nogc is used everywhere in their code, then
they won't end up with either GC allocations or collections. However, anyone
using it selectively really can't rely on it to avoid collections unless
their program is single-threaded and will always remain so.

- Jonathan M Davis





More information about the Digitalmars-d mailing list