News for hackers. Stuff that matters.
Re:Why Is C Faster Than Java (2009) by akkartik on Sunday December 26, 2021 @10:50PM (#29698932)
The flip side of getting great performance is all the exposure to security vulnerabilities.

Git seems to have an excellent security track record: https://www.cvedetails.com/vulnerability-list/vendor_id-4008... However, I think the right claim here is, "be very sure you have the chops to benefit from C's speed without compromising security."

Re:Why Is C Faster Than Java (2009) by chii on Sunday December 26, 2021 @11:02PM (#29699031)
> getting great performance is all the exposure to security vulnerabilities.

ironic to post this right after java's biggest vuln to date (log4shell).

Re:Why Is C Faster Than Java (2009) by winrid on Sunday December 26, 2021 @11:06PM (#29699061)
Which had nothing to do with Java or how it manages memory. You could have the same vuln in NodeJS or Python.
Re:Why Is C Faster Than Java (2009) by lelanthran on Monday December 27, 2021 @01:41AM (#29700076)
> Which had nothing to do with Java or how it manages memory. You could have the same vuln in NodeJS or Python.

I think parent was pointing out that the biggest and costliest security exploit ever found had nothing to do with buffer overflows, memory management, etc.

Re:Why Is C Faster Than Java (2009) by sgift on Monday December 27, 2021 @02:08AM (#29700208)
It seems almost impossible to say if log4shell was bigger or more costly than Heartbleed or the Debian OpenSSL bug (there are probably still keys out there made with the damaged randomness). Log4shell is just in recent memory.
Re:Why Is C Faster Than Java (2009) by lelanthran on Monday December 27, 2021 @02:41AM (#29700378)
> It seems almost impossible to say if log4shell was bigger or more costly than Heartbleed or the Debian OpenSSL bug (there are probably still keys out there made with the damaged randomness). Log4shell is just in recent memory.

Seems pretty clear to me - Heartbleed (and all the other serious memory exploits) required a great deal of skill and a lot of luck to exploit, and in return you either don't get a remote execution, or you get a very tiny chance of a remote execution.

In comparison, log4j is about as easy to exploit into an RCE as it is to use curl.

Log4j is a guaranteed remote-execution exploit just by filling in a user facing form with the correct URL, while memory exploits are not guaranteed to result in an RCE, requires more skill than simply typing into an input box or an email.

Re:Why Is C Faster Than Java (2009) by brabel on Monday December 27, 2021 @04:06AM (#29700825)
> Log4j is a guaranteed remote-execution exploit just by filling in a user facing form with the correct URL

This is only true of systems that were using very outdated JVM versions... on the newer ones (we're talking 2016 or newer JDK releases, not like last month), you would need to pull off a serialization exploit to indirectly get RCE, which is quite a bit harder than sending a HTTP request.

Re:Why Is C Faster Than Java (2009) by srfilipek on Monday December 27, 2021 @08:43AM (#29702441)
> Heartbleed (and all the other serious memory exploits) required a great deal of skill and a lot of luck to exploit, and in return you either don't get a remote execution, or you get a very tiny chance of a remote execution.

Heartbleed wasn't about RCE at all. It was about memory disclosure -- memory that contained secret signing keys. The fallout was that keys needed to be revoked and rotated.

Reading out memory and extracting the secret keys was actually pretty simple. There were multiple POCs available.

Re:Why Is C Faster Than Java (2009) by bajsejohannes on Monday December 27, 2021 @03:56AM (#29700769)
Spectre and Meltdown too. How much performance loss did we have to make up for because of that?
Re:Why Is C Faster Than Java (2009) by bruce343434 on Monday December 27, 2021 @04:31AM (#29700957)
That's to do with CPU speculative execution, not C or Java
Re:Why Is C Faster Than Java (2009) by tored on Monday December 27, 2021 @06:54AM (#29701728)
Actually it does

https://queue.acm.org/detail.cfm?id=3212479

"The root cause of the Spectre and Meltdown vulnerabilities was that processor architects were trying to build not just fast processors, but fast processors that expose the same abstract machine as a PDP-11. This is essential because it allows C programmers to continue in the belief that their language is close to the underlying hardware."

Re:Why Is C Faster Than Java (2009) by bruce343434 on Monday December 27, 2021 @08:13AM (#29702215)
Just because an academicist has an opinion does not make it fact. That quote reads like a blogpost and with its lack of citation it might as well be. Yes, it sounds plausible. but well.

Let's assume it's true: your argument would be "intel made a mistake, but since they would have only made that mistake when doing stuff that appeases C programmers (would they have?), it's actually because of C."

Now, I think this is a bit of a stretch.

ETA: or did you mean that it has to do with C for that reason? in which case, ok, I see how you mean.

Re:Why Is C Faster Than Java (2009) by verall on Monday December 27, 2021 @10:44AM (#29703373)
Oh yea, if not for emulating the PDP-11, processor designers would have no interest in instruction level parallelism.

This article is pretty funny actually:

> On a modern high-end core, the register rename engine is one of the largest consumers of die area and power. To make matters worse, it cannot be turned off or power gated while any instructions are running

Yea, let's just gate off the RAT. What's it for again?

Re:Why Is C Faster Than Java (2009) by will4274 on Monday December 27, 2021 @08:25PM (#29709227)
Java exposed the same abstract machine as a PDP-11 too. The key "PDP-11" thing is that all memory is treated as equally accessible, rather than the reality - i.e. that some memory is in caches on certain cores only and can therefore be accessed more efficiently on those cores.
Re:Why Is C Faster Than Java (2009) by kaba0 on Monday December 27, 2021 @04:12AM (#29700850)
How is it the biggest and costliest security exploit ever? With even the most basic of firewalls a server should absolutely have it is not really exploitable. I’m not trying to downplay it, but I really don’t see how is it even remotely close to some ssh bugs.
Re:Why Is C Faster Than Java (2009) by vbezhenar on Monday December 27, 2021 @04:56AM (#29701095)
Basic firewall allows outgoing connections, so it wouldn't help.
Re:Why Is C Faster Than Java (2009) by Traubenfuchs on Monday December 27, 2021 @05:08AM (#29701154)
I‘d argue that not allowing your servers to make outgoing calls to non whitelisted targets is very basic security.
Re:Why Is C Faster Than Java (2009) by vbezhenar on Monday December 27, 2021 @07:03AM (#29701784)
No, it's not. I can easily configure open ports using firewall-cmd. That is basic security. And there's no dedicated options to configure outgoing calls, there's no sane defaults to start with (I have no idea which targets should be whitelisted: ntp? update servers? anything else?), there's no system-wide integration, like my dnf can choose different mirror every time it runs.

Of course it makes sense to configure outbound white list, but there's no infrastructure in RHEL or Ubuntu and nobody's going to bother with custom scripts for that.

Re:Why Is C Faster Than Java (2009) by varjag on Monday December 27, 2021 @05:35AM (#29701283)
Are we still talking about the bug that relies on poorly chosen defaults?
Re:Why Is C Faster Than Java (2009) by formerly_proven on Monday December 27, 2021 @09:30AM (#29702736)
Do you disable DNS on your servers as well or use a restricted internal-only resolver?
Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @04:10AM (#29700841)
The point is that not every security problem stems from the memory model, and myopically focusing on memory safety evidently doesn't stop do much to prevent vulnerabilities.
Re:Why Is C Faster Than Java (2009) by ekidd on Monday December 27, 2021 @07:31AM (#29701962)
According to Microsoft's data, about 70% of security vulnerabilities are memory safety bugs. So definitely not all, but taking them off the table makes a big difference.

https://www.zdnet.com/article/microsoft-70-percent-of-all-se...

Another big chunk of bugs, including forgetting to escape strings, can often be reduced by building strongly-typed APIs that distinguish between "String", "Sql" and "Html" types.

Java actually does quite well by these metrics. It's memory safe, tries to eliminate undefined behaviors, and it has an adequate type system. However, the mere existence of runtime code-loading is a risk, as we saw with Log4j.

Re:Why Is C Faster Than Java (2009) by foobiekr on Monday December 27, 2021 @01:35PM (#29705266)
I admit that I like C, but I would use it only sparingly (if at all) professionally at this point despite expert-level C experience and skill. However, that 70% figure is deeply saddening to someone coming at this from a C perspective - C is _dire_ in safety terms and I have personally found and fixed a large number of C bugs. The idea that it's _only_ 70% is pretty sad, because it means we are well and truly doomed.

I write this tongue in cheek, obviously. I've seen dire, dire security bugs in Java in particular but also in terms of fitting parts together (lo, broken ACLs, useless AWS SGs, inter-process assumptions that don't hold, injection vulnerabilities of myriad types, etc.). The truth is, we are doomed, and not just because of the 70%.

Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @10:00AM (#29702978)
I imagine there would be quite a lot of selection bias involved there, since memory safety bugs is the class of errors that is easiest identify.

Unless Microsoft claims to have identified all bugs. That would be a bold claim indeed.

Re:Why Is C Faster Than Java (2009) by adgjlsfhk1 on Monday December 27, 2021 @11:09AM (#29703628)
it might not be all bugs, but 70% of all found bugs is still a lot of bugs that were completely preventable with better language choice.
Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @11:12AM (#29703651)
I'm not convinced this statistic is saying more than that these bugs are easily identifiable. There is a lot of tooling for identifying memory errors, and virtually none that could identify something like log4shell.
Re:Why Is C Faster Than Java (2009) by adgjlsfhk1 on Monday December 27, 2021 @12:28PM (#29704530)
they are more easily identifiable, but they are also very simple. when writing C, you probably write a potential memory safety bug roughly every 100 lines. Even if you detect 99% with automated tooling that's still a pretty sizable attack surface.
Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @12:31PM (#29704550)
> probably write a potential memory safety bug roughly every 100 line

If that is what your C code looks like, then yeah, you should probably not be writing C code.

Re:Why Is C Faster Than Java (2009) by adgjlsfhk1 on Monday December 27, 2021 @01:26PM (#29705166)
every array access and string comparison is a potential vulnerability in C. I'm not saying that in practice, all of them will be vulnerable, just that every one of those is a place where you could miss a check and end up with a memory bug.
Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @02:02PM (#29705604)
That's kind of a disingenuous way to reason about it. You absolutely can bounds check your memory accesses in C, and bounds-checked accesses are as safe in C as they are in any other language.
Re:Why Is C Faster Than Java (2009) by adgjlsfhk1 on Monday December 27, 2021 @02:21PM (#29705815)
the point isn't that it's impossible to write C that's safe, it's that doing so requires 100% success rate of a human implementing something correctly. furthermore, the main cases where C can get a performance benefit over safer languages are where there is a complicated invariant that ensures safety. the problem is that these are incredibly easy to break during refactoring, or when a different dev modifies the code later. compilers are much better then humans at verifying that code is correct.
Re:Why Is C Faster Than Java (2009) by larsrc on Monday December 27, 2021 @04:04AM (#29700816)
It has everything to do with Java, though not memory. It exploits Java's write-once-run-everywhere feature as well as the decision to initialize classes, including running code, on load rather than on first instantiation.
Re:Why Is C Faster Than Java (2009) by watwut on Monday December 27, 2021 @06:18AM (#29701506)
Classes are loaded when they are about to be used for the first time.
Re:Why Is C Faster Than Java (2009) by ajross on Sunday December 26, 2021 @11:22PM (#29699173)
Which argues that maybe language choice just doesn't matter that much for security. It's true that C allows a class of mistakes that don't exist in higher level runtimes. It's equally true that this class of mistakes represents an increasingly vanishing share of real world exploits. Static analysis tools and runtime hardening techniques don't "fix" C, exactly. But in practice they work well enough to push C's foibles down into the noise floor.

But at the same time, C remains, and will probably always remain, the easiest language on which to tune and optimize. It's not going anywhere. Our grandkids will still be using systems with C firmware at their core.

Re:Why Is C Faster Than Java (2009) by friedman23 on Sunday December 26, 2021 @11:41PM (#29699328)
> Which argues that maybe language choice just doesn't matter that much for security

No it doesn't. There is actually no logical way to infer from the parent statement that language choice doesn't matter for security.

> But in practice they work well enough to push C's foibles down into the noise floor.

This goes against findings from research that has been done into the sources of security vulnerabilities. Microsoft and the chrome dev teams have published that 70% of their security bugs are a result of memory safety.

If C is used in 100 years it will only be because of inertia. Today there are better choices in the domain of low level systems programming languages.

Re:Why Is C Faster Than Java (2009) by VRay on Monday December 27, 2021 @02:01AM (#29700181)
> If C is used in 100 years it will only be because of inertia. Today there are better choices in the domain of low level systems programming languages.

I'm not saying you're wrong, but there's effectively zero real kernel work done in anything other than c and c++. Tons of well-known open source Rust/microkernel stuff exists here in public, but behind closed doors is where almost all firmware work is happening. When someone at Microsoft, Apple, Qualcomm, Samsung, etc sits down to code firmware for billions of devices, it happens in c. I've never seen a serious proposal to switch to managed code at any of my jobs, either

I think we'll see more and more complex stuff move out of the kernel, but I don't think c is going the way of COBOL in the next 20 years at least. I'M definitely not going to start using Rust on my own, and it would take a pretty compelling case from management or a junior engineer to make me switch in the future.

Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:41AM (#29701005)
> When someone at Microsoft, Apple, Qualcomm, Samsung, etc sits down to code firmware for billions of devices, it happens in c.

Many of those companies are now in C++, actually.

Re:Why Is C Faster Than Java (2009) by kovac on Monday December 27, 2021 @01:47AM (#29700103)
Web browsers are one of the most poorly designed applications in existence. It's not surprising that such complex applications trying to do everything possible have vulnerabilities. But in no way that should serve as a benchmark for C as being inherently unsafe when far more important systems like databases, operating systems, system libraries are written in c just fine. Most of the common vulnerabilities in general are due to unnecessary complexity of systems that lend themselves to poor programming practices, configuration errors, low calibre programmers, etc (OWASP list for example). Memory safety vulnerabilities tend to just get more attention since they are in critical parts of a system, hard to exploit and so attracts highly sophisticated exploits that affect us at a nation state or industry level. If these systems were written in high languages by those high level developers, I'd go nowhere near any computer system.
Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:42AM (#29701011)
They aren't just fine as proven by CVE database.
Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:39AM (#29700997)
There were already better options before C came to be, it was UNIX being originally free that made it available everywhere.
Re:Why Is C Faster Than Java (2009) by umanwizard on Sunday December 26, 2021 @11:32PM (#29699260)
> It's equally true that this class of mistakes represents an increasingly vanishing share of real world exploits.

Is this true? It seems implausible to me.

Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @01:15AM (#29699947)
> Which argues that maybe language choice just doesn't matter that much for security.

I think it just means that these languages all have elevated potential for security issues, but there are languages without pervasive gratuitous dynamism or memory problems.

> Static analysis tools and runtime hardening techniques don't "fix" C, exactly. But in practice they work well enough to push C's foibles down into the noise floor.

But that’s all additional effort to integrate these tools and practices onto a language which already has a very low iteration velocity (all of the time spent debugging memory issues, package issues, build system issues, etc which simply don’t exist in many modern languages).

> But at the same time, C remains, and will probably always remain, the easiest language on which to tune and optimize. It's not going anywhere. Our grandkids will still be using systems with C firmware at their core.

This sounds like a concession to me. Of course C will smolder on in obscure, legacy firmware long after it becomes obscure—so did COBOL, but we don’t pretend COBOL’s vestigial existence is owed to its merits rather than a quirk of history.

This is a fine and normal thing. C did it’s job for a time, but languages aren’t emerging which are better suited to modern computing requirements. This process will continue and these languages which are chipping away at C’s market share will be eroded themselves eventually.

Re:Why Is C Faster Than Java (2009) by yjftsjthsd-h on Monday December 27, 2021 @10:31AM (#29703261)
> we don’t pretend COBOL’s vestigial existence is owed to its merits rather than a quirk of history.

Um. COBOL survived so long specifically because it did some things better than alternatives, mostly around how it handled numbers. Yes, also inertia and historical accident, but also because it was actually good at its job.

Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @11:37AM (#29703909)
It beat out others of its day on merit, as did C, but we’re talking about C and COBOL competing against modern languages in a modern landscape. In other words, COBOL’s dominance in the 60s was due to merit, but it’s vestigial existence today is a historical artifact—it isn’t simply the best language for the application.
Re:Why Is C Faster Than Java (2009) by lmm on Monday December 27, 2021 @12:19AM (#29699549)
Not a flaw in Java, a flaw in a specific overengineered logging framework. Relevantly, not a flaw that applies to JGit.
Re:Why Is C Faster Than Java (2009) by echelon on Monday December 27, 2021 @12:30AM (#29699640)
Runtime classloaders expose a huge blast radius. That's unfortunately a Java language feature.
Re:Why Is C Faster Than Java (2009) by marginalia_nu on Monday December 27, 2021 @07:13AM (#29701852)
I guarantee Java would be vulnerable to the same category of errors even without runtime class loaders. Java puts a lot of emphasis on dependency injection, and has done this for a fairly long time. This takes the form of having classes pull dependencies themselves through some central registry over explicit construction.

It's arguably a symptom of a larger problem the ecosystem's sheer size.

Re:Why Is C Faster Than Java (2009) by tsimionescu on Monday December 27, 2021 @02:48AM (#29700413)
As does dynamic linking, which is perfectly equivalent.
Re:Why Is C Faster Than Java (2009) by runeks on Monday December 27, 2021 @07:14AM (#29701861)
True, but that doesn’t make it better.
Re:Why Is C Faster Than Java (2009) by tsimionescu on Monday December 27, 2021 @01:24PM (#29705139)
Well, there are 0 languages that don't support dynamic linking, so I don't see why we would single out Java.
Re:Why Is C Faster Than Java (2009) by runeks on Tuesday December 28, 2021 @03:07AM (#29711569)
Dynamic linking is not a language feature, it’s a feature of the operating system. If we’re talking about dynamic loading, there are plenty of languages that don’t support this natively, but only through its C bindings (e.g. Haskell).
Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @12:52AM (#29699798)
Not only a feature but an idiom.
Re:Why Is C Faster Than Java (2009) by larsrc on Monday December 27, 2021 @04:07AM (#29700830)
And made worse by static initializers being run when the class is loaded rather than when the class is first instantiated.
Re:Why Is C Faster Than Java (2009) by vbezhenar on Monday December 27, 2021 @04:57AM (#29701098)
Class is loaded when it's first instantiated, unless you're explicitly loading it.
Re:Why Is C Faster Than Java (2009) by lmm on Monday December 27, 2021 @12:54AM (#29699812)
Most languages' built-in serialization has similar issues, e.g. Python pickle.
Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @01:21AM (#29699979)
Even if this is true, most languages don’t use dynamic code loading as often as Java.
Re:Why Is C Faster Than Java (2009) by lmm on Monday December 27, 2021 @01:48AM (#29700115)
In what sense? In many popular languages (Perl/Python/Ruby/...) all code loading is dynamic. Java does have more of a built in RMI framework than most languages, but it's rarely used in modern code.
Re:Why Is C Faster Than Java (2009) by nine_k on Monday December 27, 2021 @02:30AM (#29700326)
Pick a few random executables from your OS, check how many .dll or .so libraries do they dynamically link to.

For Python, JavaScript, Ruby, etc it's not even a question.

Re:Why Is C Faster Than Java (2009) by netheril96 on Monday December 27, 2021 @02:48AM (#29700411)
You practically cannot have the same vulnerability in C, because no one would bother implementing that kind of flexibility in C.
Re:Why Is C Faster Than Java (2009) by dtech on Monday December 27, 2021 @05:11AM (#29701170)
"Our language is harder to use than X, so it's safer because no-one will try to do certain things with it" is hardly a compelling defence.
Re:Why Is C Faster Than Java (2009) by rat9988 on Monday December 27, 2021 @10:06AM (#29703032)
It wasn't meant as a defence.
Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:44AM (#29701016)
Another one that never saw the heyday of enterprise C code.
Re:Why Is C Faster Than Java (2009) by lmm on Monday December 27, 2021 @04:27AM (#29700939)
And if you disconnect your computer from the internet then it's safe from most remote attacks.
Re:Why Is C Faster Than Java (2009) by reyqn on Monday December 27, 2021 @06:16AM (#29701490)
Was there not a flaw in the jndi api for ldap? log4j was just the entry point from what I understood.
Re:Why Is C Faster Than Java (2009) by drdec on Monday December 27, 2021 @07:32AM (#29701968)
No, JNDI is an API for accessing remote code. It stands for Java Naming and Directory Interface. Not supporting LDAP, the most common remote directory, would have been a flaw.

The flaw was in log4j's use of the library which allowed remotely connecting and executing code by default based on connection strings which were possibly user-entered.

Re:Why Is C Faster Than Java (2009) by reyqn on Monday December 27, 2021 @06:49PM (#29708399)
Ok so JNDI's purpose is actually accessing remote code. You just have to not give it any untrusted data. It seems like a dangerous tool to use.
Re:Why Is C Faster Than Java (2009) by watwut on Monday December 27, 2021 @04:01AM (#29700796)
And it was easy to fix vulnerability. Easy to workaround (turn off) with command line switch.

It was expensive, simply because that library is extremely popular.

Re:Why Is C Faster Than Java (2009) by johnisgood on Monday December 27, 2021 @11:05AM (#29703593)
Well... buffer overflows are an easy fix, too, and yeah, popular projects get more attention and scrutiny.
Re:Why Is C Faster Than Java (2009) by 1vuio0pswjnm7 on Monday December 27, 2021 @12:54AM (#29699811)
There are other factors. Not every application of C has the same amount of exposure to security vulnerabilities.

On Github I see people rewriting simple UNIX utilties, e.g, cat, in some "safe" language, e.g., Rust. Yes, it is possible there could be some exploit based on an error in cat, maybe triggered by some malicious input that the user fails to detect, but this would not be a concern to the same degreee as errors in large, complex applications with network and other privileged access that are relatively new. For example, using C to write cat versus using C to write git.

Exposure to security vulnerabilities depends not only on the language used, but also when, where and how the application is used.

Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @01:28AM (#29700020)
Security is only one reason to rewrite utilities in another language. In particular for these foundational utilities, it means more of your dependency tree can be reproducibly built without the help of specialist package maintainers. Other reasons also include “fun” and “learning”.
Re:Why Is C Faster Than Java (2009) by kovac on Monday December 27, 2021 @01:59AM (#29700171)
Considering that package mgmt is a well solved problem, rewriting these tools for that in return for slow, bloated alternative sounds like a huge tradeoff to me. Fun and learning on the other hand, sure.
Re:Why Is C Faster Than Java (2009) by the_duke on Monday December 27, 2021 @03:02AM (#29700479)
Rust rewrites are often excellent and very fast. (ripgrep, fd, ...)

A big reason is that they are usually multi-threaded, while the C counterparts are not.

Writing (memory)safe and maintainable parallel code is much, much easier in Rust than in C.

Re:Why Is C Faster Than Java (2009) by blub on Monday December 27, 2021 @03:53AM (#29700752)
Can’t say I disagree with you. A multi-threaded, memory safe cat sounds like a worthwhile future project for the Rust community.
Re:Why Is C Faster Than Java (2009) by fractalb on Monday December 27, 2021 @04:25AM (#29700925)
I didn’t understand the need for a multi-threaded version of cat. File reading/writing is a sequential thing, right?
Re:Why Is C Faster Than Java (2009) by kreeben on Monday December 27, 2021 @06:09AM (#29701441)
AFAIK operating systems delegate reads/writes to their IO subsystem. This subsystem can optimize the IO operations by scheduling them in an order that differs from when they arrived to the IO queue and this improves performance in some cases. Instead of FIFO the subsystem operates on the segment of your storage device that is closest to its current location. Modern IO subsystems are both thread-safe and smarter than the average programmer.
Re:Why Is C Faster Than Java (2009) by adrianN on Monday December 27, 2021 @02:30AM (#29700327)
I'm not so sure that package management is really a well solved problem. Sure, there are some solutions, but as far as I know all leave some things to be desired.
Re:Why Is C Faster Than Java (2009) by nineteen999 on Monday December 27, 2021 @04:48AM (#29701047)
The obvious solution, of course, is to write yet another package manager.
Re:Why Is C Faster Than Java (2009) by YPPH on Sunday December 26, 2021 @10:53PM (#29698957)
Is that necessarily the case? From what I've heard, Rust offers great performance and safety.
Re:Why Is C Faster Than Java (2009) by II2II on Sunday December 26, 2021 @11:29PM (#29699228)
From my understanding, Rust performs more compile time checks for safety. In those cases, you may be able to maintain the performance of C without sacrificing safety. On the other hand, you may still get away with writing more performant C code since C allows the developer to play fast and loose with the rules. I'm not saying that is a good thing, but git can get away with it since it since it probably attracts very competent developers.

The other thing mentioned in the post was eliminating overhead. My limited experience with Rust leads me to believe it is closer to C++ than C in this respect. That is to say there is a lot of higher level functionality that you can use and it is optimized for performance in the general case. On the other hand, that functionality is still for the general case, so a competent developer who has a thorough knowledge of what the program is trying to accomplish will be able to write more performant code.

Re:Why Is C Faster Than Java (2009) by nine_k on Monday December 27, 2021 @02:35AM (#29700351)
That C allows developers to play fast and loose is actually one of the reasons why C is slower than Fortran in many cases. Aliasing does not exist in Fortran, and array dimensions are well-defined, and this alone allows for much more aggressive optimizations, impossible to do safely in C.

(Is Fortran still relevant? Try compiling Tensorflow, to say nothing of serious numerical modeling stuff. And yes, I know about __restrict; it's more of a band-aid.)

Re:Why Is C Faster Than Java (2009) by EVa5I7bHFq9mnYK on Monday December 27, 2021 @03:44PM (#29706580)
The version of Fortran I worked with had no stack nor heap. Needless to say, it was very fast.
Re:Why Is C Faster Than Java (2009) by therealcamino on Monday December 27, 2021 @01:10PM (#29704986)
> Aliasing does not exist in Fortran

Ha, I have some disgusting use of COMMON blocks from the 80's to show you. But that doesn't affect the optimizer's ability to assume that there's no aliasing, so you're totally correct about that.

Re:Why Is C Faster Than Java (2009) by umanwizard on Sunday December 26, 2021 @11:34PM (#29699271)
> C allows the developer to play fast and loose with the rules.

So does Rust, in unsafe blocks. Is there anything that can be done in C that can’t be done in unsafe Rust?

Re:Why Is C Faster Than Java (2009) by saghm on Sunday December 26, 2021 @11:46PM (#29699372)
There are some architectures that C supports better than Rust due to having gcc's backends as an option as well as LLVM, but within a given architecture that both support, I don't think there is.
Re:Why Is C Faster Than Java (2009) by vvanders on Sunday December 26, 2021 @11:44PM (#29699352)
Only thing that comes to mind immediately is VLA/alloca but I'd have to check. Most of the gnarly pointer things are fair game and work just as well as their C counterparts.
Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:49AM (#29701050)
VLA are thankfully no longer required for ISO C compliance, who had thought it was a good idea in first place?

Google even sponsored the work to clean the Linux kernel from them.

Re:Why Is C Faster Than Java (2009) by bbatha on Monday December 27, 2021 @02:09AM (#29700219)
Rust supports VLAs, Box<[T]> is pretty common in fact. It just doesn’t have a native way to alloc them on the stack.
Re:Why Is C Faster Than Java (2009) by cesarb on Sunday December 26, 2021 @11:45PM (#29699359)
> Is there anything that can be done in C that can’t be done in unsafe Rust?

Duff's device <https://en.wikipedia.org/wiki/Duff's_device>.

Re:Why Is C Faster Than Java (2009) by josephcsible on Sunday December 26, 2021 @11:55PM (#29699421)
You're mixing up a thing you can do with a way that you can do something. You can write Rust code with the same observable behavior as Duff's device in C.
Re:Why Is C Faster Than Java (2009) by kaba0 on Monday December 27, 2021 @03:23AM (#29700576)
Also, Duff’s device is pretty terrible and a modern compiler will output much better code.
Re:Why Is C Faster Than Java (2009) by LAC-Tech on Monday December 27, 2021 @05:39AM (#29701303)
> You're mixing up a thing you can do with a way that you can do something.

I don't think they are.

> You can write Rust code with the same observable behavior as Duff's device in C.

Yes, turing complete languages can do that.

Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:48AM (#29701041)
A good example of cool tricks to impress fellow hackers that have no place in code bases that should meet certain quality levels of long term maintenance.
Re:Why Is C Faster Than Java (2009) by rthomas6 on Monday December 27, 2021 @12:11AM (#29699502)
Modifying memory on the heap in multiple threads without a mutex, from one pointer.

Why anyone would want that on purpose, I don't know.

Re:Why Is C Faster Than Java (2009) by umanwizard on Monday December 27, 2021 @12:17AM (#29699536)
What stops you from doing this in Rust?
Re:Why Is C Faster Than Java (2009) by the_duke on Monday December 27, 2021 @03:08AM (#29700503)
The language itself, unless you go out of your way to circumvent the protections with `unsafe`.

In this particular case it's the `Sync` trait and the generell rules around sharing data.

Re:Why Is C Faster Than Java (2009) by umanwizard on Monday December 27, 2021 @03:23AM (#29700575)
> unless you go out of your way to circumvent the protections with `unsafe`

I was specifically referring to the things you can do in `unsafe` blocks.

Re:Why Is C Faster Than Java (2009) by rthomas6 on Monday December 27, 2021 @11:47AM (#29704025)
It was my impression that you can't do it even in unsafe because ownership rules still apply. But apparently I was wrong and there is a way to do it?
Re:Why Is C Faster Than Java (2009) by umanwizard on Monday December 27, 2021 @12:34PM (#29704581)
Ownership rules still apply _to references_, but not to raw pointers.

This program prints "12". The only difficulty is that Rust's designers have intentionally made this pattern difficult by marking raw pointers as !Send, but you can get around that by wrapping them in a Send type.

    use std::{time::Duration, ops::{DerefMut, Deref}};
    
    struct SendWrapper<T>(*mut T);
    
    impl<T> Deref for SendWrapper<T> {
        type Target = T;
    
        fn deref(&self) -> &Self::Target {
            unsafe { self.0.as_ref() }.unwrap()
        }
    }
    impl<T> DerefMut for SendWrapper<T> {
        fn deref_mut(&mut self) -> &mut Self::Target {
            unsafe { self.0.as_mut() }.unwrap()
        }
    }
    
    unsafe impl<T> Send for SendWrapper<T> {}
    
    fn main() {
        let mut x = 10;
        let p1: *mut usize = &mut x;
        let p2: *mut usize = &mut x;
        let mut s1 = SendWrapper(p1);
        let mut s2 = SendWrapper(p2);
    
        let j1 = std::thread::spawn(move || {
            *s1.deref_mut() += 1;
        });
        let j2 = std::thread::spawn(move ||  {
            std::thread::sleep(Duration::from_secs(1));
            *s2.deref_mut() += 1;
        });
        j1.join().unwrap();
        j2.join().unwrap();
        println!("x: {}", x);
    }
Re:Why Is C Faster Than Java (2009) by perryizgr8 on Monday December 27, 2021 @12:31AM (#29699645)
There are many benefits. Here is a good video that explains:

https://youtu.be/c1gO9aB9nbs

Re:Why Is C Faster Than Java (2009) by unrealhoang on Monday December 27, 2021 @01:05AM (#29699892)
Rust does have UnsafeCell for gurus/experts to build such complex concurrency building blocks, pack it in a safe API for us plebs to use.
Re:Why Is C Faster Than Java (2009) by ekidd on Monday December 27, 2021 @08:16AM (#29702234)
> On the other hand, you may still get away with writing more performant C code since C allows the developer to play fast and loose with the rules.

I have some thoughts on how Rust performance compares with C!

I spent yesterday optimizing a sort routine in Rust. I need to sort records where the key length is known at compile-time, and the payload length is different every time sort is called. I expect the average sort call to involve at least 250 million rows and over 400 columns. Speed matters.

Here's what I've learned so far, about how Rust versus C question:

- Rust has generics with monomorphization. This makes it easy to compile two different versions of the sort routine to run on different length keys.

- The very fastest version of my recursive base case, a 20-item insertion sort, currently uses unsafe code. This allows me to eliminate a couple of bounds checks that LLVM isn't eliminating automatically. I may figure out how to do this with safe code before shipping. (In 5+ years of production Rust, I've never needed unsafe for performance. This may be the first time!)

- Performance often comes down to cache locality. Both C and Rust give me the fine-grained memory layout control that's essential. Also, this means that "detached key" sorts are almost always a bad choice, even when the alternative is repeatly moving large record payloads around.

- The Rust "criterion" benchmarking library makes it super-easy to run valid performance tests.

- The Rust "proptest" allows me to generate large amounts of test data and verify properties like "reckless_sort always produces output matching std sort."

- "cargo fuzz" will be useful when I try to break the finished code. Seriously, it's a super-nice fuzzer workflow.

- Rust's standard quicksort contains all sorts of funky optimizations: It detects semi-sorted arrays, it breaks patterns, it falls back to merge sort when things go wrong. It even marks "cold" paths for LLVM to improve instruction cache usage.

- If I get my sort working, the "rayon" library will make it utterly painless and safe to split up the recursive calls over multiple CPUs.

Overall, this has been a pretty fun experience. I'm not sure whether the finished product will use unsafe Rust in the inner loop. But the combination of generics, rayon, and tightly integrated test/bench/fuzz tooling has made this a very pleasant experience.

Verdict: I am entirely happy using Rust for hot loops. (Even if my current fastest inner loop does use "unsafe" to bypass a few bounds checks I'm not clever enough to eliminate otherwise.)

Re:Why Is C Faster Than Java (2009) by jmspring on Sunday December 26, 2021 @11:59PM (#29699444)
C can be written in a way to minimize vulnerabilities. You use things like critical thinking and sanitize/validate inputs to functions and before you call libraries.

The log4j vulnerabilities and others like it where input validation specific to what you expect would not happen in properly paranoid systems.

Re:Why Is C Faster Than Java (2009) by adrianN on Monday December 27, 2021 @12:10AM (#29699494)
There are approximately zero codebases of nontrivial size that are written in memory unsafe languages and free of serious security holes that would be impossible in safer languages. That is pretty good evidence that the measures you suggest are not very effective.
Re:Why Is C Faster Than Java (2009) by bear8642 on Monday December 27, 2021 @12:21AM (#29699566)
Still approx. zero, but seL4 kernel's written in C and proven correct preventing many security holes memory control prevents
Re:Why Is C Faster Than Java (2009) by erik_seaberg on Monday December 27, 2021 @12:52AM (#29699795)
Wasn’t that generated from Haskell source rather than handwritten, after years of formal verification of not only the source but also the C code generator and the final machine code?
Re:Why Is C Faster Than Java (2009) by kobebrookskC3 on Monday December 27, 2021 @01:25AM (#29699997)
afaik, the c is handwritten for performance reasons, and is manually proven to be a refinement of the executable haskell model. the haskell is then proven to be a refinement of an abstract, more succinct spec.
Re:Why Is C Faster Than Java (2009) by nine_k on Monday December 27, 2021 @02:32AM (#29700338)
Fine! Now we have a way to write guaranteed safe C code.

It requires quite a lot of qualification from the development team, though.

Re:Why Is C Faster Than Java (2009) by kobebrookskC3 on Monday December 27, 2021 @02:59AM (#29700465)
if ur fine with writing 4 lines of code a day from an expert...i guess
Re:Why Is C Faster Than Java (2009) by nine_k on Monday December 27, 2021 @03:29AM (#29700608)
With endless bug-hunting in hastily-written C code, it could be fewer than 4 lines a day of completed code. (I hope nobody actually does so poorly, except while learning C.)
Re:Why Is C Faster Than Java (2009) by kobebrookskC3 on Monday December 27, 2021 @04:12AM (#29700855)
of course, with unverified code, "completed" means absence of evidence of bugs, not evidence of absence.
Re:Why Is C Faster Than Java (2009) by tsimionescu on Monday December 27, 2021 @02:52AM (#29700439)
seL4 is absolutely tiny, not a good example - it's probably smaller than log4j. Plus, it took years of PhD time to develop.
Re:Why Is C Faster Than Java (2009) by iamtedd on Monday December 27, 2021 @12:50AM (#29699787)
seL4 was released as open-source in 2014. I know of the helicopter drone; have any other projects made use of seL4? If not, why not? My naive guess is that it's too hard to build on the project without introducing vulnerabilities and therefore negating the benefits of the kernel, but I'd love to be shown otherwise.
Re:Why Is C Faster Than Java (2009) by oynqr on Monday December 27, 2021 @02:40AM (#29700373)
Doesn't OpenBSD have a decent track record?
Re:Why Is C Faster Than Java (2009) by a-dub on Sunday December 26, 2021 @11:24PM (#29699184)
it's kinda funny really. what you gain by eliminating memory bugs you lose in boundless complexity sprawl that is basically encouraged by the design of the language and its major libraries.
Re:Why Is C Faster Than Java (2009) by throwaway894345 on Monday December 27, 2021 @01:39AM (#29700067)
The complexity isn’t new, it’s just moved from manually managing dependencies or reinventing wheels into more fruitful, higher level applications. Of course, some of these are frivolous, but probably no worse than spending one’s complexity budget wading through dependency hell.
Re:Why Is C Faster Than Java (2009) by throwawaylinux on Sunday December 26, 2021 @11:16PM (#29699124)
There is a single memory error CVE there, the last one.

Assuming a higher level language would not allow that without introducing any other security problems, and assuming the CVEs roughly represent the secureness of the software, then using a higher level language would be a pretty small marginal improvement in security.