Optimization for free

Discussion in 'Game Development (Technical)' started by princec, Jan 30, 2006.

Thread Status:
Not open for further replies.
  1. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    Javagaming.org thread about a GamaSutra article on games optimisation. Basic story: about half the optimisations given in the article are given for free in the Java/.net world. Just a morsel for thought.

    Cas :)
     
  2. Michael Flad

    Indie Author

    Joined:
    Aug 4, 2004
    Messages:
    190
    Likes Received:
    0
    The basic topic of the article is about mature optimization in terms of - don't to quick implementations in lowlevel stuff which you almost for sure have to do later on anyways or which won't cost you anything if done correctly from the start of the development. Seems that those talking about the article didn't get that idea.

    And the given samples are

    - avoid allocations
    - avoid RTTI
    - avoid aliasing problems
    - avoid per frame code (distribute over multiple frames)
    - avoid iterations (f.i. don't iterate over all game entities for aiming but have separte lists containing only targetable entities for a faster search)
    - profile inline functions (to learn more in detail which subpart of some bigger system/function requires the most of the performance - f.i. if it's something like updateparticles() you'd want to know if it's your mathcode or maybe something else)
    - process offline (use pak files for resources, do texture compression, texture paging whatever possible)

    I have to use Java since a few years but I'd pretty happy to learn which 50% they're talking about.
     
  3. Robert Cummings

    Original Member

    Joined:
    Apr 3, 2005
    Messages:
    1,155
    Likes Received:
    0
    Problem is, isn't Java not particularly amazingly fast anyway, so it's optimisations are really needed?
     
  4. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    Avoid allocations - never do this in Java: just allocate, unless the constructor is expensive.

    Avoid RTTI - irrelevant in Java, as RTTI is built-in and free.

    Profile inlined functions - irrelevant in Java. You don't even know what it's inlined - it figures this out for itself and automatically figures out what's best

    And no, I expect I can write Java code that'll thrash the pants off most people's C++ code given any non-trivial task :) Performance hasn't been a problem for a few years now.

    Cas :)
     
  5. Robert Cummings

    Original Member

    Joined:
    Apr 3, 2005
    Messages:
    1,155
    Likes Received:
    0
    I like the fact you use java and stick two fingers up. I use Blitzmax myself, and thats really similar to Java in a lot of ways.
     
  6. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    Better, in many ways, for games, but that's largely down to the libraries.

    Cas :)
     
  7. mr n00b

    Original Member

    Joined:
    Sep 9, 2005
    Messages:
    40
    Likes Received:
    0
    It's no more expensive to do dynamic allocation in C++ than it is in java. You should never do a lot of dynamic allocations, regardless of which language you chose.

    What do you mean it's free? It's no more free than C++ RTTI, the only difference is that in C++ you have the ability to turn it off if you don't need it.

    You don't have to manually inline functions in C++ either, a good compiler will do it for you. That poor C++ programmers inline too much is their problem, not C++.

    For good or evil C++ gives you extremely much control if you wan't it.

    I agree though that a good Java program can certainly beat a badly written C++ program, and vice versa...

    /mr n00b
     
  8. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    That is a statement which I am trying to get across to you is almost entirely but not quite 100% incorrect!

    You absolutely should allocate like crazy in Java. No ifs or buts except for that one caveat when your object is expensive to construct or collect for some reason. And it is several orders or magnitude faster than new or malloc in the C/C++ world (and reading between the lines, the garbage collector is faster than manual deletes in C/C++ as well*). A Java allocation is literally a register increment 99.999% of the time.

    Cas :)

    * Yes, really!
     
  9. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    So... your argument is "this is irrelevant in Java because I can't turn it off anyway?" You mean "free" as in "I have no choice but to pay this overhead so I'll use it anyway"?
     
  10. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    No, there is no overhead, therefore it is irrelevant. That's the point of the thread. C people can turn it on at a cost. Java people get it without any cost. See?

    Cas :)
     
  11. mr n00b

    Original Member

    Joined:
    Sep 9, 2005
    Messages:
    40
    Likes Received:
    0
    No I don't see, somewhere under the hood in Java there has to be some code that handles the RTTI and running that code must cost something.

    Or to put it another way, if it were impossible to turn of RTTI in C++ it would be as free there as it is in Java.

    (Perhaps you're joking and I don't get the point?)

    /mr n00b
     
  12. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    No, not joking - it's free. There is obviously a little memory overhead with RTTI but because the runtime is designed from the very ground up to understand RTTI it's far more optimised than the C++ solution. RTTI is used for all sorts of other things in Java. The VM uses the RTTI in all sorts of clever ways at runtime as well which C++ can't, and this makes RTTI actually increase performance in many situations. Nifty. You don't have to believe me but I do wonder why the possibility always seems so remote for old school coders. A bit like garbage collection - C programmers insist that new and delete are faster and as safe as the GC system in Hotspot in the face of all the actual evidence to show it, etc.

    Cas :)
     
  13. Greig Hamilton

    Original Member

    Joined:
    Sep 14, 2005
    Messages:
    29
    Likes Received:
    0
  14. PoV

    PoV
    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    2,132
    Likes Received:
    0
    I hate these coversations. And my daddy can beat up your daddy. 'cmon, both languages suck. Java is no better than C++ than C++ is better than Java. You pick what flaws you can live with, and which ones you can't, and choose a language appropriately. If PC's are enough, and you want instant cross platform capability, and you havn't been tainted by the vile beast that is J2ME, then Java's an option. If you can bare to wait 2-3 more instructions for built in RTTI, can't live without templates, operators, latest graphics tech, and actual support on non PC/Cellphone platforms, you go C++. You're not going to convince someone to switch sides by agressively telling them their choice sucks, or forwarding them to other fanboys that dig it. At least not here, where we're supposed to be a forum of professional developers from various backgrounds and motivations.
     
  15. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    As a professional developer I'd advise that you take heed of the gist of this thread though, which is that by and large programmers are wasting their time doing things that are already done for them better by cleverer people than they. Programmers are notorious for prima donna behaviour to the detriment of their colleagues and the business. The same mentality stretches right down to indie game development. New tools have come along in the last decade that let you concentrate your efforts more on the results rather than the path to getting there - embrace them and move on!

    And reading between the lines, if you want a professional programming job these days, and you only know C++... don't expect much interest from employers. Given that a lot of us in here rely on day job skills to make money in order to do the indie games thing it's even more pragmatic to investigate it. At this point I'd normally also advocate Blitz which has most of the good bits of Java and is even better for games dev but that won't be getting you any jobs either.

    Just food for thought.

    Cas :)
     
  16. Savant

    Original Member

    Joined:
    Feb 8, 2005
    Messages:
    1,674
    Likes Received:
    0
    Agreed 100%. I've been teaching myself other languages to keep my skills fresh and up to date. Lately it's been C# and I'll never go back to C++ if I can at all avoid it now.
     
  17. mr n00b

    Original Member

    Joined:
    Sep 9, 2005
    Messages:
    40
    Likes Received:
    0
    I would like you to present some real evidence that supports the following claims:

    1. Java RTTI is free. (Although you've already admitted that there is a memory overhead which makes it not free per definition. Since memory footprint and access pattern is the single most important thing when it comes to performance this can hardly be neglected).

    2. Java memory allocation is free in 99.999% of the cases and several order of magnitude faster than malloc. I'm interested to hear how a memory allocator that in the end in turn has to allocate memory using the OS kan be free. Also there is nothing that prevents me to overload the global operator new and delete in C++ to behave _exactly_ as the Java memory allocator.

    I'm also interested to hear in detail in which way Java uses "nifty" RTTI features to make programs run faster (specific code or cases please).

    Also, C++ is not a compiler but a language. There is nothing in the definition of C++ RTTI that makes it impossible to implement a solution as efficient as that Java uses.

    Nobody here has disputed the benefits of automatic garbage collection, so I don't see why you brought it up. I resent your sweeping generalizations about C programmers.

    I really shouldn't comment on this because I think this comment is a real low point of yours, but I just can't help myself so here goes.

    I'm living evidence of the contrary. Although I know a bit of Java, Python and some other languages I've never needed to use them proffessionally and I've gotten a lot of job offers (I haven't yet seen a job offer that I have found interesting which required me to know Java). Your claim is clearly unfounded since there are literay thousands and thousands of programmers who use C++ as their main programming language.

    On the other hand, if all you know is Java you won't be working in the mainstream (non indie) game industry, except perhaps as a tool programmer (and problably not even that since most studios use MFC/Visual Basic/.Net och Python with wxWindows).

    /mr n00b
     
  18. princec

    Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    4,873
    Likes Received:
    0
    No, go do your research yourself. I'm just suggesting you do some.

    The whole point of this thread is that C++ programmers claim they can implement anything and then spend a load of effort doing so, when it has already been solved in certain other languages, so why not take a look at them. You can solve any problem you like in C++, I'm sure. The Hotspot JVM is written in C++.

    Resent away to yourself then. I brought it up as an example of the class of ignorance that often bubbles up in threads like this. I'm sort of suprised it hasn't yet ;)

    Now you are deliberately being obtuse. I didn't say you can't get a decent paid job using C++, I suggested that it's rather easier to get a Java job than a C++ job, and it will continue to get easier in the future. Go here to examine first hand the trends which have been continuing steadily for the last decade. There are 50% more jobs available for Java alone versus C++, never mind .net. Also take note of the rates and salaries.

    Cas :)
     
  19. HairyTroll

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    582
    Likes Received:
    0
    Java is fast, however there is always a cost. The JRE trades memory for speed. All the tricks that the JRE uses to speed up execution (JIT, code profiling etc.) consume memory. Normal Java coding practices that work well on a PC will kill you dead on a console or embedded system where memory is at premium.

    As always, choose the best tools for the job.
     
  20. Vorax

    Original Member

    Joined:
    Jan 21, 2005
    Messages:
    349
    Likes Received:
    0
    Like Cas, I'm also a big Java proponent, but HairyTroll's words are wise.
     
Thread Status:
Not open for further replies.

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer