r/ProgrammerHumor • • Oct 07 '26

Meme minecraftException

Post image
9.3k Upvotes

830 comments sorted by

View all comments

Show parent comment

2

u/Ma4r Oct 08 '26

???? Where the fuck do you get these numbers? Java literally boxes all primitives generics, read: that int you instantiated in a generic function is now a heap item that needs to be garbage collected. And java is notoriously bad for cache locality that a ring buffer is actually a terrible data structure in java, you need shenanigans like the LMAX disruptor to deal with it. Not only that you NEED to be tuning your GC for every different game you develop on it because you don't want 20 ms gc pauses when your game is based on delta time or even just to avoid completely intermittent lag spikes in general

1

u/Ok-Scheme-913 Oct 09 '26

Oh my god, is everyone a moron here who just reads some big words and that's it?

Also, ints and longs are actually cached in the JVM so they don't have to get GCd.

And you almost never have to tune the GC, in the last decade or so. Java's GCs are the state of the art. Sure you can do dumb shit that will run slow, but that's also true of c++ (even more so)

1

u/Ma4r Oct 09 '26 edited Oct 09 '26

Try using generics and check the generated bytecode, even a simple for loop can cause the compiler to fail at unboxing.

And the point is not that it's a very good gc or what not, even ZGC have a fat tail for long STW pauses when using common game engine patterns, unless you know the ins and outs of Java , only then you can avoid the common traps and mistakes (check out how many ways you can fuck up with Java arena allocators), meanwhile compared to C++ its way more straightforward and you need to explicitly fuck up

0

u/Ok-Scheme-913 Oct 09 '26

Unboxing doesn't happen at the byte code level..

Do you mean generated machine code?

And sure it can fail, but even then the first 256 ints are a range of pointers only. You don't have to do some complex pointer chasing to have the actual number

1

u/Ma4r Oct 10 '26 edited Oct 10 '26

A heap lookup is still a terrible cost when you're rapidly switching between threads or handling a bunch of numbers at once: Consider a regular Struct of Array pattern in a game engine i.e particle similation, you got an array of particle velocities and you can calculate the average, what unboxed ints would do :

  1. You fetch first int, CPU loads entire block of i.e first 32 ints into cache in one RAM read call
  2. You process first element, loop proceeds to second element
  3. CPU sees second element in CPU cache, we can immediately proceed

What a boxed int would do:

  1. You fetch first int, CPU loads entire block of i.e first 32 int pointers into cache in one RAM read call
  2. You process first element, oh look, it's a pointers, so i need to dereference first, i load another contiguous block of 32 ints from RAM, but this time, the other 31 are actually random data from the heap . But anyways, the entire thing is put into cache and invalidate the cache from first read
  3. You processs first element, move to 2nd
  4. CPU wants to read the 2nd element, oh look it's not in cache, you make ANOTHER ram call to load the same first 32 bits of pointers into cache and invalidate the cache (we literally just retrieved the same block of data a few cycles ago too lmao)
  5. Rinse and repeat

Sure in practice we would have multiple cache liens at once, but the point is it's extremely inefficient to waste precious CPU cache space this way.

You literally need third party library/utility classes, just to avoid something as simple as this, now imagine more complex data structures with variable access patterns

0

u/Ok-Scheme-913 Oct 10 '26

You can always just use arrays. It's not like people don't make tradeoffs in other languages.

Also , heap is just memory, hot enough heap region is just as fast as stack. There is no fundamental difference between the two.

1

u/Ma4r Oct 10 '26 edited Oct 10 '26

Did you even read my example? It's not about hot or not, it's about contiguous access, and also , this is less about you CAN go around it, but how troublesome it is,this issue alone means that you cannot use generics container (which is easily the most natural way one would have a container) without a big performance impact and now you got dumb code like IntArrayList LongArrayList, etc, and no way someone new to java would be aware of a footgun like this, no one in their right mind would suspect a standard library array list of all data structures would cause cache thrashing.

Yes, there are trade offs, this is not shitting on java because they do have great features, this is about java not being suitable for game engine development. It's clear here who is the moron that only got surface level knowledge of the language they are working in, unlike you i profile these things myself

0

u/TrueExigo Oct 08 '26 edited Oct 09 '26

Java does not generally box primitives in generics: boxing only occurs where a primitive value needs to be converted into a reference type. For example, `int[]` remains a compact array of primitive values. Even when boxing does occur, HotSpot optimisations such as escape analysis and scalar replacement can completely eliminate some object allocations.

Java is notoriously bad for cache locality

is also incorrect. Cache locality depends heavily on the data layout. Object-heavy structures such as `List<Integer>` are unfavourable; primitive arrays, SoA layouts and pre-allocated data structures, on the other hand, are very well suited. This is precisely why primitive arrays or specialised data structures are frequently used for numerically intensive game and engine code.

a ring buffer is actually a terrible data structure

On the contrary: a pre-allocated ring buffer is particularly useful for low-latency code because it avoids allocations in the hot path. The LMAX Disruptor solves a different problem: highly optimised communication between threads, involving sequencing, memory ordering, the avoidance of false sharing, and preallocation. The ring buffer itself is not the problem that is being ‘fixed’ by this.

you need to tune the GC for every game

Modern HotSpot GCs are largely designed to be user-friendly. G1 is the default and attempts to meet pause targets automatically; ZGC is explicitly designed for low latency and, in JDK 25, is intended to pause application execution for no longer than approximately 1 ms during costly GC operations. This is no guarantee of real-time performance, but 20-ms pauses are by no means an unavoidable feature of Java.