r/java • u/WolverineJones • Apr 29 '15
Tone it down a Notch
http://imgur.com/gallery/KRtc2fN78
u/stormcrowsx Apr 29 '15 edited Apr 29 '15
Minecraft does not perform well, which is usually the criticism it receives. If you put it beside most C++ games Minecraft feels clunky and slow. The strength of Java is not that it performed fast for Minecraft but Notch was able to program it quickly and as Minecraft has been passed around between developers they still seem to be able to easily add to it.
Java performs reasonably well and it maintains reasonably well. Java to me has been the language that's kind of an all-rounder, its decent in nearly every category you'd rate a language on. Being an all-rounder also means that it excels at very little compared to others which kind of makes it a boring language sometimes.
13
u/Scaryclouds Apr 30 '15
Comparing Minecraft to other "C++ games" is a meaningless endeavour. Far far more often is it the design of the system holding back performance rather than the language it was written in.
47
u/caltheon Apr 29 '15
Minecraft performance is bad because Marcus doesn't know how to optimize 3d graphics. His culling alone was pitifully done
35
u/Chops_II Apr 29 '15
Consider that, if you limit it only to the blocks you can see, and only to the up to 3 sides you can see at once, but mostly one, there can be literally millions of polygons on screen. It really isn't that easy to make a game like this perform well, in any language.
Other games don't face the same issue, because their maps aren't made of millions of individual objects.
It would be even worse if the objects were able to move often, then you couldn't even benefit from coalescing chunks.
If you have a good solution, I'd love to hear it.
16
u/caltheon Apr 30 '15
The lowest hanging fruit is obviously blocks not adjacent to a transparent block can be ignored. Next, grouping blocks of the same typr into larger blocks and rendering that instead of all the individual faces. A lot of this has been done by optiminer and the new team by this point.
10
u/Chops_II Apr 30 '15
Also, like you pointed out, Minecraft now does all this. So these aren't examples that prove that Minecraft is crap due to inexperience, as above post was trying to claim
5
u/caltheon Apr 30 '15
these were all done after Marcus left the project.
7
u/Chops_II Apr 30 '15
Fair enough. But you said Minecraft is bad, not was bad.
3
u/caltheon Apr 30 '15
True, I should have said was, though the original decisions he made still plague the game today
8
u/Chops_II Apr 30 '15
though the original decisions he made still plague the game today
Because I'm lazy, and because you made the claim, can you give any particularly notable examples of where this is the case?
2
u/Chops_II Apr 30 '15
Yep. There's a lot that can be done, but there are also, like you pointed out, complexities, like transparent blocks. Also, as I mentioned before, because the world can change, you have to recalculate the joined chunks every time they change.
This is OK for Minecraft usually, but for a game like Deposition, with real time physics, it's much harder to make use of those optimizations without the optimizing itself taking too long.
-5
u/SparrowMaxx Apr 29 '15
A world made exclusively of squares is probably the simplest exercise in performant world rendering possible.
11
u/Chops_II Apr 29 '15
Apart from the alternative that almost every other game has: single mesh map.
Sure, games throw doodads all over it to a greater degree than minecraft, but that's because they can afford to; they're not already rendering millions of individual objects like minecraft is.
12
Apr 29 '15
In before armchair programmers enlight in us on how would they do it... Ohh wait!
0
u/caltheon Apr 30 '15
Armchair professional Java developer please, and see my other comment
0
Apr 30 '15
[deleted]
3
u/caltheon Apr 30 '15
hindsight is 20/20. Dude had an AMAZING idea for a game that he wrote the base for in a matter of a week or so.
3
u/spiderzork Apr 29 '15
And Minecraft is working ok now, but that is only because everything that was done by notch has be rewritten.
1
3
Apr 30 '15
also, excessive threading. Everything is an object and each object has 4 threads. lol
CONTEXT SWITCH AWAY!
-3
0
Apr 29 '15
[deleted]
3
u/stormcrowsx Apr 29 '15
Calling native libraries libraries is not easy enough that I would consider it practical to just drop into a native language and use it when you need the performance. Python is even easier to call native code and slower performance (more incentive), and if you asked most python devs if they ever wrote or even used native code directly from their programs they'd probably tell you no. So if Python devs who have more incentive to use native code don't do it often, I don't forsee Java devs doing it.
0
Apr 30 '15
[deleted]
1
u/stormcrowsx Apr 30 '15
It was many many years ago that I tried but I had a lot of issues in deployment. "Game fails to start distro x version y" or "Not working on windows x". I have been interested in messing with libgdx which appears to be a more comprehensive solution than the libraries I used in the past.
5
u/cogman10 Apr 29 '15
I've never benchmarked it and could be wrong. But I believe the current FFI adds a lot of overhead. That means that if you are going to drop down to native code, you'll need to make sure that you doing a significant amount of work that overcomes the overhead of leaving the JVM.
4
u/mschaef Apr 30 '15
But I believe the current FFI adds a lot of overhead.
Are you sure about that? There are significant chunks of the Java runtime library that are built on JNI, some of which are rather performance critical.
FileInputStreamis an example that comes to mind immediately:3
u/karlthepagan Apr 30 '15
From memory these methods are white listed to bypass verifying the stack.
2
u/mschaef Apr 30 '15
Interesting... I'd love to know more.
2
u/karlthepagan Apr 30 '15
I'm a little under the weather, but while searching I find this interesting technique: avoid messing with the stack by using C++11 continuations.
http://nerds-central.blogspot.com/2012/04/extreme-jni-performance.html
0
Apr 29 '15
[deleted]
13
u/evilmidget38 Apr 29 '15
Minecraft's modability has very little to do with reflection, and a lot more to do with how easy it is to decompile Java and modify the bytecode. The modding platforms that users have created use reflection pretty heavily(Bukkit, FML[although they like their bytecode manipulation more than reflection], etc.), although this hasn't always been the case. Bukkit used no reflection for the first year or so of its existence, with the exception of JUnit's usage for unit tests. Forge had very little in the way of an API for a long time, and is primarily focused on binary patching of the MC jar, manipulating bytecode rather than using reflection. Admittedly I'm less familiar with Forge than I am with Bukkit. If you really want to just mod minecraft, all you need to do is replace some class files and delete the META-INF. Jar files being zip files makes this incredibly easy.
The ability to easily decompile Java, the heart of reverse engineering, is simply amazing. The decompilers are free and high quality, and while fernflower(the most popular one in Minecraft modding) is pretty bad, it still produces code that mostly compiles, with the exception of a couple of edge cases(fails to handle enums overriding methods, doesn't always generate throws declarations, etc.).
2
u/stormcrowsx Apr 29 '15
When it comes to java and opengl my experience has not been great with cross platform.
-8
u/DoktuhParadox Apr 29 '15
Well OpenGL kind of sucks, to be honest.
2
u/Chops_II Apr 29 '15
What alternatives are there to OpenGL that are cross-platform?
-1
u/DoktuhParadox Apr 29 '15
2
u/Chops_II Apr 29 '15
Being the only option doesn't mean it's the best option.
Uh... Yes it does. Perhaps you mean 'best possible option'? Then sure.
Maybe it would be more constructive if you explained why you think it's bad?
1
u/DoktuhParadox Apr 29 '15
That is indeed what I meant. I thought my way of putting it sounded wrong...
Really it could just be a bunch of developers using it incorrectly, because many games that use OpenGL are slow and crash many, many times more than with other graphical APIs.
Though, of course, that's not OpenGL's fault. Maybe I'm just a little salty.
1
u/Chops_II Apr 29 '15
I think Valve has seen performance improvement when switching to OpenGL. CBF looking it up right now, so prove me wrong if that's the case. Also, it is only one example, but they do some pretty neat stuff.
1
3
u/mikehaggard Apr 29 '15
What's the better API then?
-1
u/DoktuhParadox Apr 29 '15
Does something have to have a better version to be considered bad?
Being the only option doesn't mean it's the best option.
5
u/clutchest_nugget Apr 29 '15
Being the only option doesn't mean it's the best option.
(ノಠ益ಠ)ノ彡┻━┻
10
0
0
0
Apr 29 '15
[deleted]
-4
u/hothrous Apr 29 '15
My biggest thoughts on the uses of java are that it gets used in a lot of very predictable environments. The biggest advantage of Java is the JVM. I just think that using the JVM for everything is a waste when all of your servers are identical.
If your environment is predictable, then there isn't a huge benefit to Java.
0
Apr 29 '15
[deleted]
-2
u/hothrous Apr 29 '15
If all of your servers are hosted on VMs, then you can set up a single VM and then clone it a bunch of times to have nearly identical VMs. This is a predictable environment. An example being, all of my instances are running Ubuntu Server 14.04 using compiler X with Y libraries installed. So I write my code for Ubuntu Server 14.04 using compiler X with Y libraries installed. If I need to upgrade, I go through the motions to test on Ubuntu Server 15.04 using X compiler with Y libraries installed, and fix anything I need to fix. Which isn't any different than upgrading from Java 7 to 8 and testing and fixing anything that breaks.
The advantages of Java are primarily in unpredictable environments because the JVM compensates for the differences. In a predictable environment, you only have to compensate for the unexpected, which is something you have to do in Java anyways.
Essentially Java is the Microsoft Windows of the coding world.
3
u/mschaef Apr 30 '15
The advantages of Java are primarily in unpredictable environments because the JVM compensates for the differences.
The compensation is only partial, at best. In any event, there are other advantages worth mentioning. (High quality compiler, GC, good fault containment, large suite of libraries, etc.)
the JVM compensates for the differences.
If that's the sole objective, there are other means (portability libraries, etc.) that will serve as well, and do so less intrusively.
2
u/cogman10 Apr 29 '15
I agree. But I think the development cycle is pretty valuable for java. Right now we have developers using different versions of linux and windows. It wouldn't be to hard to add even more environments to the mix and still develop without having to launch a VM locally.
Another advantage is that your development machine can easily be a different uarch from your deployment server. While most everything is x86, if you want to run on something else it is possible (low powered ARM server?) without having a painful emulation layer.
1
u/mschaef Apr 30 '15
Another advantage is that your development machine can easily be a different uarch from your deployment server.
This is not necessarily exclusive to Java. Early in my career, I was involved in some embedded systems work that used the same C codebase across real-mode x86, protected-mode x86, mc68k, and Win32/x86. Several modules I was involved in were developed/maintained as Win32 apps and then ported into the embedded targets. This was done as small part of an effort that took at most 30-40 person-years over 5 calendar years, so the costs weren't ridiculously high.
-1
u/hothrous Apr 29 '15
That would be a situation where a predictable environment is not being achieved. To a certain degree, the development environment is on the developer, though. If those problems exist in many situations, the developer is just being stubborn.
1
u/cogman10 Apr 29 '15
Maybe not stubborn. It is pretty easy to add a build/library dependency that is platform specific without even realizing it. Heck, it can even happen just because newer versions of OSes don't support the old libraries needed to build things.
1
u/mschaef Apr 30 '15
That would be a situation where a predictable environment is not being achieved.
I think you're confusing predictability with consistency (or maybe homogeneity).
If those problems exist in many situations, the developer is just being stubborn.
It may also be related to the hardware/software stack that's been provided to the developer. In commercial settings, it's not uncommon at all that a developer has no choice over their development tools.
20
u/doctorsound Apr 29 '15
Since Minecraft's inception, noobs have been criticizing the use of Java, yet it's still going strong :)
19
u/dardotardo Apr 29 '15
With good reason, game performed horribly at first.
11
u/scalava Apr 29 '15
The server performance is still horrendous though...
3
u/dardotardo Apr 29 '15
Yeah, think most of the development effort has been expanding platforms (mobile, Xbox), and tuning client performance versus taking a look at the architecture driving the multiplayer, which I'm guessing is very basic and sending way too much information back and forth between server and clients.
Makes sense considering that's where the money is at, not the relatively small portion of people hosting very large servers.
9
u/vemacs Apr 29 '15
Not Java's fault, and plenty of Java-based servers run fine (Spigot)
8
u/scalava Apr 29 '15
True you definitely can't blame the language. But Minecraft servers weren't built to scale very much at all and the performance with 500+ players on a server proves this. Sure you can use BungeeCord to scale across multiple servers but it's a shame that a modern server fails to scale past a few hundred players.
5
u/vemacs Apr 29 '15
300 per instance is the peak I get with reasonable performance running Spigot on a 1271v3. It can definitely be improved, but compared with much simpler game servers (I'm talking Battlefield 4, GTA), it's not exactly worse.
2
u/cogman10 Apr 29 '15
Yeah, especially since server applications are java's bread and butter. If there is one thing java does well, it is acting as a long running server.
1
u/Cilph Apr 30 '15
Surprising considering a lot of large infrastructures run on Java and are highly performant.
7
u/BasilTarragon Apr 29 '15
Blame the worker, not the tools.
5
u/dardotardo Apr 29 '15
The tools can only be so good in this case, when dealing with garbage collection. I love Java, and use it professionally, but I would not use it for a game engine.
4
u/josefx Apr 29 '15
The tools can only be so good in this case, when dealing with garbage collection.
Even a language without GC will slow down when the developers spam memory allocations and the Minecraft developers seem to do exactly that http://www.reddit.com/r/Minecraft/comments/2k3rg6/water_woes/cljp483 . Memory allocations can be avoided, it only involves a lot work and thought ( a bit more with Java ).
0
u/dardotardo Apr 29 '15 edited Apr 29 '15
Which is exactly why I wouldn't recommend Java for consistent and reliable memory allocation such as Games. It's far to easy to get lazy in a managed language to treat memory as an infinite resource. With a language such as C, this is much more apparent.
Games are reliant on performance, performance requires consistency and granular control. Something you don't get with Java unless you start circumventing the JVM, which is not only ill advised by Oracle themself, but a path you do not want to travel down.
2
u/angryundead Apr 29 '15
Which is a shame, really. Java's ecosystem is, what I would call, extremely healthy. The third party libraries are great and most things are pretty snappy these days between a greater focus on event-driven/asynchronous programming and performance.
I feel like games written in Java could also benefit from the abundance of testing toolkits too. It's hard to imagine what is even going on in some of these games with bug reports like "the second time you do ${thing} the sound engine ignores the mute setting." (Paraphrased actual bug report from Hearthstone.)
In any case I'd like to see it happen but GC isn't the only problem with getting Java up to the levels of speed (especially inter-cpu communication and memory/gpu bandwidth) that is required for AAA (or, hell, AA) titles.
(The flip side of this is that the Unity engine seems to do all right and from all I've heard performance is... not a priority.)
0
Apr 30 '15
[deleted]
0
u/dardotardo Apr 30 '15
Yeah, I never said you couldn't do it in Java, I said it wasn't the intention of the language. By pooling you're ignoring what makes Java great, it's amazing JVM, and its excellent GC tools.
If you're going to pool resources and implement your own memory management system, do it in language that needs to, and therefore has 3rd party tooling behind it already, such as C or C++.
2
14
u/konk3r Apr 29 '15
Very true, but a screw and a nail both have their own strengths. It's always best to use the right tool for the job provided you're trained in using it.
35
2
1
u/caltheon Apr 29 '15
There aren't many uses for nails anymore. Self driving screws beat nails for most carpentry work except roofing.
7
u/konk3r Apr 29 '15
True, but if you are in an impoverished region and don't have the money for an automatic screwdriver, than nails are the proper choice.
Likewise, if you want the most efficient game engine possible, you're probably going to want to use some form of C. If you just want to make a game quickly, you grab unity (or some other premade engine). If you want to make a full game engine but it doesn't have very high requirements and you want it done quickly, Java will do fine.
7
u/irishdevil1 Apr 29 '15
Nails are used All the time in carpentry. As are screws. As has been said, different tools for different uses. Nails aren't going anywhere though.
4
u/BiberButzemann Apr 30 '15 edited Apr 30 '15
Are we seriously discussing nails v. screws in /r/programming /r/java? It was just a silly metaphor.
7
2
u/irishdevil1 Apr 30 '15
Apparently so. I'm a carpenter breaking into the programming field, if i were to use an incorrect analogy in programming i hope someone would correct it. Obviously we're off-topic at this point, just thought I'd clear that up in case any programmers are planning on taking up carpentry. Don't want them to neglect the all-important Nail.
1
2
u/Manitcor Apr 29 '15
Except managing the screw factory seems to be a major hassle. I went from making my building to managing a factory, when the hell did this happen?
Wait, there is a factory factory? GET ME ONE OF THOSE NAO!
1
u/jahannan Apr 29 '15
That's a complaint against enterprise design patterns, not Java itself. Admittedly a lot of libraries also follow the factory design pattern but it's entirely possible to write Java without having to know about factories. Hell, with a little Spring magic you can get all the functionality of factories without ever writing any factory code.
4
u/galaktos Apr 29 '15
1
u/TweetsInCommentsBot Apr 29 '15
I love it when inexperienced programmers tell me Java sucks. It's like a newbie carpenter saying nails are obviously better than screws.
This message was created by a bot
4
u/oddwhirled Apr 30 '15
Why would anyone say Java sucks, especially if they have never/barely used it! It's a great language, it's super functional, contemporary, and fairly easy!
1
u/matthedev May 01 '15
it's super functional
Lambda functions were introduced in Java 8, but super functional? ;)
2
u/HaMMeReD Apr 30 '15
Yeah, Java is only used for tons of mobile games really well.
However, I'd say that java is always about 5 years behind the performance curve of hard native code, but it comes with much higher portability and maintainability.
2
u/llogiq Apr 30 '15
... java is always about 5 years behind the performance curve of hard native code, ...
Depends on your target area. Case in point: I'm using Java in statistical data processing, and I benchmarked my main algorithm in well-optimized C as a proof-of-concept. Turns out that performance was approximately equal within 3% bounds, so I continued using java.
2
u/CubsThisYear Apr 29 '15
Anytime people talk about the inherent speed advantage that C++ supposedly has (spoiler alert: it doesn't), I press them to explain why this advantage would exist?
A few answers that are obviously wrong: -You can use pointers in C. You can use pointers in Java too, you just can't do arbitrary arithmetic on them. This is very rarely a performance advantage. -Java is an interpreted language. Not after startup it isn't. It's running the same (or better) machine code that GCC produces for equivalent operations. -Java has garbage collection. This actually makes allocation faster in most cases, since you don't have to track as much to do deallocation. If you GC, sure there is a cost, but high-performance apps in any language simply don't dynamically allocate memory.
There are a couple of points that might be valid: -you can allocate arbitrary objects on the stack in C++. It's just stupid that Java doesn't have this, but it's probably a minor point and its easily coded around. -you can write custom assembly. I'd argue this is to difficult and specialized to be useful in real development. Hardware changes to fast for this to be viable. Also you have to be really good to beat a dynamically optimizing compiler.
That's about it. C++ isn't magically faster, it's just slightly lower level, mostly in ways that don't matter.
2
u/mschaef Apr 30 '15 edited Apr 30 '15
A few years ago, there was a great post by one of the JGit developers on the relative disadvantages of Java vs. C as an implementation language for git. It's worth a read: http://marc.info/?l=git&m=124111702609723&w=2
You can use pointers in Java too, you just can't do arbitrary arithmetic on them. This is very rarely a performance advantage.
Many of the dangerous things that C lets you do with pointers that Java does not are useful to avoid memory copies. (pointer arithmetic, pointers into the middle of structs or arrays, and pointer type casts, etc.) Occasionally, it's useful to use pointer arithmetic to control the layout of memory structures to avoid allocations.
-you can allocate arbitrary objects on the stack in C++. It's just stupid that Java doesn't have this, but it's probably a minor point and its easily coded around.
How would you easily code around it? With a relocating GC, Java can reduce an allocation to a pointer bump, but still has significant costs to free the allocated memory. With stack allocation, both the allocation and the free amount to nothing more than a pointer bump. With this C function, the three ints are allocated and freed all at one with a single stack operation for allocation and a single stack operation for free.
#define STACK_BUF_SIZE (4) void my_fn() { int buf[STACK_BUF_SIZE]; // ... stuff here }That's not achievable in Java, unless you trust the JIT compiler to optimize away allocations, which isn't nearly as trustworthy as the guarantees that C provides.
edited: To correct embarrassing error /u/CubsThisYear pointed out in their response.
2
u/CubsThisYear Apr 30 '15
Interesting article - hard for me to say how specific that stuff is to git. My first thought is that it would pretty easy to just implement a few functions in C and use JNI where needed. Also primitive collections are not a big deal IMO. There are several good libraries that provide them.
As to avoiding GC, you just have to allocate all of your memory up front and manage it yourself, AKA exactly how you do it in C. You don't worry about the cost of freeing memory because you don't actually free it, you just reuse it. Note you don't do this for your whole program, only the spots where it matters.
Finally your example about stack allocation is very confused. Java would allocate those 3 ints in the exact same way that C would, in the stack. The difference comes when you are talking about complex objects. But as I said, you can just preallocate them and the problem basically goes away. Furthermore, it's a dumb problem that is created by Sun/Oracle's resistance to change. C# has had stack allocation for years and it is conceptually equivalent to Java.
My main point is that C++'s ratio of brokenness (lack of module system, arcane syntax, weird implicitness) to useful things is way too high a cost to pay in exchange for a small performance boost.
1
u/mschaef Apr 30 '15
Finally your example about stack allocation is very confused.
Thanks. It was a long day, and you're of course right. I've changed the example to be closer to my intent. Stack allocating temporary buffers isn't really possible on Java unless you trust the compiler to implement some fairly clever optimizations.
But as I said, you can just preallocate them and the problem basically goes away.
I don't know that the preallocation/pool strategy would work all that well for something like a stack allocated buffer. At the very least, it won't come close to matching C. Allocate a pool of
char[], and you have pointer chasing to get to the buffer, reduced locality of reference (even if the buffer is in the cache, it adds more cache pressure) and then the overhead of whatever algorithm is used to mange the pool.Switch to allocating segments of a single large 'char[]' can solve some of the issues, but then you need to be passing in both the base of the array and an offset into the array. This, of course, assumes that the interface being called supports both arguments, and even if it does, it means additional pressure on registers, etc.
Note you don't do this for your whole program, only the spots where it matters.
I'd be at least a little concerned about this approach if only due to the fact that switching from automatic to manual memory management isn't a search/replace type of thing.
Furthermore, it's a dumb problem
The fact it's a dumb problem doesn't make it any less of a problem.
Also primitive collections are not a big deal IMO. There are several good libraries that provide them.
I've had good luck with Trove.
Interesting article - hard for me to say how specific that stuff is to git.
Some of the stuff is relatively general. I've been hit with some of it, as well a few other issues, developing a small database system. (One specific example is the fact that there's no good way to definitely
munmapa file in the standard API.)
0
u/4forpengs Apr 29 '15
I love it when inexperienced programmers tell me Java sucks. It's like a newbie carpenter saying nails are obviously better than screws.
Well... maybe you're using screws when you should be using nails...
6
-9
Apr 29 '15
his comparison makes literally no sense.
26
u/darthtrevino Apr 29 '15 edited Apr 29 '15
He's saying different tools are suited to different jobs. Java doesn't inherently "suck", it has strengths and weaknesses like any other tool.
edit: Additionally, if you can't qualify why you think something sucks, then your opinion is worthless.
0
Apr 29 '15
[deleted]
13
u/SirChasm Apr 29 '15
Nah, if you have a powered screwdriver and a soft material, you can just force drive the screws in too.
2
Apr 29 '15
And that powered screwdriver is called "C++" :)
12
u/SirChasm Apr 29 '15
I think C++ is the embodiment of a tool you have to crank manually with all your might. So something like this
2
u/curiousGambler Apr 30 '15
If that thing is C++, the C is a simple pocket knife. Actually, maybe it doesn't even fold. But it's perfect :)
2
u/joequin Apr 30 '15
The power screwdriver is called intellij. It generates the majority of the boiler plate.
1
u/curiousGambler Apr 30 '15
This is a good point. One thing about Java is that when you write it in IntelliJ or Eclipse, you end up generating like 80% of a class, then writing only the interesting parts yourself.
Plus, you basically know if it will work or not before you even run it, which is nice. And this is really a compliment to Java, being designed in a way that is easily tooled. And the hard work of the people who build the tools of course.
1
u/DannyB2 Apr 30 '15
When the only hammer you have is C++ then all problems begin to look like a thumb.
1
39
u/camilos Apr 29 '15
Most people that hate java fall under two categories. Beginners who think Java is all about applets or swing apps. Users who are not happy about Java runtime being installed in their computers.
Basically, people who have no idea what makes java so useful and popular.