News for hackers. Stuff that matters.
Re:Why Is C Faster Than Java (2009) by jillesvangurp on Monday December 27, 2021 @03:43AM (#29700691)
The counter argument here is something like Lucene, which is written in Java but has been heavily optimized over the years using many of the same kinds of tricks the Git people used to optimize Git. There are frequent attempts to replicate parts of what Lucene does in different languages; usually under the assumption that it will be faster and better because of magical properties people associate with things like native compilation or the supposed manual memory management skills of the programmers doing the work.

Most of these efforts are relatively niche compared to Lucene because they never quite catch up in terms of features, scale, etc. and because the programmers involved are messing around on the fringes of the problem space instead of coming up with algorithmic breakthroughs, which at this point is the quickest way to make things faster. Well, that and cutting some major corners and pretending it's all the same by running some silly benchmark.

The fallacy here is confusing language, idioms, memory models, frameworks, etc. and assuming it's all set in stone. It isn't. Just because you are using Java does not mean everything has to be garbage collected, for example. Lucene actually uses memory mapped files, byte buffers, etc. for a lot of things. So, it does not actually need to do a lot of garbage collecting. It uses the same kinds of solutions you'd pick when using C. And they perform ballpark as you'd expect them too meaning that unless you improve the algorithms, you are not going to be magically a lot faster. The same is true of hotspot, the JVM's runtime compiler, which is written in C and, surprise, uses a lot of the same kind of trickery used by the fine people working on e.g. LLVM. So, a lot of things people assume must surely be slower just aren't and a lot of the insurmountable bottlenecks that people assume must surely be there always actually have well known ways of being worked around.

Of course there is always room for more optimization. Lucene is well over two decades old now and they still regularly come up with major performance improvements. There's nothing magical about how they do that; just a lot of hard work that goes into it that would be somewhat challenging to improve on just by switching language and compilers.

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @04:59AM (#29701108)
That's just because there's no a lucene equivalent C library with the same level of attention?

however, there are increasingly such written in C++ (pisa) and rust (tantivy). They handily beat lucene in benchmark suites [1] - so it seems like lucene does suffer from a java penalty - despite getting even more developer attention than pisa and tantivy I would think.

1: https://tantivy-search.github.io/bench/

Re:Why Is C Faster Than Java (2009) by jillesvangurp on Monday December 27, 2021 @05:35AM (#29701282)
No, because a lot of the work would end up being the same kind of work with no inherent advantages of one over the other. Easy to predict because there used to be a C implementation of Lucene. It couldn't keep up in terms of features or performance so work on that stopped a long time ago.

Most of the libraries you mention don't come close to even implementing a tiny portion of Lucene. Classic case of apples and oranges. Also, good examples of the niche things I was talking about. Benchmarks like you mention are kind of self serving like that. They measure something but not everything and probably very selectively. They are faster at what exactly? Under what circumstances? Why? As soon as you answer those questions, what inherent limitations do the Lucene developers have replicating that?

A lot here boils down to how the underlying search engine implements tokenization, stemming, language analyzers, fuzzy matching, and a few other things that you'd need to build a search engine that doesn't suck. The benchmark conveniently does not specify any of that; presumably because it lacks many of those features or has extremely naive implementations of those things. That would be the kind of corner cutting I was talking about. What kind of relevance ranking is being used here? How good is it? Was that even evaluated or considered? Hint, Lucene gives you many options here. Search quality and performance are the big trade off here. Anything that trades off quality over performance is going to look good until you look at the quality.

Also things like the index size and document volume are not specified. Or the hardware this ran on. Or the JVM configuration, compiler flags, etc. It's like benchmarking a formula 1 car by how quick it is at parallel parking. Yes, a T-Ford is going to be faster at that maybe. But is that even meaningful to look at?

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @05:52AM (#29701353)
Sure - there's no doubt that lucene is a much bigger and more "enterprise" solution that is used professionally today by many big companies from Elasticsearch to Mongo to Solr.

But for core text search and indexing - tantivy does indeed support tokenization, stemming, fuzzy matching, and so on. For the core use case of performing a text search on a large corpus of text a lot of the functionality one would need is there - as you can see if you look at the list of queries in the benchmark. From the list of queries there's little I would miss from our "enterprise" use of lucene today - speaking for myself.

One might argue "oh but if you add some obscure features that 5% of people use like a real enterprise solution then it will slow down by 2x" - but I doubt it.

To me, tantivy and the likes are a technology demonstrator - they show that lucene could be significantly faster if it wasn't in java.

Re:Why Is C Faster Than Java (2009) by jillesvangurp on Monday December 27, 2021 @06:17AM (#29701496)
The whole point of a search engine is getting you good results. Not alright result, or some random results in the wrong order, but the best possible results. Bench marking relevance is much more important than bench marking performance for search.

Lucene is a Swiss army knife for building you something to get you the best results possible. Those aren't enterprise features if you actually care about what your users do with search results. For example because your sales are directly correlated to that. The trick with Lucene is to do as much as you can possibly get away with as opposed to doing as little as you can do to call it a day that seems to be the strategy here for proving X is faster than Y with this particular benchmark. It's faster because it does less useful work.

The proof of concept here would be building something that is as good and as fast. By not even trying to benchmark how good things are, you kind of make the point that you either don't know or don't care enough to even bother to measure it.

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @06:24AM (#29701538)
Why did you offer lucene as a counterpoint, if you deny that there’s any equivalent implementation elsewhere that isn’t lucene? That just means lucene cannot be a counterpoint if it just so happens that the only implementation is in java.
Re:Why Is C Faster Than Java (2009) by jillesvangurp on Monday December 27, 2021 @06:43AM (#29701666)
Because people have been trying pretty hard to replicate what Lucene does in other languages specifically because they thought they could do a better job. The reason the Java implementation continues to dominate is that it being implemented in Java has repeatedly proven to be not as much of an issue as people assume it to be. At least not enough to matter.

And since the article is called "why C is faster than Java" and the main argument is literally "here's this thing implemented in C that is fast", it's an excellent counterpoint to go "here's this other thing in Java that is fast".

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @07:00AM (#29701761)
Sure, I would agree that lucene is “fast enough”. But that doesn’t mean it couldn’t be faster if implemented in another language, and I believe that tantivy demonstrates that it could be.

A historical accident isn’t a counterpoint.

Re:Why Is C Faster Than Java (2009) by zelphirkalt on Monday December 27, 2021 @06:23AM (#29701533)
This however, throws out all productivity, that a language like Java gives developers in contrast to C, plus memory management. The C equivalent most likely would be a buggy version with the typical memory management issues, or consume much more time to create.

I am not a big Java fan. We are not talking a Haskell or Rust or similarly safe language here. However, Java is still far ahead of C in that regard.

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @06:36AM (#29701621)
From what I’ve seen working in the financial industry, hardly anybody uses lucene directly. It is typically behind elasticsearch or solr or whatever - which then takes care of load balancing and replication and all that.

For these use cases, you could swap out lucene for something faster or more “low level” in implementation without affecting the typical application using it much, as all interaction happens across some rest API.

Re:Why Is C Faster Than Java (2009) by zelphirkalt on Monday December 27, 2021 @07:18AM (#29701877)
The point I am making is, that you would have to write that lower level or somehow different thing first. If you do it in C, then how do you make it as safe, while still having the same functionality? In huge projects this has mostly not been achieved. Large C projects (and C++ project) suffer from memory safety issues. This seems to be the general rule. Even the best make mistakes, when working with big C code bases, or there are so few of those "best", that they cannot possibly create a huge project all by themselves.

If you replace a more or less safe implementation, with a buggy, memory unsafe C version of it, you are not going to make anyone happy about the performance improvement. Always assuming, that you put in "only" the same amount of time and number of people, not more, than the original implementation.

The amount of things you need to keep in mind with all the memory management and other details when writing C code can also make you blind to other issues like proper input validation and implementation of invariants of application settings. You have to put a lot of energy into making sure you are not leaking memory and avoiding memory safety issues, so you lack that energy when it comes to higher level issues. This is directly affecting developer productivity.

Re:Why Is C Faster Than Java (2009) by JetSetWilly on Monday December 27, 2021 @07:39AM (#29702011)
I mean, obviously using different languages has different trade offs in many dimensions. If speed were all that mattered then everything would be written in assembler.

But the point at issue is that by using java you are trading off speed (potentially for other benefits).

By the way - tantivy at least is implemented in rust so it shouldn’t suffer from memory safety issues just as lucene shouldn’t. And the rust language will provide some guarantees that java doesn’t - like freedom from data races.

Re:Why Is C Faster Than Java (2009) by Too on Monday December 27, 2021 @10:41AM (#29703349)
The premise here was writing the lower piece something. Not using it.
Re:Why Is C Faster Than Java (2009) by dig1 on Monday December 27, 2021 @05:57AM (#29701376)
> there's no a lucene equivalent C library with the same level of attention?

Actually, there is [1]. Clucene has been around for years, it is a re-implementation of Lucene in C++, and yet the only serious usage I've seen was it as a file indexing engine in KDE many years ago (although I might miss other projects). Later it was dropped from KDE.

I've had (indirect) relations with search/indexing stuff for years, and no one ever complained about Lucene's speed or had any problems with it.

[1] http://clucene.sourceforge.net/

Re:Why Is C Faster Than Java (2009) by spaetzleesser on Monday December 27, 2021 @12:00PM (#29704190)
There used to be a C Lucene version. I used it for a desktop software. It was way faster than the Java version but wasn't maintained well.
Re:Why Is C Faster Than Java (2009) by chx on Monday December 27, 2021 @09:16AM (#29702654)
For C++ wouldn't Manticore, the Sphinx Search fork be a better example?
Re:Why Is C Faster Than Java (2009) by blub on Monday December 27, 2021 @04:10AM (#29700842)
That’s not a counter-argument, because most Java developers can’t write such C-like code and more importantly they don’t want to.

The debate was always about idiomatic Java vs. idiomatic C.

What’s amazing to me is that I’ve had such discussions 15 years ago, 10 years ago, etc. I would have hoped that the Java community would have accepted the reality by now :-)

Still, there’s also an interesting new development happening in this area: the members of the Rust community have picked up the mantle of native vs. managed performance. By now they’ve spent so much social capital on “rewrite it in Rust”, that it’s not clear if this will be a positive for native dev advocacy, but the show will definitely be fun to watch.

Re:Why Is C Faster Than Java (2009) by josephg on Monday December 27, 2021 @05:50AM (#29701343)
> it’s not clear if this will be a positive for native dev advocacy

I've rewritten a few things in rust. Seems pretty positive to me, because you can mix some of the best optimizations and data structures you'd write in C, with much better developer ergonomics.

A few years ago I wrote a rope library in C. This is a library for making very fast, arbitrary insert & delete operations in a large string. My C code was about as fast as I could make it at the time. But recently, I took a stab at porting it to Rust to see if I could improve things. Long story short, the rust version is another ~3x faster than the C version.

https://crates.io/crates/jumprope

(Vs C equivalent: https://github.com/josephg/librope )

The competition absolutely isn't fair. In rust, I managed to add another optimization that doesn't exist in the C code. I could add it in C, but it would have been really awkward to weave in. Possible, but awkward in an already very complex bit of C. In rust it was much easier because of the language's ergonomics. In C I'm using lots of complex memory management and I don't want to add complexity in case I add memory corruption bugs. In rust, well, the optimization was entirely safe code.

And as for other languages - I challenge anyone to even approach this level of performance in a non-native language. I'm processing ~30M text edit operations per second. A few years ago I tried something similar in JS and got about 100k edits per second. (300x slower)

But these sort of performance results probably won't scale for a broader group of programmers. I've seen rust code run slower than equivalent javascript code because the programmers, used to having a GC, just Box<>'ed everything. And all the heap allocations killed performance. If you naively port python line-by-line to rust, you can't expect to magically get 100x the performance.

Its like, if you give a top of the line Porsche to an expert driver, they can absolutely drive faster. But I'm not an expert driver, so I'll probably crash the darn thing. I'd take a simple toyota or something any day. I feel like rust is the porsche, and python is the toyota.

Re:Why Is C Faster Than Java (2009) by codr7 on Monday December 27, 2021 @09:15AM (#29702645)
Re-implementations often turn out faster simply because you understand the problem better, which seems to be the case here.

The same algorithm not going to run faster in Rust than tuned C, period. Just like C is never going to run faster than tuned Assembly. I wish we could move on to discussing something that matters.

Re:Why Is C Faster Than Java (2009) by specialist on Monday December 27, 2021 @11:00AM (#29703544)
Implementation vs performance. And hand-coded assembly would likely be faster still. So what?

u/josephg's point is the that implementing equally performant code in 'C' was cost prohibitive for them.

This is applying the principle behind "economics of compiler optimizations" writ large. Not a religious battle.

Re:Why Is C Faster Than Java (2009) by josephg on Monday December 27, 2021 @04:18PM (#29706879)
> The same algorithm not going to run faster in Rust than tuned C, period.

Weirdly enough, before I added that optimization my rust code was ~20% faster than the C code anyway. And I have no idea why - the programs were (as far as I could tell) identical. I was using the same compiler backend (llvm) and before alias analysis was turned on for rust. And -march=native in both cases.

Could just be a weird coincidence of the compiler’s inlining decisions - though I suspect not. I tried investigating it but I don’t understand x86_64 assembly enough to understand how the binaries differ.

Re:Why Is C Faster Than Java (2009) by unwind on Monday December 27, 2021 @07:16AM (#29701867)
Cool!

From a very quick look, the sample code prints 'r' instead of 'str' which was confusing. Also, is 'str' leaked or freed with 'r'?

Re:Why Is C Faster Than Java (2009) by josephg on Monday December 27, 2021 @04:08PM (#29706799)
In the C example str is heap allocated, so it’s not associated with r and leaked in that example. I should free it to make that clear!

After writing a lot of rust code this year it’s sort of weird reading that, and seeing that aspect of the code not be explicit or obvious.

Re:Why Is C Faster Than Java (2009) by kaba0 on Monday December 27, 2021 @04:41AM (#29701004)
But nor does most code require that sort of control over memory.

Eg. would a program with very diverse code paths improve that much from fitting data into L1 cache better? I am genuinely asking it. Because of course for things like pipewire handling audio streams it is very important and Java would not be a good fit. But for a web application I really don’t see C beating Java by much if by any (and the techempower benchmark shows native languages at top only when those are heavily optimized very specifically eg. depending on the exact format of HTTP requests)

Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:37AM (#29700988)
Applying the same argument to C, C isn't a system programming language, because the use cases where it is used as such rely on compiler extensions not required for ISO C compliance.
Re:Why Is C Faster Than Java (2009) by bob1029 on Monday December 27, 2021 @12:00PM (#29704196)
You also see the same thing with fintech areas like ringbuffer-based order matching engines. Sure, with an infinite amount of time you could make a C implementation run a little bit faster, but practically speaking you would have a hard time building something that gives the business the same level of confidence as a Java/C# solution. Especially, if there's piles of manual memory management and ASM hacks going on.

In my experience, C#/.NET is in a really interesting position when it comes to these questions. It does support value types out of the box which can add another order of magnitude over Java implementations of the same code. One example of this is the LMAX Disruptor. This was originally written for Java and then ported to C#. Because C# supports value types, a special ValueDisruptor variant was developed specific to its port, which enables performance that would otherwise be impossible in the Java solution (which is constrained to using reference types in the buffer).

https://medium.com/@ocoanet/improving-net-disruptor-performa...

Re:Why Is C Faster Than Java (2009) by jcranberry on Tuesday December 28, 2021 @02:43PM (#29717155)
You definitely don't need an infinite amount of time to make C or C++ implementation of a matching engine run faster than a Java based one.

I've worked at two fintech companies, a prop shop and an investment bank and seen some APIs for other companies' matching engines and they've pretty much all been C/C++.

Re:Why Is C Faster Than Java (2009) by jcranberry on Monday December 27, 2021 @11:01AM (#29703556)
I don't see how this is a counterargument, or highlighting any fallacy.

The article talks about hacks used to get around garbage collection in Java and increase performance, including memory mapped files and byte buffers. It also talks about the pain points of using these hacks and the costs they have to pay despite these hacks.

It also concludes that it is practical to build Git in a higher level language and JGit performs reasonably well, despite not getting quite C level performance or its memory utilization.

Re:Why Is C Faster Than Java (2009) by zerocount on Monday December 27, 2021 @04:17AM (#29700879)
There's no counter argument to an article that specifically breaks down why C git is faster that JGit back in 2009. Lucene isn't relevant.
Re:Why Is C Faster Than Java (2009) by omginternets on Monday December 27, 2021 @10:25AM (#29703210)
Where does one go to learn all these optimization strategies and tactics? I really want to take a more deliberate approach to optimizing my (Go) code, but I’m currently stuck banging on things with a wrench between benchmarks.

Frustratingly, most advice I get is non-actionable, and amounts to restating general principles like “be aware of cache”. Are there any books or online resources that can help me build a more principled approach to runtime performance?

Re:Why Is C Faster Than Java (2009) by halpert on Monday December 27, 2021 @11:03AM (#29703567)
The general process I’ve seen is to profile the application and look at what’s taking the most time. Then you sit down and think about how you could make the slowest parts faster. There isn’t a one-size-fits-all answer to your question. Benchmarks are good for validating your improvements, but not for finding what to improve. That’s what profiling is for.
Re:Why Is C Faster Than Java (2009) by omginternets on Monday December 27, 2021 @01:08PM (#29704964)
Yeah; this is what I meant by “banging on things with a wrench between benchmarks”. I can’t fight the feeling that there is some background knowledge that makes the search more directed.
Re:Why Is C Faster Than Java (2009) by lossolo on Monday December 27, 2021 @06:16PM (#29708140)
There is some background knowledge, like knowing time and space complexity of algorithms and datastructures that you use and how this abstractions interact with underlying hardware on which you are running your application. Knowing how CPU, CPU caches, OS memory management, as you mentioned that you work with Go so Go internals (like goroutines scheduling, how memory is allocated by Go runtime, how GC works etc), kernel task scheduling (context switching etc) work and how they impact your workload. A lot of it is intuition that you get with working with code that needs optimising throughout the years and a LOT of reading but sometimes you can flush that intuition into the toilet because you often find bottlenecks in places you wouldn't think about. So it really depends on the nature of your application (is it a network application or HPC, multi threaded? single threaded? distributed? you are optimising for high throughput or low latency, do you control hardware, network etc)
Re:Why Is C Faster Than Java (2009) by alaties on Monday December 27, 2021 @12:51PM (#29704769)
I've been similarly frustrated in the past.

Interestingly, I've found that studying other engineering disciplines outside of software engineering to be best. The best explanations and modeling frameworks for concurrency I learned were from network engineering books and a couple hardware design classes I took.

Re: principles around performance analysis and diagnostics, Brendan Gregg is a name you should look up. His book on systems performance is a tremendous resource in both principles and methodology. Even though it's not focused on coding specifically, the same principles apply.

Re:Why Is C Faster Than Java (2009) by omginternets on Monday December 27, 2021 @01:07PM (#29704948)
Now that is a recommendation that I was not expecting, and which makes perfect sense in hindsight! Cheers! I shall order his book immediately.
Re:Why Is C Faster Than Java (2009) by yencabulator on Monday December 27, 2021 @11:19PM (#29710334)
> restating general principles like “be aware of cache”

It's a very powerful concept, and there really isn't all that much beyond it. That's the problem with pretty much all of the deep truths about computing, they're frankly pretty simple once you grok them.

But let's at least restate that well: https://www.youtube.com/watch?v=CSqbjfCCLrU https://www.youtube.com/watch?v=C6EWVBNCxsc

Re:Why Is C Faster Than Java (2009) by omginternets on Wednesday December 29, 2021 @01:51AM (#29722574)
I’ll take it! Thanks for the links.
Re:Why Is C Faster Than Java (2009) by mohanmca on Monday December 27, 2021 @05:21AM (#29701213)
Well, It is not Lucene alone, there are many tools in java written around Lucene such as ElasticSearch/Solr/Elassandra and so on, so no language or runtime can replace everything. Eventually something might catch-up. That is why it is not language alone! It is developer, runtime, ecosystem and problem domain, brainshare.
Re:Why Is C Faster Than Java (2009) by coliveira on Monday December 27, 2021 @01:59PM (#29705570)
Lucene is the typical example of software that works well despite not being as fast as possible, because its features are more important than raw speed that C could provide. Nothing wrong about that. After all, that's why we buy more hardware. However, it doesn't disprove the point of the article. It could be possible to build something at much better speed than Lucene if there was the interest and man power to do that in C.
Re:Why Is C Faster Than Java (2009) by geodel on Monday December 27, 2021 @10:38AM (#29703316)
One need to see the amount of resources to run Lucene or its enterprise solution like Elasticsearch. So Java based solution work because enterprise can through lot of resources at it. But it does not mean they are efficient or faster. Else Java folks at Oracle wouldn't be spending decade long effort on flatter memory layout of Java objects.
Re:Why Is C Faster Than Java (2009) by burntoutfire on Monday December 27, 2021 @10:37AM (#29703313)
> Lucene actually uses memory mapped files, byte buffers

How do they cast raw memory pointers into Java types? In C, you just cast a void pointer to a pointer to intended type, and voila - that piece of memory is interpreted as if an object of that particular type resided there. Is anything of this sort possible in Java?

Re:Why Is C Faster Than Java (2009) by kaba0 on Monday December 27, 2021 @04:09AM (#29700839)
Just a nitpick, but Hotspot is written in C++ (and asm).
Re:Why Is C Faster Than Java (2009) by pjmlp on Monday December 27, 2021 @04:34AM (#29700971)
GraalVM and JikesRVM aren't though.

As for Hotspot, some C++ subsystems are now in Java.