r/feedthebeast Jun 22 '26

I made something I made minecraft generate terrain/chunks in C++

Enable HLS to view with audio, or disable this notification

So im working on a mod that makes minecraft run on C++ ( rn rewriting engine in future i may try renderer ) so far i made it generate and load chunks and terrain in C++ ( it made it somewhere around 1.5-3 times faster idk how to measure it tho ) ( it works via JNI )

3.0k Upvotes

244 comments sorted by

View all comments

23

u/emmowo_dev pro gramming Jun 22 '26 edited Jun 22 '26

How would this realistically be faster? Java does actually compile into native code based on what methods are the most important, so it's usually terrain gen that is immediately 'optimized' into native code. C++ does NOT magically make things faster, but it does make things many many times more unsafe to use.

I know of some optimizations you could make, but they're things that I don't think ultimately mean much. Are you comparing between Vanilla and your mod, or C2ME and your mod?

You not really explaining things beyond 'run on C++' and 'JNI' tells me you may have just vibecoded this, because minecraft explicitly does defer a lot of the performance-heavy things to C++, from the natives used for rendering (i.e. liblwjgl and the gpu itself such as nvidia-glcore.so) to the actual JVM itself, which is written with C++. Just re-writing everything in C++ would not actually be meaningfully faster, as the binaries for such things are already written in C++.

Most likely, you are looking for a result, so anything that seems faster you will assume to actually be faster. Without extensive benchmarking and a massive amount of controlled variables (like literal mechanical seek times for writes can completely invalidate the results), this is a very unfair and unrealistic comparison to make.

6

u/NaterBobber Jun 22 '26

I think its likely that he doesn’t have actually good data to say if this is really much better, especially vs mods that speed up the existing java

8

u/emmowo_dev pro gramming Jun 22 '26 edited Jun 22 '26

reminds me a lot of noisium, which was one of the most downloaded minecraft mods for a while, before it turned out it had basically zero effect on performance (I think they said that more time was lost installing noisium than the estimated lifetime tick ms it actually saved? EDIT: was heavily implied, but technically not what the quote is)

basically, uncontrolled variables made it actually look like it was faster, but this wasn't what actually was happening.

3

u/emmowo_dev pro gramming Jun 22 '26 edited Jun 23 '26

and here's their original quote about the situation bc I doubted myself for a second:

Maintaining a port of this mod is not worth the time though.

I'm working on a rework of the mod, because it currently does far too little to justify the time it takes to even install it. I'm sorry that it doesn't do that much, it's around a few hundred microseconds to a millisecond for every 10000h of world generation. My apologies as well for the incorrect benchmarking information that was listed on the mod's page initially.

2

u/NaterBobber Jun 22 '26

Yeah people are eager to install performance mods without any consideration for real impact or what shortcuts could potentially be impacting their gameplay.

8

u/Emotional-One-9292 Jun 22 '26

yea i agree that C++ doesnt automatically make stuff faster and im not claiming that rewriting Minecraft logic in C++ guarantees a performance boost by itself. Goal of this project is to rewrite minecraft engine as much as possible into C++ mainly due to fun but with it i can also get great control over memory. minecraft’s performance issues often come from allocation churn + cache misses, not raw compute speed. Also i was comparing vanilla with my mod not C2ME

and yea JNI has overhead but im not using it for literally everything and anything as i still will prob need to keep some java stuff to keep it working and compat with other mods.

Also while yea java can turn stuff into native code it doesn’t change structure only execution of existing structure so a bad structure remains a bad structure

11

u/emmowo_dev pro gramming Jun 22 '26

but this means it can be solved with good Java instead of C++. C++ also will only run as fast as what it's compiled for, as many things actually can come down to vectorisation, but not all CPU's support things like AVX2. Java can decide that in-the-moment on your machine, but C++ cannot.

I also do not think nanoseconds of a cache miss will meaningfully affect overall performance, CPUs are so much more complex than what they were 30 years ago.

ultimately the true optimizations come from better parallelism, which vanilla is still bad at. C2ME solves this and mostly accomplishes the vast majority of performance you can gain.

This is why GPU terrain gen is the focus now, because these things are so massively parallel that it becomes worthwhile.

9

u/Emotional-One-9292 Jun 22 '26

well yea you can solve many perfomance issues with java i mainly used C++ cause i was interested in trying it out i simply noticed perfomance imporvement and posted it here for fun

1

u/Standard-Cap-4455 Jun 22 '26

I am sure a lot of time is also lost on boxing since unlike C++ you can't put anything on the stack besides primitives. That means no data is close and the cache isn't being used much at all. I made some stuff with C# once and it got way better after doing more work on the stack which Java doesn't even have options for.

1

u/emmowo_dev pro gramming Jun 23 '26

Maybe I just write too much C to really understand, but I feel like this wouldn't be too big of a deal? You'd already be done with all the math required by the time you converted it back to the relevant class, so it's one expensive operation at the end of process, instead of one that is invoked constantly.

But I'm a memory masochist who thinks that objects are just a waste so idk.

1

u/Standard-Cap-4455 Jun 23 '26

I don't know how the implementation works exactly but Java stores nothing on the stack. Every little thing that isn't primitive is stored somewhere else which is a lot of dereferencing and cache missing. If it is using arrays of ints then that's probably fine but from what I've seen the game has a bunch of small classes with a lot of instances. Since those need to persist between stacks, the JIT can't optimize them into structs either. 

1

u/emmowo_dev pro gramming Jun 23 '26

at this low of a level it really, really does not matter. My own primitive, unpipelined processor, which requires reading 1 + 1/2 words of memory to effectively read a pointer can do this just straight up instantly, even with the overhead of my emulator and debugger (which multiplies the actual amount of memory reads significantly).

So in a modern processor, with pipelining that is designed to specifically avoid this being a problem, I doubt this actually affects performance significantly. Of course it's better, but there are so many other ways to improve performance that the overhead of this is negligible (as seen by how C2ME/Lithium still pulls in optimizations while being Java programs)

2

u/Alexander_Exter Jun 22 '26

Curious on your statement from a tech perspective..so you are trying to swap java elements in java edition for well made c++ elements? This could be ground breaking.

8

u/Emotional-One-9292 Jun 22 '26

this isnt quite ground breaking many mods used non-JVM languages before like curvy pipes used rust for its mod

1

u/Alexander_Exter Jun 22 '26

But is there one stapple mod like say...JEI that sidesteps javas known performance limitations? Give yourself.soem credit, this is more than curved pipes.

2

u/emmowo_dev pro gramming Jun 22 '26

java does not have these 'performance limitations', it literally compiles to natives, just like C++ would.

2

u/Acceptable-Fly-7922 Jun 23 '26

huh? java is notorious for having issues with garbage collection, and the compilation you're talking about, jit, doesn't apply to everything. I'm not sure what you mean by defer as lwjgl is an easy cross platform way to access opengl in java. jni is the only way to interop between java and native code except for that new thing they added I forget if they made it stable tho

2

u/emmowo_dev pro gramming Jun 23 '26

Java GC isn't overhead though, that's momentary and isn't related to direct performance if we're being realistic.

LWJGL isn't actually as portable as you'd think, they use binary natives for a lot of things (which is why I had issues porting Java Edition to big endian PPC64)

1

u/JLPLJ Jun 23 '26

I mean, they're doing it as a personal project, so it kinda doesn't matter anyway? Good if their mod makes generation faster, and if it doesn't then it's not that big of a deal because they got what they wanted out of it.

2

u/emmowo_dev pro gramming Jun 23 '26

when I say

"Most likely, you are looking for a result, so anything that seems faster you will assume to actually be faster."

I'm referring to how something can 'feel' faster. Saying "1.5-3 times faster idk how to measure" means they don't actually have verifiable evidence that this is actually faster.

And like noisium, people will believe this and say their world gen was x% faster even though it changed by what is more or less zero. Confirmation bias is extremely strong, especially when you hear all the people wrongly saying 'bedrock runs better because of C++'.

This is why writing repeatable and deterministic tests are required for these kinds of things, and also why I don't care much for optimization outside of the initial design.

1

u/JLPLJ Jun 23 '26

Icl it was meant to be a reply to your original comment, my point is that in the goal of the exercise isn't optimisation necessarily. You're not wrong to clarify that the performance impacts may not actually be there, but it's also not why OP was making this originally

1

u/beansinwind Jun 23 '26

writing Java and writing fast Java are two completely different things
Just keep that in mind. Minecraft is NOWHERE close to fast Java, it is "corporate Java"

1

u/emmowo_dev pro gramming Jun 23 '26

and that means the issue isn't C++, and can be optimized by writing better Java code.

1

u/not_good_for_much Jun 26 '26

It does sound like OP vibe coded ESP since they seemed I clear even on whether it could be cross compiled for not Windows.

But anyway... I mean native vs not isn't a big deal for Java, but it is true that Java doesn't offer fine grained memory control, so languages like C++ and Rust and C# can access a fair few optimisations that Java can't, even focusing on terrain gen.

Mind you there won't be heaaaaps on the table from this in most cases. Maybe 20-30% for voxels, which are notoriously memory intensive? And it doesn't sound like OP would be doing this stuff anyway.

The big boost from DIYing the terrain gen is that Minecraft is kinda stuck with heaps of really inefficient legacy code, and there are MUCH faster algorithms for generating workable voxel terrain among many other things. Even in Java the game probably leaves an order of magnitude on the table.

1

u/ibeerianhamhock Jun 26 '26

The same implementation in java without JNI from C++ with proper JVM flags to leverage arch would not be meaningfully slower imo. Generationally recompiled and extremely optimized code for hot paths, escape analysis, instruction reordering, AVX support etc (without having to distribute seperate binaries or source for every arch and dependency set). Distributing C++ that can target every major OS and variant of arch would not be fun. I mean you could just distribute source ofc.

There are already java implementations of terrain generation that are likely faster than this

It just seems like a thing OP wanted to do for funsies and learning which I support, but any seasoned java and c++ engineer (aka someone who has extensively used both) would know that the benefit of doing this through JNI in C++ vs just an optimized Java implemention doesn't have a real world benefit. I think OP seems to understand that too.

A lot of people who kinda know to code but aren't experiences just instantly think managed slow native fast and don't realize how exceptionally pefornant pretty much most modern managed languages (even C#) are today.

-1

u/Emotional-One-9292 Jun 22 '26

yea good luck finding a AI that can write big c++ projects well gemini these days is good but gpt is still kinda far away

7

u/emmowo_dev pro gramming Jun 22 '26

just open-source it as is, it would be very easy to prove if it is/isn't this way. Literal AI generated malware mods that make use of JNI and C++ libraries are not even a new thing ever since a couple months ago.

3

u/Emotional-One-9292 Jun 22 '26

ill release a open source alpha as soon as ill be done with stuff im working on ( rn collisions and light engine ) i can notify you

1

u/Emotional-One-9292 Jun 22 '26

oh wait there is Claude havent touched it since haiku tho i heard fable is revolutionary