Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Anyone else here get a major kick out of refactoring and optimizing regularly?  (Read 2771 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Posted 2013-05-19 14:53:05 »

...because I seem to like doing it very much.

After hitting a somewhat logical break last week with my project, I felt I needed to do some good bit of refactoring, so I did, and afterwards, I loved how neat and tidy and proper everything was.

Now this weekend, I also hit a logical break, and during one occasion where I had to leave my game running for a while, I came back to discover that my memory was almost all used up. So I frantically searched for how to properly analyze memory leaks in Java and found a nice tutorial that makes use of JVisualVM, which eventually led me to find a texture I was repeatedly loading/creating inside the render() loop that was causing the memory usage buildup Emo

So anyway... is there anyone here who are compulsive refactorers/optimizers here? And what are the best practices/tips you can share regarding this?

Me, having newly discovered this way to analyze my memory usage, I think I'll get the urge quite often to optimize the memory usage of my project as much as possible. Although as of now, it seems that I'm barely hitting 90MB memory usage and my biggest memory hog is when I draw all of the sprites I'm using on-screen... which I'm looking at now and can't see how I can further optimize it to be more efficient.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 130
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-05-19 14:57:44 »

I wish I was good enough to make my code more efficient - but I don't have enough low-level knowledge. It's honestly not that much of a loss, though; I'd rather have a full game that hogs up CPU and RAM than a half-baked game that is super efficient. Optimizing is somewhat enjoyable, but you have to think whether your optimization is going to be worth the time you take to develop it, and if it's going to work at all.
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #2 - Posted 2013-05-19 15:09:45 »

I am absolutely the worst about this, I take forever just trying to find the best possible solution that takes the least amount of code possible. Its a really bad habit, but I REALLY hate writing inefficient code.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #3 - Posted 2013-05-19 15:11:34 »

Yeah it awsome to break down some code that uses like 50-100 ms to render and tweak it so it uses no more than 1ms.
But its important you can let it loose.
Often im spending to much time on optimizing stuff, what does not even need optimising.
So try to keep thinking, does this function really affect my performance?
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #4 - Posted 2013-05-20 01:14:39 »

I am absolutely the worst about this, I take forever just trying to find the best possible solution that takes the least amount of code possible. Its a really bad habit, but I REALLY hate writing inefficient code.

The more I go along with my project, the more I discover that I might be in this same boat.

Is it really all that bad? Tongue

Yeah it awsome to break down some code that uses like 50-100 ms to render and tweak it so it uses no more than 1ms.
But its important you can let it loose.
Often im spending to much time on optimizing stuff, what does not even need optimising.
So try to keep thinking, does this function really affect my performance?
Yeah, so basically the question is that is it enough that the code has good optimization, or should it run with the best possible optimization?

Personally, I might be the type to push myself towards the best possible optimization, but I'm the same as Jimmt... I also don't have enough low-level knowledge to know how to optimize a piece of code. Like now for example, my biggest memory hog is the code in my render() loop that draws all of my objects on the screen - specifically the SpriteBatch.Draw() command - but as much as I know, there's no other way to do that so I don't know how to further optimize it. Yet, for all I know, there might be a better way to handle things.

Offline davedes
« Reply #5 - Posted 2013-05-20 02:20:22 »

Why is SpriteBatch.draw a memory hog? All it does it places vertices in an array and moves a pointer forward.

Optimization is important to learn; but optimizing blindly is useless and a waste of your time. It would be much better to focus your energy optimizing practical and demonstrated bottlenecks. Especially bottlenecks on the GPU side of things; for example, something like this on desktop, or taking advantage of bilinear filtering and non-dependent texture reads for mobile blurs.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #6 - Posted 2013-05-20 02:29:12 »

^ Sorry, misused the term... I just meant to say that it's the biggest thing that's using memory in my game right now... not that it's a "memory hog" Tongue

So far, my game is taking up almost 90MB RAM and SpriteBatch.draw inside the render() loop is using the biggest fraction of that, which is not a lot Smiley

Online HeroesGraveDev

JGO Kernel


Medals: 249
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2013-05-20 05:19:26 »

I don't like major refactoring.

I carry on my project until I either finish of give up from lack of organisation*.

Any improvements to organisation of code are done in whatever next project I do.


*Most of the time I try to fix it up and get it good enough to be called a game.

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #8 - Posted 2013-05-20 05:35:11 »

I guess a big part of it is just that I'm very OC Grin Grin Grin

Offline relminator
« Reply #9 - Posted 2013-05-20 05:49:37 »

Profile your code.  Though I have no idea how to do that in java.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #10 - Posted 2013-05-20 06:15:10 »

What does that mean, to profile one's code?

Offline relminator
« Reply #11 - Posted 2013-05-20 08:56:40 »

It means that you run your actual application on top of a profiler so that you will have an idea of where to optimize.

I generally use that in my native binaries though.  I don't know how to do that in java but there's probably an switch to turn on profiling.
Offline pitbuller
« Reply #12 - Posted 2013-05-20 09:11:41 »

Optimizing blindly is lottery. If you use enough time you might guess winning ticket.
If you don't know the bottleneck all your time is wasted. Always profile.

ps. Optimizing with profiler is much more fun. You can quite accurately tell that all the effort is giving you something back.
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #13 - Posted 2013-05-20 09:22:22 »

I mostly use an stopwatch class to surround the function.
Not 100% accurate, but its really fast to see your changes.

Also i try to optimise the best way, so for example i never write to images / textures, but directly to a buffer.
I know these are micro optimalisations, but i try to avoid casts, and use bitwise operators when possible.
Offline StrideColossus
« Reply #14 - Posted 2013-05-20 09:31:32 »

I'm quite an aggressive re-factorer, if (for example) I find I've been unconciously using cut-and-paste code code that needs condensing into some common base-class or helper.  The only thing I would caution against is re-factoring as procrastination, i.e. fiddling with the code rather than actually implementing something Wink

For optimisation:  I try and aim for the 'cleanest' code that implements what I want and then profile to see what is bottle-necking (if anything), even if that's crude timer blocks around a section of code.  I'm definitely in the camp of avoiding prior optimisation, again it's easy to get bogged down in the details of trying to write some super-efficient section of code which doesn't actually need optimisation in the first place (and you end up making is complex and messy and thus bug-prone).

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #15 - Posted 2013-05-20 09:51:05 »

It means that you run your actual application on top of a profiler so that you will have an idea of where to optimize.

Is optimizing using JVisualVM the same as profiling my code?

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #16 - Posted 2013-05-20 09:52:52 »

Refactoring is an important part of being a developer. I'd even say if you aren't good at it or you don't like it that you are limiting yourself. I write code relatively loosely so I don't waste time perfecting code that might not be used. As I progress I go back and improve the code I'll actually be using. A developer that skips this step generates relatively low quality code. You need to hold the entire part of the code you are working with in your head and thoroughly understand it, have a plan for rewriting or restructuring it, and then implement that plan.

Refactoring is generally to improve the design of your code. It is easier to see what is needed so everything fits together nicely after you have something working, and you can still do so with an eye toward the future of the program. Optimization is different. First, it shouldn't be done at all unless there is a problem. That is a hard and fast rule. It is hard to follow, but it exists to keep you from wasting your time and making your code unreadable. Second, see the first.

@heisenbergman, yes.

Offline gouessej
« Reply #17 - Posted 2013-05-20 11:05:27 »

Hi

At first, you speak about refactoring. Personally, I prefer doing some small code refactoring regularily rather than doing bigger refactoring less frequently. Maybe you know my funny comparison, I try to keep my source code pretty in order to prevent myself from losing my motivation. I can't spend a lot of time in ugly source code.

Secondly, you speak about optimizing. Don't forget that "premature optimization is the root of all evil". I agree with Nate, it should be done when there is a problem. You have to check whether there is something wrong, you have to measure (I agree with pitbuller, "always profile") before starting to modify your source code in order to be able to check further whether your modification(s) really improve(s) the situation. For me it is possible to have a clean and efficient code at the same time. You can write unreadable and inefficient code too. In my humble opinion, before planning to use any known micro-optimization, think about macro-optimizations, think about using more efficient algorithms before thinking about how to improve their implementations. I obtained the biggest speedups by avoiding to do the most silly things when using OpenGL (for example never call Thread.sleep() when your OpenGL context is current) and by spending some time in choosing the algorithms (and implementing them) that fit the best to my problems. I have to admit that this last step can require months or even years of work but it has been very useful in my case. It wouldn't have been necessary if existing engines, frameworks and libraries already provided some working implementations of these algorithms even though some of them provide some useful abstract data types to implement them. I prefer releasing my source code so that other developers won't have to start from scratch if they need the same tools as long as they agree with the licensing terms.

Offline Roquen
« Reply #18 - Posted 2013-05-20 12:12:08 »

Best case micro-optimizations are linear increases...never forget that.  Sometimes linear increases are sufficient, but really you want to be pretty sure that there isn't a reasonable high level change that would give you more "bang-for-your-buck".
Offline Simn
« Reply #19 - Posted 2013-05-20 13:18:11 »

I had a moment a while ago where I did some work on my project and I suddenly just took a break and thought: "Man, this is not good at all". Tongue So I spent a couple of hours after that just optimizing and refactoring, and I was so glad afterwards. Everything behaved good, and looked nice and clean too.

I've still keep this trend; I try to write as good code as I can, an afterwards when I come back to it, I ask my self: "Is this good, or is this bad?". If it's bad, I just optimize/refactor it until I'm satisfied. Smiley

- Simn
Offline sproingie

JGO Kernel


Medals: 202



« Reply #20 - Posted 2013-05-21 21:04:09 »

It means that you run your actual application on top of a profiler so that you will have an idea of where to optimize.

Is optimizing using JVisualVM the same as profiling my code?

That's why visualvm calls those tabs "profilers", yes.  It's easier than hand-rolling timers and way superior to guesswork. But sometimes you need to log times so you can get an idea of the trend over time.  For that I recommend Metrics by the ever-awesome Coda Hale.  One annotation on a method is all it takes, and you can use visualvm's built-in graphing or export it to stuff like ganglia.

Offline pitbuller
« Reply #21 - Posted 2013-05-21 21:17:48 »

Just used two hours to optimize normal from height map(actual its just function) calculation at vertex shader. Was so happy when micro optimized function was two times shorter and faster. Some times optimized code looks ugly but sometimes you can reduce complexity without losing functionality.
Offline xsvenson
« Reply #22 - Posted 2013-05-21 22:54:15 »

JVisualVM is a decent profiler bundled with JDK since... I actually dunno when, I assume jdk5.
When optimizing or looking for performance gains in java land, one should defenitly start by running this tool.

Refactoring is an evolutionary step in the life of the code. There is the refactoring-at-large, which is an overhaul of the design which is a "stop-the-world" process. This is the kind of code maintenance the client doesn't want to pay for since there is no viewable, touchable benefit, except for the developer.
Then there is the lower refactoring, which is abstracting the code and removing duplications and general health level maintenance. This is the "runtime" proccess, where You can refactor the code while activly developing and as such I'm doing pretty much as soon as I see a need.

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (26 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!