News for hackers. Stuff that matters.
Re:Garnet – A new remote cache-store from Microsoft Research by whimsicalism on Tuesday March 19, 2024 @12:02AM (#39752834)
surprised to see a garbage collected language project (C# for Garnet) beat redis/dragonfly
Re:Garnet – A new remote cache-store from Microsoft Research by latchkey on Tuesday March 19, 2024 @12:48AM (#39753036)
I think it is rare, but isn't impossible. I remember in the 2000's watching someone do a demo of Java/JVM beating the pants off a C++ application. If I remember correctly, it was something about how the JIT was doing an optimization that you would have had to write assembly in order to optimize it to the same level.
Re:Garnet – A new remote cache-store from Microsoft Research by thayne on Tuesday March 19, 2024 @01:56AM (#39753301)
There are at least two reasons I can think of why a JIT language with GC can outperform C/c++:

1. Memory management with a GC often has higher throughput. With the downside that you can have high latency when a garbage collection occurs.

2. The JIT compilation can potentially do a better job of optimizing, because it has information about how the code has been run so far.

It is possible for c or c++ (or rust) to get get the first using alternative memory management strategies, and the second by hand optimizing, and/or using profile guided optimization.

Re:Garnet – A new remote cache-store from Microsoft Research by pjmlp on Tuesday March 19, 2024 @05:12AM (#39754012)
3. The runtime also supports C++, and one emits the same MSIL code sequence that the C++ compiler would generate.

Which is why WASM isn't that great novelty for grey beards.

Re:Garnet – A new remote cache-store from Microsoft Research by monocasa on Tuesday March 19, 2024 @01:39PM (#39758304)
I've also seen GC reliant concurrent algorithms that would require a lot more synchronization to keep track of when to actually free an object. GCed languages can just forget about the object and rely on the GC later to figure out what threads have access to valid objects. C and C++ need to figure out if it's safe to free.
Re:Garnet – A new remote cache-store from Microsoft Research by nurettin on Tuesday March 19, 2024 @01:20AM (#39753172)
There were coding competition benchmarks at the time putting javac against the underdeveloped gcc 2.95 of the time. The trick they did with java programs was to allocate a region of memory only once and reuse that whenever they needed more, simulating a stack. Then the programs were benchmarked as hot start and cold start. Hot start timings were used to bench against C++ programs. If the algorithm they used was better, this sometimes resulted in head to head or better performance.
Re:Garnet – A new remote cache-store from Microsoft Research by latchkey on Tuesday March 19, 2024 @01:24AM (#39753192)
I remember that one too, but this one was not just the hot memory trick, which seems relatively easy. It was entirely JIT based, where the JIT happened to pick a better path than gcc.
Re:Garnet – A new remote cache-store from Microsoft Research by blackoil on Tuesday March 19, 2024 @01:11AM (#39753132)
The investment in optimization in CLR or JVM can be huge as they impact millions of applications. While each C / C++ code will have to be hand optimized.

Also limits on number of people in given time who can write optimal C code vs C# will also make managed code better.

Re:Garnet – A new remote cache-store from Microsoft Research by theLiminator on Tuesday March 19, 2024 @03:23AM (#39753614)
Couldn't you just replace the CLR and JVM with llvm and gcc and then replace C with assembly and arrive at the same conclusion.
Re:Garnet – A new remote cache-store from Microsoft Research by blackoil on Tuesday March 19, 2024 @04:05AM (#39753782)
Now commenting beyond my expertise, I understand both ecosystems have developed orthogonally, C#/Java assumes code to be higher level and "dumb" and optimization are in library/CLR/JVM. While C/C++ are developed with code at lower level and developer making sure code is optimized.

Now, I could be talking out of ass, haven't done enough C/C++ coding in over a decade.

Re:Garnet – A new remote cache-store from Microsoft Research by balls187 on Tuesday March 19, 2024 @09:50AM (#39755721)
You can write poorly optimized C#.

And the intel C++ compiler has provided significant performance for decades via optimization without needing to optimize by hand.

Re:Garnet – A new remote cache-store from Microsoft Research by Dumble on Tuesday March 19, 2024 @03:42AM (#39753703)
I'm not the person you are replying too, but I believe that of course, the pattern holds if you keep shifting it down. I.E. using a faster CPU will speed up all programs running on it, each (already optimized) ASIC has to be optimized further individually.
Re:Garnet – A new remote cache-store from Microsoft Research by astral303 on Tuesday March 19, 2024 @10:40PM (#39762421)
I don’t think so, because it is the runtime JIT (Just-In-Time) optimizer that is the critical speed advantage that allows the CLR and JVM to beat C and C++.

The inlining of virtual calls is the critical optimization that enables this. Because C/C++ is optimized statically and never at runtime, it is unable to optimize results of function pointer lookups (in C, and thus also virtual calls in C++). However, the JITs can inline through function pointer lookups.

In sufficiently complex programs, where polymorphism is used (i.e. old code that calls new code without knowing about it), this yields an unsurpassed speed advantage. Polymorphism is critical to managing complexity as an application evolves (even the Linux kernel, written in C, uses polymorphism, e.g. see struct file_operations).

Re:Garnet – A new remote cache-store from Microsoft Research by rfoo on Tuesday March 19, 2024 @01:59AM (#39753317)
Plus that it's easier to have unmanaged code in C# than in Java.
Re:Garnet – A new remote cache-store from Microsoft Research by larodi on Tuesday March 19, 2024 @02:49AM (#39753477)
Is it possible to have unmanaged code in Java at all… or you mean linking libs and exposing them through Java API in the lang?
Re:Garnet – A new remote cache-store from Microsoft Research by rfoo on Tuesday March 19, 2024 @03:29AM (#39753637)
Ah, I meant Java code manipulating off-heap memory. For Java I don't think there's a way to mix managed and unmanaged code like you do in C#/C++ CLR.
Re:Garnet – A new remote cache-store from Microsoft Research by neonsunset on Tuesday March 19, 2024 @03:36AM (#39753676)
In C# you usually don't want to mix in C++. Manipulating off-heap memory is indeed easy - nowadays pointers to both object interiors, stack and unmanaged memory can be represented as `ref T` where T is byte, int, etc.

These are then subsequently wrapped by `Span<T>` and `ReadOnlySpan<T>` respectively. This way a span can be a slice of memory that can have any origin:

    var fromStack1 = (stackalloc byte[32]);
    var fromStack2 = (Span<byte>)[0x20, 0x20, 0x20, 0x20];
    var fromHeap = new byte[32].AsSpan();
    unsafe
    {
        var ptr = NativeMemory.Alloc(32);
        var fromMalloc = new Span<byte>(ptr, 32);
        // Don't forget to free :)
    }
Re:Garnet – A new remote cache-store from Microsoft Research by pjmlp on Tuesday March 19, 2024 @05:20AM (#39754051)
Panama has helped into that direction, it isn't like C# though, for feature parity we will eventually need Valhalla, which since they want to keep existing JARs working, it is taking its time.

Ignoring the type systems from Eiffel, Objective-C, Oberon and Modula-3 linage, even though they were inspirations for Java, has been shown to have been a bad decision in hindsight.

Re:Garnet – A new remote cache-store from Microsoft Research by pjmlp on Tuesday March 19, 2024 @05:11AM (#39754009)
True, I still look forward to have Valhala, eventually.
Re:Garnet – A new remote cache-store from Microsoft Research by fulafel on Tuesday March 19, 2024 @02:05AM (#39753340)
Seems it wins by using more sophisticated data structures & algorithms, instead of "tight code".
Re:Garnet – A new remote cache-store from Microsoft Research by pjmlp on Tuesday March 19, 2024 @05:10AM (#39754004)
Not all garbage collected languages are made equal, some of them, like C# and .NET, do provide all the performace knobs that are needed for C++ like coding.

People only have to learn how to use them, instead of placing all garbage collected languages into the same basket.

In this specific case, MSIL and .NET were designed to support C++ as well, and languages like C# and F# do have ways to access those features, and even if some feature isn't exposed at the language grammar level, you can emit the same MSIL that C++/CLI would generate.

Re:Garnet – A new remote cache-store from Microsoft Research by orthoxerox on Tuesday March 19, 2024 @05:17AM (#39754031)
There's a series of blog posts by Oren Eini that shows that C# can be sufficiently fast even when you don't really optimize anything. Beating Redis in every benchmark is a whole another level, of course.

https://ayende.com/blog/197412-B/high-performance-net-buildi...

Re:Garnet – A new remote cache-store from Microsoft Research by Thaxll on Tuesday March 19, 2024 @09:52AM (#39755744)
Because it's not the language that matters here, the performance comes from the architecture of the storage among other things.

I've seen Go code best Rust one, really when comparing languages you should look at the same implementation, if the design is completely different its not really comparable.

Re:Garnet – A new remote cache-store from Microsoft Research by surajrmal on Tuesday March 19, 2024 @10:35AM (#39756136)
While true, gc tends to affect tail latencies as it can increase variance. Obviously not always the case, but without sufficient planning it's likely. Languages with less stop the world problems tend to trade it for throughput, however if the application is not CPU bound it doesn't matter. The same can happen in a non gc language where a similar lack of allocation planning is done, but it tends to be considered more carefully because those languages really put allocations in your face.
Re:Garnet – A new remote cache-store from Microsoft Research by astral303 on Tuesday March 19, 2024 @10:50PM (#39762464)
Manual memory management vs garbage collection trades speed of allocation for speed of freeing memory. Freeing memory is expensive in a garbage collected language, but allocation is basically free.

Modern garbage collectors are very good and handle many common use cases rather efficiently, with minimal or zero pauses, such as freeing short-lived objects. Many GC operations are done in parallel threads without stopping the application (you just pay the CPU overhead cost).

Also JITs in both the CLR and the JVM perform optimizations such as escape analysis, which stack-allocate objects that never escape a function’s scope. These objects thus do not have to be GC’d.

So really with a GC’d language, you mostly have to worry about pauses and GC CPU overhead. Most GCs can be tuned for a predictable workload. (A bigger challenge for a GC is a variable workload.)

Re:Garnet – A new remote cache-store from Microsoft Research by neonsunset on Wednesday March 20, 2024 @06:11AM (#39764594)
Correction: JVM implementations perform escape analysis, in particular, because Java does not have structs. .NET does not perform escape analysis for objects and all attempts to experiment with it so far has shown greater impact on compilation speed with little to no profit in allocation rate. However, you will find average allocation traffic much lower in .NET because C# already puts way more data on stack through structs, non-boxing operations (where Java boxes), stack allocated buffers, non-copying slicing with spans and object pooling (which is common in Java too).
Re:Garnet – A new remote cache-store from Microsoft Research by astral303 on Wednesday March 20, 2024 @06:36AM (#39764745)
Thank you, I missed the stack allocation design doc stating it’s on the roadmap. (https://github.com/dotnet/runtime/blob/main/docs/design/core...)

Appreciate the detail about the stack allocated bits in .NET.

Re:Garnet – A new remote cache-store from Microsoft Research by neonsunset on Wednesday March 20, 2024 @06:48AM (#39764810)
Yeah, it kind of is. There are quite a few of experiments that are conducted to see if they show promise in the prototype form and then are taken further for proper integration if they do.

Unfortunately, object stack allocation was not one of them even though DOTNET_JitObjectStackAllocation configuration knob exists today, enabling it makes zero impact as it almost never kicks in. By the end of the experiment[0], it was concluded that before investing effort in this kind of feature becomes profitable given how a lot of C# code is written, there are many other lower hanging fruits.

To contrast this, in continuation to green threads experiment, a runtime handled tasks experiment[1] which moves async state machine handling from IL emitted by Roslyn to special-cased methods and then handling purely in runtime code has been a massive success and is now being worked on to be integrated in one of the future version of .NET (hopefully 10?)

[0] https://github.com/dotnet/runtime/issues/11192

[1] https://github.com/dotnet/runtimelab/blob/feature/async2-exp...