News for hackers. Stuff that matters.
Re:D as a C Replacement by petre on Tuesday July 02, 2019 @05:17PM (#20339484)
> For things that don't need classes (and there is a lot of code) I stick to C.

I'd use D anyway just for the GC or whatever, less compiler fighting, betterC etc. Procedural, OOP, functional, yay.

Re:D as a C Replacement by rowanG077 on Tuesday July 02, 2019 @05:41PM (#20339653)
Really? For me the presence of a GC disqualifies D as the main replacement for C and C++.
Re:D as a C Replacement by mhh__ on Tuesday July 02, 2019 @06:05PM (#20339806)
How much software genuinely requires not having a GC? If you don't want it, don't use it, but using it can make your software completely safe and fast (Garbage Collection is often faster than RC as long as pause times are acceptable, and added memory use).

A lot of the standard library ranges are inherently lazy and therefore require no interaction with the GC. Exceptions are also nogc now.

Re:D as a C Replacement by quicknir on Tuesday July 02, 2019 @06:17PM (#20339892)
If you can tolerate GC then like 20 languages (as or less obscure than D) enter the conversation. If you can tolerate GC and the performance implications that go with it, then using C or C++ is rarely a good choice to begin with (well, other than legacy, which is a valid and common reason).
Re:D as a C Replacement by mhh__ on Tuesday July 02, 2019 @06:25PM (#20339950)
D isn't defined by performance or memory management, however. There are many D features which are just blisteringly professional in how they approach software development: Using D is conducive to writing good software.

Metaprogramming, for example, in D is obvious and almost fun. The compile times are ridiculous compared to C++, i.e. I can build the main D compiler in three different configurations in about 5sec on my machine.

Re:D as a C Replacement by quicknir on Tuesday July 02, 2019 @06:40PM (#20340044)
I'm not saying that D is defined by that. I'm simply saying that if you're comparing to C or C++, you are talking about use cases that are defined by performance and/or memory management (or else, they are being used for legacy reasons). If you have a green field project that doesn't have massive performance concerns, and doesn't have to be specifically in C or C++ for other reasons (toolchain availability, available developer resources, etc), you probably aren't using C or C++.

Some people would argue that you can use D for equally high performance things to C++, and make sure you use the GC very selectively, etc. However, you don't appear to be making that argument. If you aren't, then there's just no real point comparing C++ and D. If you don't have any of those requirements, and you are ok with obscurity, you have much stiffer competition from many other languages like Haskell, Kotlin, etc.

Re:D as a C Replacement by WalterBright on Tuesday July 02, 2019 @10:48PM (#20341203)
There are people who specifically use D for writing high performance applications. They find it faster than C++ for a rather subtle reason - D code is more plastic, meaning it is easier to refactor D code trying out different algorithms looking for a faster one.
Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @12:31AM (#20341582)
I'm sure these people exist but they're the exception rather than the rule. The two biggest industries in SG14, the C++ low latency study group, are games and HFT. In both these areas, D penetration is pretty much zero. And in HFT at least while many features would be nice (especially reflection), I can't imagine it would be anything but a performance hit. Just the fact that the best D implementation for performance is llvm based, and most people do not find that llvm produces assembly as good as gcc or icc, is already an instant global hit.
Re:D as a C Replacement by WalterBright on Wednesday July 03, 2019 @12:55AM (#20341688)
D reflection is at compile time, not runtime, so no performance hit.
Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @02:06AM (#20341999)
I didn't intend to imply otherwise but I see my wording was unclear. I meant that D generally would be a hit, though it's only really speculation either way.
Re:D as a C Replacement by WalterBright on Wednesday July 03, 2019 @02:12AM (#20342028)
There is no inherent reason that D code would be slower than C++ code.

I should know, I've written a C++ compiler and know where the bloat is buried, and designed D to eschew features that would be inherently slower than the C++ counterparts.

You can even turn off the runtime array bounds checking.

Re:D as a C Replacement by p0nce on Wednesday July 03, 2019 @05:03AM (#20342764)
If you use the same backend... no. They are both native languages with "no room below".

llvm vs icc could be a hit, but that's all really.

Re:D as a C Replacement by GoblinSlayer on Wednesday July 03, 2019 @09:14AM (#20343993)
Re:D as a C Replacement by mhh__ on Wednesday July 03, 2019 @01:35AM (#20341873)
LLVM and GCC are so close together these days that it makes almost no difference practically, and when it does you should start optimizing based around your code not your compiler.

Root of all evil etc. etc.

Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @02:14AM (#20342033)
Dude I mean the context in which I'm discussing this is a large organization operating on timescales shorter than microseconds with numerous people involved in optimizing every single part of the pipeline. We are waaaaaaaaaaay past the point of "premature" optimization. This is just optimization, and optimization where a few percent difference between compilers is huge.

Your comments read like you're explaining optimization to a beginner, it's a bit bad faith tbh.

Re:D as a C Replacement by mhh__ on Wednesday July 03, 2019 @03:05AM (#20342234)
https://www.phoronix.com/scan.php?page=article&item=gcc-clan...

GCC and LLVM have broadly similar performance characteristics(i.e. for any arch/uBenchmark combo there is another that puts the other one faster).

If GCC is appreciably faster for your purpose, then fair enough but LLVM is not drastically slow by any means (Especially when you consider that any "Anything you can do..." Between LLVM and GCC moves much faster on the LLVM side due to a much saner codebase)

Re:D as a C Replacement by erk__ on Wednesday July 03, 2019 @03:45AM (#20342423)
You are talking about it like comparing a Ford to a VW they are pretty much the same, but what the poster above talks about is more like comparing formula 1 cars where a single percent of engine power can win or lose you the race. The difference may be small enough for by far the most purposes, but this one is where the small difference can cost a lot.
Re:D as a C Replacement by GoblinSlayer on Wednesday July 03, 2019 @09:19AM (#20344020)
Hmm... the thread started with GC, are you sure you want to allocate memory while struggling for the last percent of performance?
Re:D as a C Replacement by pjmlp on Wednesday July 03, 2019 @01:26AM (#20341837)
Both of which use their own C++ subset, while ignoring the standard library.

So even C++ isn't good enough for them.

Yet a few vocal anti-C++ devs in the game world, from Insiomatic Games are now at Unity driving HPC# efforts, so there's that.

Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @02:05AM (#20341992)
HFT doesn't subset C++ nor does it ignore the standard library. Given you're totally wrong about one I'm not inclined to believe you on the other, and I have an examples from the standard library used in game dev eg atomics.
Re:D as a C Replacement by WalterBright on Wednesday July 03, 2019 @02:09AM (#20342014)
The people I know using C++ for games wrote their own atomics library :-)
Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @02:41AM (#20342144)
Well you can certainly elect to torture yourself if you really want to. Atomics in C++ compile to assembly in very straightforward ways and there isn't really an enormous design space there. Preshing certainly makes it seem like Ubisoft is using C++ atomics; if it makes sense for them with so many developers and creating AAA games... I'd be curious to know the motivation for writing their own.
Re:D as a C Replacement by jcelerier on Wednesday July 03, 2019 @03:27AM (#20342329)
> Atomics in C++ compile to assembly in very straightforward ways

in release mode.

Re:D as a C Replacement by pjmlp on Wednesday July 03, 2019 @02:26AM (#20342092)
Disabling RTTI and exceptions is subseting C++, as it is outside of ISO C++ requirements, even if is common support among C++ compilers.

As for game developers, you don't have to look any further than Unreal, CryEngine or EASTL, or a couple of talks at GDC Vault.

So who's right?

Re:D as a C Replacement by quicknir on Wednesday July 03, 2019 @02:53AM (#20342183)
Except that in HFT we don't generally disable RTTI or exceptions. I work at a top HFT firm, have friends/colleagues at other top firms, have seen multiple people like Carl Cook at Optiver state in talks that they use exceptions... So what on Earth are you talking about?

Not using some or most of the standard library is precisely not an example of subsetting C++. Just because the C++ standard library has a hash table available doesn't mean that every project has to use it. Companies standardizing their own high performance data structures where it makes sense, and other things as well, is just something that happens and often makes sense independent of language.

Re:D as a C Replacement by p0nce on Wednesday July 03, 2019 @09:24AM (#20344052)
> we don't generally disable RTTI or exceptions

Keep up the good fight! "Exceptions are slow" is a realy outdated opinion

Re:D as a C Replacement by pjmlp on Wednesday July 03, 2019 @02:57AM (#20342195)
I guess Herb Sutter is all wrong on his talks then, what does he know.
Re:D as a C Replacement by scythe on Tuesday July 02, 2019 @11:40PM (#20341417)
Ultimately, though, this is the real tragedy of D. It started as a C clone, and continues to be named like it's a C clone, but it is no longer a C clone. It is a general-purpose memory-aware compiled programming language inspired by C++, but it is pretty different by now! People expecting a C clone miss what else it has to offer.

(Can it be a C replacement? Sure, but so can Rust/Swift/Go/etc)

Re:D as a C Replacement by riffraff on Wednesday July 03, 2019 @01:57AM (#20341962)
I don't believe it ever was a C clone. I remember it always presented as a C _successor_, like C++, but in a different direction.
Re:D as a C Replacement by makapuf on Wednesday July 03, 2019 @03:38AM (#20342387)
Go (and some others, swift surely) cannot be used as a C replacement since they need a runtime.
Re:D as a C Replacement by pjmlp on Wednesday July 03, 2019 @07:09AM (#20343226)
Just like C does for calling into main(), doing floating point emulation if CPU not available, calling library initializers (common C extension), handling VLA allocations.

Just because it is tiny does not make it inexistent.

Then there is the whole POSIX, which is kind of runtime that wasn't made part of ISO C, but follows it everywhere.

Re:D as a C Replacement by makapuf on Wednesday July 03, 2019 @06:22PM (#20349522)
It's not necessarily tiny but it's optional (besides main() ). Linux kernel, baremetal embedded does not use this.
Re:D as a C Replacement by pjmlp on Thursday July 04, 2019 @02:27AM (#20352056)
Linux kernel surely used it during the time they had VLAs in, they are now mostly removed thanks to Google efforts reducing memory exploits on the Linux kernel.

One just links another implementation, just like printf() vs printk().

Re:D as a C Replacement by GoblinSlayer on Wednesday July 03, 2019 @09:36AM (#20344138)
libc.so.6 is about 2MB.
[dead]
Re:D as a C Replacement by dang on Wednesday July 03, 2019 @02:07AM (#20342005)
Please don't be rude or aggressive in HN comments. It only makes this place worse, and we're trying to have a forum that doesn't go down in flames.

https://news.ycombinator.com/item?id=20340078 was much better.

https://news.ycombinator.com/newsguidelines.html

Re:D as a C Replacement by umanwizard on Tuesday July 02, 2019 @11:24PM (#20341357)
I’ve seen large, poorly written Java programs[0] where an entire core at 100% is dedicated to the GC. I have never seen that behavior for things like ~shared_ptr in C++ or similar languages.

Then again, OOMing due to leaks is something you’re more likely to see in poorly-written systems in non-GC languages (Objective-C, C, C++, etc), so there are certainly trade offs.

[0]: Maybe “poorly written” is unfair. Perhaps “written by people who are unaware of how to write code that performs well in a GC system”.

Re:D as a C Replacement by krzat on Wednesday July 03, 2019 @02:49AM (#20342165)
Probably the most important usage of C are shared libraries. How well it will work if I have D library with it's own GC, another Go library with second GC and app written in C# with yet another GC?
Re:D as a C Replacement by jcelerier on Wednesday July 03, 2019 @03:16AM (#20342287)
> Garbage Collection is often faster than RC as long as pause times are acceptable

I have never in my life worked in any kind of software where this was remotely acceptable

Re:D as a C Replacement by rowanG077 on Wednesday July 03, 2019 @08:13AM (#20343574)
You are right most software it doesn't matter if you have a GC. However then D is not competing against C and C++ it's competing against almost every other language.
Re:D as a C Replacement by nicwilson on Tuesday July 02, 2019 @09:33PM (#20340866)
Mere presence shouldn't.

If you never allocate from it (which is able to be verified very easily with the @nogc attribute), it will never trigger. You can disable it and trigger when you feel it is needed. You can choose to just leak.

The default GC is OK, but not great. However it recently was made multithreaded in the mark phase so it is much faster than it used to be. There is a fork based (on linux only IIRC) GC, used by several companies for hard realtime systems, that marks and sweeps in a separate process with CoW pages.

Re:D as a C Replacement by WalterBright on Tuesday July 02, 2019 @10:45PM (#20341193)
The -betterC switch to the compiler compiles a D subset that doesn't have a GC.
Re:D as a C Replacement by newacct77 on Tuesday July 02, 2019 @06:47PM (#20340078)
D's garbage collector can be disabled, so would you mind elaborating?
Re:D as a C Replacement by CameronNemo on Tuesday July 02, 2019 @07:50PM (#20340417)
How well is that supported by the ecosystem?
Re:D as a C Replacement by p0nce on Tuesday July 02, 2019 @09:21PM (#20340824)
Not using the GC is rather easy, there are plenty of mitigation methods. Not using the D runtime is much more involved. Not linking with the D runtime is a more hardcore endeavour.
Re:D as a C Replacement by dymk on Tuesday July 02, 2019 @08:01PM (#20340489)
Very poorly
Re:D as a C Replacement by rowanG077 on Wednesday July 03, 2019 @08:11AM (#20343566)
Yes and then you can't use most of the nice features of D. Forget about libraries.
Re:D as a C Replacement by edwintorok on Tuesday July 02, 2019 @06:36PM (#20340019)
What makes you choose it over Rust or Go?
Re:D as a C Replacement by 6thaccount2 on Tuesday July 02, 2019 @07:01PM (#20340150)
Not OP, but I can guess.

D has a built-in GC by default and doesn't force you to manage memory like Rust. Also, the author mentioned that D is pretty straightforward to anyone with some basic programming knowledge (think C, C++, Java, and C# which fits the majority of coders). Rust is very different from those languages. My main goto language is Python and I can understand D a lot more than Rust which confuses me to no end (disclaimer: I'm a total noob).

Go is probably as easy to read as D if not more and is also GC and fast. It is also very spartan. I bet D has a lot of additional features.

Re:D as a C Replacement by petre on Wednesday July 03, 2019 @01:38AM (#20341887)
With Rust I always find myself fighting the compiler just like with C, it's very strict about everything, no GC, steep learning curve.

Go is okay but you can't get clever with it. You're productive from the start butit becomes routine quite fast and at a certain point there isn't anything new to learn. Error checking is also quite annoying.

D has the best of both worlds and it also plays nicely with C/C++, something that the author of Zig also understood is a must have in any language that aims to be a better C/C++. In D one is productive from the start and there's always something new to learn. And one learns it as they code, without having to first read Programming in D by Ali Çehreli to make any sense of the language.

I also tried Crystal and liked it but one is more productive in D and Go without any prior knowledge of those languages. I was kind of annoyed by the fact that Crystal removed the for loop, which works perfectly fine in Ruby.

Re:D as a C Replacement by lordnaikon on Wednesday July 03, 2019 @02:58AM (#20342198)
If you fight with the rust compiler the same amount as you do with a C compiler than you're making something wrong in both languages. Don't get me wrong but the rust and C compiler are on the direct opposite of the spectrum – for me. I love how the rust compiler is holding my hand to avoid all the nasty bugs i would introduce into my code.
Re:D as a C Replacement by petre on Wednesday July 03, 2019 @03:57AM (#20342466)
The overall experience is similar. Rust definitely has better error messages. The difference is that in C once your program actually compliles it can also easily segfault at runtime. With D and Go you just compile the program and it works. It only complains when you're doing something awfully wrong instead of being a grammar nazi all the time. Additionaly, D allows you to enable or disable certain features if you wish to. Want safety? Use @safe and @trusted functions. Want to disable the GC? Use @nogc functions. And the list goes on. One can enable all of these on demand.

https://dlang.org/spec/function.html#function-safety

Re:D as a C Replacement by edwintorok on Wednesday July 03, 2019 @05:51AM (#20342951)
You can opt-in to a Gc with Rust via Rc<> and Arc<>: https://words.steveklabnik.com/borrow-checking-escape-analys...

Don't get me wrong, I like a language with a Gc by default, my favourite language is OCaml, but I can't claim it to be a drop-in replacement for C projects. Go, Rust (or D) stand a better chance at that.