www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How usable is the D language without a garbage collector?

reply LinguisticMystic <LinguisticMystic mail.ru> writes:
I'm looking for a modestly mature language to be a C++ 
replacement for my hobby needs. D seems to be a good one as it 
ticks all the boxes of C++ while fixing most (all?) of its flaws. 
However, what confuses me is the fact that D has a garbage 
collector. My necessary requirement is that the runtime should 
not include any such thing. And D does have some sort of  nogc 
switch. My question is, how usable is the scenario of GC-free 
programs in reality? Does the switch apply globally, so that the 
compiled binary is free from GC code? How many essential 
libraries can work without GC? Is it better for me to look 
elsewhere if I don't want GC?

Thanks for your clarifications!
Jul 15 2022
next sibling parent reply max haughton <maxhaton gmail.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 I'm looking for a modestly mature language to be a C++ 
 replacement for my hobby needs. D seems to be a good one as it 
 ticks all the boxes of C++ while fixing most (all?) of its 
 flaws. However, what confuses me is the fact that D has a 
 garbage collector. My necessary requirement is that the runtime 
 should not include any such thing. And D does have some sort of 
  nogc switch. My question is, how usable is the scenario of 
 GC-free programs in reality? Does the switch apply globally, so 
 that the compiled binary is free from GC code? How many 
 essential libraries can work without GC? Is it better for me to 
 look elsewhere if I don't want GC?

 Thanks for your clarifications!
What kind of software do you want to write
Jul 15 2022
parent reply LinguisticMystic <LinguisticMystic mail.ru> writes:
On Friday, 15 July 2022 at 09:31:14 UTC, max haughton wrote:

 What kind of software do you want to write
Video games, number crunching. Stuff C++ is usually used for.
Jul 15 2022
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
On 15/07/2022 9:40 PM, LinguisticMystic wrote:
 On Friday, 15 July 2022 at 09:31:14 UTC, max haughton wrote:
 
 What kind of software do you want to write
Video games, number crunching. Stuff C++ is usually used for.
Neither of which D's GC is the enemy of. The GC is a library that can only be triggered to stop the world, but is not guaranteed to stop the world on, during a from GC memory allocation. You are quite free to use malloc/free and not tell the runtime about a thread iff you need to. You can also disable scanning when a pause would not be desirable. D has been used in both scenarios with the GC turned on. https://dlang.org/blog/the-gc-series/
Jul 15 2022
prev sibling next sibling parent max haughton <maxhaton gmail.com> writes:
On Friday, 15 July 2022 at 09:40:05 UTC, LinguisticMystic wrote:
 On Friday, 15 July 2022 at 09:31:14 UTC, max haughton wrote:

 What kind of software do you want to write
Video games, number crunching. Stuff C++ is usually used for.
For number crunching the GC is not going to get in your way (i.e. more than the actual mathematics) unless you write extremely unfortunate code (this is true of any memory allocator).
Jul 15 2022
prev sibling parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Friday, 15 July 2022 at 09:40:05 UTC, LinguisticMystic wrote:
 On Friday, 15 July 2022 at 09:31:14 UTC, max haughton wrote:

 What kind of software do you want to write
Video games, number crunching. Stuff C++ is usually used for.
Depends on what kind of video games you are thinking of. A strategy game or a basic RPG MUD would be no problem, even with the GC. You can bind to non-templated C++, so you could use a flexible C/C++ renderer like Skia running on a non-gc thread. If you want to create a first person shooter game then you should pick the framework(s) first, and language second. Creating a quality game-engine for that from scratch takes a massive investment given hardware differences etc, so the language-choice becomes secondary. That said, everything in C++ is RAII-based so it is hard to beat in terms of language features for that type of programming, despite C++ being one of the most time consuming languages around. C++ is not really a hobby language… Anyway, you don't have to stick to one language. You can mix.
Jul 15 2022
parent reply Araq <rumpf_a web.de> writes:
On Friday, 15 July 2022 at 11:18:15 UTC, Ola Fosheim Grøstad 
wrote:
 Anyway, you don't have to stick to one language. You can mix.
But you shouldn't mix as debugging and the tooling situation in general across languages is terrible. At least when "mixing" implies some sort of scripting language with its own interpreter loop.
Jul 15 2022
next sibling parent electricface <electricface qq.com> writes:
On Friday, 15 July 2022 at 11:48:31 UTC, Araq wrote:
 On Friday, 15 July 2022 at 11:18:15 UTC, Ola Fosheim Grøstad 
 wrote:
 Anyway, you don't have to stick to one language. You can mix.
But you shouldn't mix as debugging and the tooling situation in general across languages is terrible. At least when "mixing" implies some sort of scripting language with its own interpreter loop.
Recommended reading it: https://dlang.org/blog/the-gc-series/
Jul 15 2022
prev sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Friday, 15 July 2022 at 11:48:31 UTC, Araq wrote:
 On Friday, 15 July 2022 at 11:18:15 UTC, Ola Fosheim Grøstad 
 wrote:
 Anyway, you don't have to stick to one language. You can mix.
But you shouldn't mix as debugging and the tooling situation in general across languages is terrible. At least when "mixing" implies some sort of scripting language with its own interpreter loop.
That's false I work with D and also some C libraries, and i can debug my program and step into C code without issues Proof: (lib: mongoose, statically linked) https://i.imgur.com/C2vjGOo.png
Jul 15 2022
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
Little Rant:

Guys, when someone comes and ask if something is feasible without 
component X, you shouldn't tell them they are wrong for wanting 
to do it the way they want to do it

D is perfectly great for using without the GC, just like using C 
is perfectly great without a GC, just like Rust is perfectly 
great withotu a GC, they are equivalent

GC is only here as an optional scratch buffer, and it is very 
handy to have when you need it

We should welcome people with their usecase and make them feel 
safe for wanting to achieve what they want to achieve

If someone asks to not use the GC and you tell them "you 
shouldn't really", they'll go check the next language in their 
list of "interesting languages"

D a great language because it is pragmatic and polyvalent, it is 
a strength, as a community, let's be pragmatic too and accept 
that some folks doesn't want to use a GC!

It's like OOP, not everything needs it, not every meal needs 
salt, not every mattress needs to be white, not every cars needs 
to be grey
Jul 15 2022
prev sibling parent bachmeier <no spam.net> writes:
On Friday, 15 July 2022 at 15:05:24 UTC, ryuukk_ wrote:
 On Friday, 15 July 2022 at 11:48:31 UTC, Araq wrote:
 On Friday, 15 July 2022 at 11:18:15 UTC, Ola Fosheim Grøstad 
 wrote:
 Anyway, you don't have to stick to one language. You can mix.
But you shouldn't mix as debugging and the tooling situation in general across languages is terrible. At least when "mixing" implies some sort of scripting language with its own interpreter loop.
That's false I work with D and also some C libraries, and i can debug my program and step into C code without issues Proof: (lib: mongoose, statically linked) https://i.imgur.com/C2vjGOo.png
I think he was referring to mixing a compiled language with Lua, or something like that.
Jul 15 2022
prev sibling next sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 Thanks for your clarifications!
I make GC free programs but not out of choice. The GC is very rarely the cause of bad performance; there is nothing preventing you from optimizing, because D is native and a system language. SO it's not like a Javascript situation, where creating garbage can be unavoidable. On the contrary, if you can use a GC you will be more productive for the non-critical parts, and creating a good program in the first place. Gaining valuable time to optimize bottlenecks. An enormous majority of system software can be written with a GC somewhere.
Jul 15 2022
prev sibling next sibling parent bachmeier <no spam.net> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:

 D seems to be a good one as it ticks all the boxes of C++ while 
 fixing most (all?) of its flaws.
I don't have an answer to your main question, because I mostly just use the GC (including for number crunching) and my life is good. However, I want to caution that you need to view D as an alternative to C++, not an iteration. I have on many occasions seen C++ users get upset because D does not always operate the way they're used to C++ operating. It's a different language.
 However, what confuses me is the fact that D has a garbage 
 collector. My necessary requirement is that the runtime should 
 not include any such thing.
Bjarne Stroustrup has, for a very long time, [boasted that you can use garbage collection with C++](https://www.stroustrup.com/bs_faq.html#garbage-collection). That has clearly not prevented you from using C++. The fact that D has a garbage collector does not mean anything; just don't use it. You'll lose some parts of the standard library.
Jul 15 2022
prev sibling next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 I'm looking for a modestly mature language to be a C++ 
 replacement for my hobby needs. D seems to be a good one as it 
 ticks all the boxes of C++ while fixing most (all?) of its 
 flaws. However, what confuses me is the fact that D has a 
 garbage collector. My necessary requirement is that the runtime 
 should not include any such thing. And D does have some sort of 
  nogc switch. My question is, how usable is the scenario of 
 GC-free programs in reality? Does the switch apply globally, so 
 that the compiled binary is free from GC code? How many 
 essential libraries can work without GC? Is it better for me to 
 look elsewhere if I don't want GC?

 Thanks for your clarifications!
It is perfectly usable without a GC You have access to malloc/free from libc and also allocators, so you can do what ever you want! I am working on an online RPG targeting WASM without touching the GC at all, no RAII (thanks to scope guards aka defer, scope(exit)), no exceptions and it's been super smooth - game server - login server - master server - front end - database stuff D is a very pragmatic language, i still use the GC for tooling projects and the deployment scripts where the GC doesn't have any impact at all, it was very useful to have even thought not necessary https://www.kdom.xyz/ If you don't want the GC then you know what you are doing you don't want to deal with 3rd-party anyways since they can mess up your memory allocation strategy You can consume C/C++ code with D, so you can consume their entire ecosystem with ease I consume GLFW on desktop, Freetype for fonts, OpenGL/OpenAL for gpu/audio and PostgreSQL for the database, they are all C projects and the integration is super smooth And there are lot of people around developing D libraries that are nogc compatible
Jul 15 2022
next sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Fri, Jul 15, 2022 at 02:55:45PM +0000, ryuukk_ via Digitalmars-d wrote:
[...]
 You can consume C/C++ code with D, so you can consume their entire
 ecosystem with ease
 
 I consume GLFW on desktop, Freetype for fonts, OpenGL/OpenAL for
 gpu/audio and PostgreSQL for the database, they are all C projects and
 the integration is super smooth
I can confirm this. In my own projects I use all sorts of C libraries with no problem: sqlite3, freetype, MPFR, Xlib, XCB, EGL, Vulkan, etc.. It's not even a problem that I freely use GC in my D code; that does not stop me from using C libraries easily. T -- Public parking: euphemism for paid parking. -- Flora
Jul 15 2022
prev sibling next sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Friday, 15 July 2022 at 14:55:45 UTC, ryuukk_ wrote:
 D is a very pragmatic language, i still use the GC for tooling 
 projects and the deployment scripts where the GC doesn't have 
 any impact at all, it was very useful to have even thought not 
 necessary
Same here, products are nogc but to do them you need quite a lot of tools and one-time experiments, and those can be with GC usage.
Jul 15 2022
prev sibling parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Friday, 15 July 2022 at 14:55:45 UTC, ryuukk_ wrote:
 You can consume C/C++ code with D, so you can consume their 
 entire ecosystem with ease
Not template heavy C++.
Jul 16 2022
parent evilrat <evilrat666 gmail.com> writes:
On Saturday, 16 July 2022 at 07:04:03 UTC, Ola Fosheim Grøstad 
wrote:
 On Friday, 15 July 2022 at 14:55:45 UTC, ryuukk_ wrote:
 You can consume C/C++ code with D, so you can consume their 
 entire ecosystem with ease
Not template heavy C++.
Taking a chance to advertise, up to c++17 mostly not a problem, some tweaks by hand might be needed in generated code though. https://github.com/Superbelko/ohmygentool
Jul 16 2022
prev sibling next sibling parent Salih Dincer <salihdb hotmail.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 I'm looking for a modestly mature language to be a C++ 
 replacement for my hobby needs. D seems to be a good one as it 
 ticks all the boxes of C++ while fixing most (all?) of its 
 flaws. However, what confuses me is the fact that D has a 
 garbage collector.
Learn like the GC doesn't exist. You won't even try hard to learn. Most things are things you already know and are familiar with. I think, you won't say also "what's up with that GC". Welcome to the club, to the D family...😀 SDB 79
Jul 15 2022
prev sibling next sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 However, what confuses me is the fact that D has a  garbage 
 collector. My necessary requirement is that the runtime should 
 not include any such thing. And D does have some sort of  nogc 
 switch.
The nogc is primarily that you aren't allocating or handling anything that uses (*or may cause*) the GC to run. You might need nothrow as well. Personally i love putting as much temporary allocations as i can on the stack, and leaving cleanup to instantly and cleanly be handled by the function cleanup code. In C/D one of my favorite things would be to allocate everything as a single block and then assign pointers as necessary so a single free will handle it rather than say 20-30. But if you aren't using the built-in array allocation, or an algorithm that will try to change the allocated memory then it should be fine. Not sure about the allocator template use, but doing structs and malloc/free should be a workable pair without relying on the GC at all. But i'm very rusty on all this anymore :(
Jul 16 2022
prev sibling parent reply welkam <wwwelkam gmail.com> writes:
On Friday, 15 July 2022 at 09:27:51 UTC, LinguisticMystic wrote:
 D seems to be a good one as it ticks all the boxes of C++ while 
 fixing most (all?) of its flaws.
It was said before but I will repeat it. Try not to view D as C++ with its flaws fixed but as C with features added to it. If you would to expect D to behave like C++ you will get frustrated as D doesnt behave like C++ in some cases.
 My necessary requirement is that the runtime should not 
 include<...>
Im not surprised you wrote that. Zero cost abstractions and minimal runtime are like gospel in C++ circles. Its not 1990 anymore and in modern times these things almost dont matter when compared to memory copying, allocations and cpu cache usage. If you want your code to go fast pay attention to memory. One time a D project had a crown of being the fastest json parser in the world. It achieved that by avoiding making temporary heap allocations. https://forum.dlang.org/thread/20151014090114.60780ad6 marco-toshiba
 Does the [ nogc] switch apply globally, so that the compiled 
 binary is free from GC code?
GC does not run periodically like a cron job. It can only runs when you are making an allocation. nogc applies to a scope and compiler makes sure that no GC allocations occur in that scope. If you put nogc at main() then your program cant allocate GC memory. If you never allocate from GC then its not going to be initialized. If GC is not initialized at all then its never used and code that's never used is not linked in final binary.
 Is it better for me to look elsewhere if I don't want GC?
I dont think so. While D usage without GC is not streamlined I believe its still a better package as a whole compared to alternatives especially if you write green field projects. If you want to write games in D you might be interested in this GDC talk https://gdcvault.com/play/1023843/D-Using-an-Emerging-Language
Jul 16 2022
parent reply Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Saturday, 16 July 2022 at 22:23:36 UTC, welkam wrote:
 It was said before but I will repeat it. Try not to view D as 
 C++ with its flaws fixed but as C with features added to it. If 
 you would to expect D to behave like C++ you will get 
 frustrated as D doesnt behave like C++ in some cases.
Well, C++ has been distancing itself from C for a long time and new features makes this more and more true for modern C++. C is a legacy language that is in for the long haul, but trying to merge higher level concepts with C leads to unnecessary complications. There are some decisions in D that undermines what C tries to achieve, like having wrapping signed int, having non zero default initialization for built in types and GC dependent language features. In total, C++ is more of a C with features added, making C constructs and idioms more and more obsolete. Making C++ increasingly more high level. D is more like an attempt to merge C, Java and templates, but trying to find a path back to C patterns while retaining some memory safety rather than providing abstractions of a higher level to deal with in optimization. It is unclear whether there is a market for C idioms. It might be waning...
Jul 16 2022
parent reply Siarhei Siamashka <siarhei.siamashka gmail.com> writes:
On Sunday, 17 July 2022 at 05:36:37 UTC, Ola Fosheim Grøstad 
wrote:
 There are some decisions in D that undermines what C tries to 
 achieve, like having wrapping signed int, having non zero 
 default initialization for built in types
Modern C compilers are "undermining what C tries to achieve" too. GCC has '-fwrapv' option for wrapping signed ints and '-ftrivial-auto-var-init=' option for preventing variables from being accidentally left uninitialized. There's also '-fno-strict-aliasing' option for eliminating the root cause of a certain class of hard to debug problems. All of this together forms a kind of somewhat safer non-standard C dialect, which is still fully compatible with the existing C code.
Jul 17 2022
parent Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= <ola.fosheim.grostad gmail.com> writes:
On Sunday, 17 July 2022 at 09:27:03 UTC, Siarhei Siamashka wrote:
 Modern C compilers are "undermining what C tries to achieve" 
 too. GCC has '-fwrapv' option for wrapping signed ints and 
 '-ftrivial-auto-var-init=' option for preventing variables from 
 being accidentally left uninitialized.
C-compilers have a gazillion of options, but the advantage of having zero-initialisation of heap allocated arrays is that you can bypass initialization if your memory manager zeros out memory for you.
 There's also '-fno-strict-aliasing' option for eliminating the 
 root cause of a certain class of hard to debug problems. All of 
 this together forms a kind of somewhat safer non-standard C 
 dialect, which is still fully compatible with the existing C 
 code.
This is necessary because of existing (old) type punning practice.
Jul 17 2022