Anyone else here get a major kick out of refactoring and optimizing regularly?

…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.

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.

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.

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?

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? :stuck_out_tongue:

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.

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.

^ 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” :stuck_out_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 :slight_smile:

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.

I guess a big part of it is just that I’m very OC ;D ;D ;D

Profile your code. Though I have no idea how to do that in java.

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

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.

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.

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.

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).

Is optimizing using JVisualVM the same as profiling my code?

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.

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.

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”.

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”. :stuck_out_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. :slight_smile: