Measuring GC latencies in Haskell, OCaml, Racket
prl.ccs.neu.edu
prl.ccs.neu.edu
I understand the fact that not everything in a game belongs to the critical 16ms budget (and know about e.g. Unity, which lets you talk C# to it), but I'm still puzzled; can someone expand on:
- Which are these non critical parts? Rendering / physics / animation are certainly in the critical path. That leaves only simple "client" game logic out of it, right? What else?
- For these non critical parts, what are the typical acceptable time boundaries? For example, the article mentions "maximal latencies of around 22ms" for Racket. How is it acceptable for non-critical-path parts? How much would still be acceptable? 50ms? 100ms? "That depends"?
- And regarding Haskell and OCaml, which I only vaguely know about, I had never asked myself how they handle memory. Which makes me ask: are there non-GCed functional programming languages? Does it make sense? (Especially given the kinds of deep complex data structures produced by the immutability that often comes with these languages.)
I've also been looking into how to add resource logic / linear logic to ghc Haskell / languages with a similar internal rep as ghc core. Still need to write that stuff up mind you.
There's a long history of no gc free functional language research. Mlkit and clean are probably two of the older but accessible implementations in the wild
[0] https://github.com/wolfgangj/bone-lisp
[1] http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.3.40...
ATS: https://bluishcoder.co.nz/2011/02/27/linear-datatypes-in-ats...
Stalin does global static life time analysis and MLKit uses GC only to augment it's region based scheme, but you can turn it off (at the cost of guaranteed space safety).
Of course Rust is a bastard child of ML as well.
I've done game development in C++, C#, and JS, and I had to worry about memory management far more in JS than in C# or C++, simply because even something as innocent as adding two vec3s requires you come up with a space for the result first, if you want to avoid gc hitches.
C#'s struct types help here, but it still has plenty of its features increase GC pressure.
I'm curious how did you expect Racket to work without a GC or any Lisp like language for that matter or did you just assume because of video game usage?
> - And regarding Haskell and OCaml, which I only vaguely know about, I had never asked myself how they handle memory. Which makes me ask: are there non-GCed functional programming languages? Does it make sense? (Especially given the kinds of deep complex data structures produced by immutability.)
Rust and C++ depending on your definition of FP. I believe there are some old ML languages as well (and even some old lisps).
I honestly was curious if the OP had some insight that I did not. I realize re-reading the comment it sounds snide but I didn't want to assume the OP's knowledge.
For all I knew Racket could have some ability to avoid GC.
Just because of video game usage and my perception of Carmack as a performance freak who wouldn't compromise on performance. (Sloppy reasoning, I know! Thus, my questions on where GC makes sense in games)
Yes, C++. For the last 18 years the C++ standard has been busy adding functional features to the language. With mixed success, but still the result is quite impressive.
I thought everything in Haskell was immutable? If so, wouldn't that imply that it's impossible to create cyclic references (i.e. A and B can't both reference each other, because one of them has to be created first, and once created, it's immutable)?
Couldn't you then just use reference counting to decide when to recycle objects instead of having full-blown GC pauses? Or is it too expensive to keep a reference count on all those objects and keep it updated? Whatever cost it has, it seems much more preferable to me compared to 50 ms GC pauses for relatively simple code.
x :: [Int]
x = 0 : y
y :: [Int]
y = 1 : x
And thus we have a circular list with the elements 0 and 1. It's technically an infinite-length list with 0 and 1 repeating forever, but Haskell will represent this in memory as a circular list.
Other languages have this property too - say, C# (not entirely true, but pretty close - at least if you think about top level methods etc.).
Also an example where the order of declarations does not matter at all, because there is just one declaration:
x :: [Integer]
x = 0 : 1 : xBut you can create it. Names are shared:
let x = [1, 2, 3] in [x, x, x]
There are more exotic ways, too.It seems far too often we run into popular languages whose growth has stalled due to their GC, or features that have been held back by GC, or whatever. And then other languages have several different GCs with different characteristics that can be swapped out with a simple CLI flag.
Is it something that is deeply and strongly coupled to the language structure? Is the parameter space for tuning just too large to practically swap one GC for another? Incompatible algorithms? Maybe one of those situations where you get 80% of the way with an ounce of effort, but to get to 81% takes years?
- Azul's GC requires cooperation from the kernel; I believe it
specifically uses the virtual memory mechanism to implement a read
barrier. Basically you need a patched Linux kernel.
- Real-time GC will assuredly lower overall throughput, and increase
complexity. At the very least you need either a read barrier or a
write barrier. I don't think anyone uses read barriers normally;
write barriers are often used for generational GC, but I *think*
the write barriers used for real-time GC involve more work than
those for generational GC.
- Adding a read or write barrier to a language implementation that
doesn't already have them means you need to guard every single
place where someone reads or writes what might be a pointer. This
is easy to screw up.
- Several real-time GC algorithms punt on moving objects, and are
thus vulnerable to memory fragmentation. This obviously reduces
the robustness of the runtime system. (Others use stop-the-world
to do compaction, and thus are not real-time.) type Chan = Map.Map Int ByteString.ByteString
is odd looking. Why the duplication?let's look at the stdlib `replicate` function. `replicate 5 'x'` gets you `['x', 'x', 'x', 'x', 'x']`. But that's a standard haskell linked list, and there's other list-y things, like ByteString. So there's bytestring module has it's own replicate function which returns a ByteString (like an array afaik) instead of a list.
Now, a bare `import bytestring` just dumps all of the module - its datatypes, its functions - into the global namespace, like a Python `from bytestring import *`, so the two replicate functions are gonna clash. There's a solution - we do `import qualified bytestring`. it behaves like a normal Python `import bytestring`, which means that we have to type `bytestring.replicate` to get that function from earlier. all of the stuff inside the bytestring module is hidden away inside this namespace.
Now, the trouble is, EVERYTHING is hidden away. So, say I want to refer to the actual ByteString datatype, say to write a type signature like: ``` length :: Bytestring a -> Int ``` Now, because of the namespace qualification I did, I actually have to write the verbose ``` length :: bytestring.ByteString a -> Int ``` So that's like saying "bytestring-module.ByteString-datatype" every time you want use a ByteString. that's what the author does, hence the weird duplication you noticed comes from.
The usual solution is: ``` import qualified Data.ByteString as B -- the functions import Data.ByteString (ByteString) -- the datatype ```
which roughly translates to Python as: ``` import bytestring as B from bytestring import ByteString # the ByteString class ```
so now, I can use `B.replicate`, and `ByteString`, which is what I want! Great! But the importing is verbose and, as I said, kinda annoying.
import qualified Data.ByteString as ByteString
import qualified Data.Map.Strict as Map
So everything from “Data.ByteString” has to be prefixed with “ByteString.”In real-world code, depending on conventions, you’d probably add unqualified imports for the type names to avoid the needless duplication:
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import qualified Data.ByteString as ByteString
import qualified Data.Map.Strict as Map
type Chan = Map Int ByteString
pushMsg =
…
let inserted = Map.insert … import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByesString.Lazy as BSL
import qualified Data.ByteString.Char8 as BSCThis same can be written as:
type Chan = Map Int ByteString
People do prefer importing Map and ByteString qualified, so their functions will have a prefix of their choice. But very few people import ByteString as "ByteString", most use something shorter.As most things in Haskell, it tends to work greatly. The usual problems go away due to some combination of Haskell's features and the main problems are things you wouldn't think about.
We can conclude that Ocaml outperforms GHC on this edge case for the GC.
There's nothing to conclude here about a general case, and it may not even be possible to make general conclusions about GC.
Exactly my experience of the Java JIT and GC where I worked on uh "optimizations" of related metrics in two or three projects.
Just a casual observation of the commento-sphere after a lay imperative programmer's cursory research of functional programming. This post, sample-of-one hugely notwithstanding, seems in the absence of counterpoints to suggest that, as potentially narrow as the benchmark may be, Ocaml has some very strong arguments against Haskell on the compiler implemenation front.
I can't help but wonder if this GC outperformance is the flipside of the coin of Ocaml's much maligned lack of multi-core capability.
Anyone here who knows what happened to that great site?
http://benchmarksgame.alioth.debian.org/sometimes-people-jus...