TerrariaClone



  • Back when I was first learning to program in Java, I decided to try to make a clone of the excellent PC game Terraria. Of course, I was convinced that my version would have many more features than the official one.

    But before I realized how silly that idea was, I produced 11,000 lines of, to date, the most atrocious code I have ever seen in my life. I make it available here mostly as a cautionary tale of what can happen if you don't pay attention to the quality of your code.

    Bonus wtf in issues, about the real terraria



  • Scrolling those Boolean arrays is hypnotic



  • So as to avoid needing to declare local variables and loop indices, all of the variables for everything are declared globally at the class level.

    Why? Why would anyone ever?

    The TerrariaClone.init() method, which is over 1,300 lines long, actually grew so large that the Java compiler started running out of memory trying to compile it! The solution? Copy half of the init() code into a new method, called codeTooLarge(), and call that from init().

    :weary:

    I see people have started making pull requests. A playable game may come out of this yet.



  • @zecc said in TerrariaClone:

    I see people have started making pull requests.

    Like this one:

    :laughing:



  • The TerrariaClone.init() method, which is over 1,300 lines long, actually grew so large that the Java compiler started running out of memory trying to compile it!

    Not that I would want to endorse 1.3k LOC functions, but the compiler barfing on a puny 1.3k LOC (or even 10x that) would be a bit embarrassing on the compiler's behalf. I frequently see much longer functions and none of the compilers that I've encountered seem to be particularly bothered by this.



  • @cvi I only remember once seeing a compiler barf on a particularly big function (one that had two levels of switch, two or three times)... And that was Microsoft QuickC, a DOS compiler possibly as old as I am.


  • Impossible Mission - B

    @medinoc What this tells me is, you haven't played around nearly enough with code generation. :smiling_imp:



  • @medinoc IIRC I had an issue with VS2013 where I'd hit a particularly bad case with some generated code (~10k LOC). No crashes, just very slow compilation. It was fixed in one of the updates.

    I never directly had problems, just a user who had an old version and couldn't upgrade for some reason. I ended up splitting the method into a couple of smaller ones, since that was a rather trivial thing to do. The split version still has ~2.5 kLOC in the largest function, and that seems fine on my target compilers (tested on various versions of VS, GCC, Clang, PGI and Intel).


  • Impossible Mission - B

    @cvi said in TerrariaClone:

    IIRC I had an issue with VS2013 where I'd hit a particularly bad case with some generated code (~10k LOC). No crashes, just very slow compilation. It was fixed in one of the updates.

    I torture-tested the Boo compiler one time with a project that was a few megabytes of macro-heavy generated code. It took about 8 hours to build. Profiling showed that almost all of that time was being spent in name lookup, re-running every lookup from first principles. After a bit of very conservative caching, I got it down to about 6-8 minutes. (There's more room for improvement there, but that would take some real work.)



  • @masonwheeler said in TerrariaClone:

    Profiling showed that almost all of that time was being spent in name lookup, re-running every lookup from first principles.

    Ugh. Yeah. That's critical, and usually surprising the first time you see it.


  • Impossible Mission - B

    @boomzilla What is "that" in this context? The quoted text refers to a few different things, and none of them seem to fit perfectly with what you said.

    Guessing you meant that it's critical to profile, and usually surprising the first time you see the results from the profiling?



  • @masonwheeler said in TerrariaClone:

    What is "that" in this context? The quoted text refers to a few different things, and none of them seem to fit perfectly with what you said.

    As I read it, it all refers to symbol lookup.

    @masonwheeler said in TerrariaClone:

    Guessing you meant that it's critical to profile, and usually surprising the first time you see the results from the profiling?

    That's often the case, yes, but I was specifically talking about this result.


  • Impossible Mission - B

    @boomzilla All right. That's why I asked. :P

    Toby Faire, when there's metaprogramming in the compiler, symbol lookup becomes harder to cache because the set of symbols available can be changed partway through the compile cycle. That's why my optimizations focused on external namespaces. (ie. stuff in referenced assemblies, rather than the code currently under compilation.) Even there, there were some caveats, but it made for some significant improvements.

    Trying to cache internal namespaces would provide a lot of additional speedup, but it'll be tricky to get right.



  • @masonwheeler said in TerrariaClone:

    tricky to get right

    Programming in a nutshell...
    I've had java barf on soap services (or the autogenerated code to interact with them) , 64kb you get in java, which isn't much at all. Not for autogenerated code anyway.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.