Complex pattern realization (C++)

This forum is for discussing shmup development, tools, engines and techniques.
Posts: 2
Joined: Wed May 15, 2013 12:49 pm

Complex pattern realization (C++)

Postby Noro » Wed May 15, 2013 2:01 pm

Hello everyone.

I'm developing a danmaku shmup with lots of bullets on screen and complex patterns for a while now. I've used to hard-code some patterns with only giving some parameters(angles, number of bullets etc.) in script but now I realize that I need a full-scriptable system for creating patterns using only script language. So I started to develop it and at some point came to a fork without quite knowing what path to take.

Let me describe my problem.
Thanks to the tutorials from this site I have a nice and tight optimized array of bullets in memory represented by static structure. Each bullet have a pointer to a pattern object that process it's movement each frame. Currently I store all data needed for patterns in bullet structure so I never need to create a pattern objects more than once at the initialization of the program. But this can be tricky if the pattern object created from script and I never know how much parameters it need to operate. Even if I give some extra memory for bullet structure(say, large array of parameters that can be freely used by scripted patterns), this is not guarantee that it will be enough at some level of complex nested patterns. And it will bloat my neat array of bullets. The obvious alternative is to create pattern objects for each bullet and store all needed data in patterns. But this way I need to create and destroy a lot of objects during program execution. And I don't have strong enough programming experience to estimate performance impact in this case.

So if anyone implemented something like this, can you give me some advise of pros and cons of both approaches, please?

User avatar
Posts: 7
Joined: Mon Aug 13, 2012 7:40 am

Re: Complex pattern realization (C++)

Postby jbadams » Sun May 19, 2013 1:19 am

What platform(s) are you targeting? If you're on PC it's likely that you won't experience noticeable performance problems from creating those additional objects.

I'd suggest implementing the simplest solution first, and then if -- and only if -- you're experiencing performance problems use a code profiler to find the offending code rather than simply guessing at where best to optimise. If you're targeting mobile platforms however, you may want to put more thought into a more efficient solution, but otherwise it's likely to be wasted effort. :)

User avatar
Posts: 8
Joined: Sun May 19, 2013 1:01 am

Re: Complex pattern realization (C++)

Postby Square » Mon May 20, 2013 1:21 am

It's hard to answer, or visualise your problem entirely from a forum post :)

I don't fully agree with jbadams's solution, there is always danger in taking the non-optimised route.

I tend to think progress in games creation is about your own architecture and reasons for designing it that way if that helps weigh up the options.

Though, here a lot of professional programmers would test parts of both scenarios, I get the feeling that you should if you have the time to do that.
Or - simply jotting down the factors, making lists of pro/con estimations on paper or something similar. A diagram or two?

I think you'd be surprised what you can ascertain from getting the facts out of your head and in front of you.

User avatar
Posts: 76
Joined: Mon Aug 20, 2012 8:11 pm

Re: Complex pattern realization (C++)

Postby wondersonic » Mon May 20, 2013 6:29 am

Square wrote:I think you'd be surprised what you can ascertain from getting the facts out of your head and in front of you.


100% agree!

User avatar
Posts: 34
Joined: Sun Jul 29, 2012 4:44 pm

Re: Complex pattern realization (C++)

Postby kdmiller3 » Wed May 22, 2013 2:00 am

Heavily scripted- or data-driven designs naturally trade some performance for flexibility but I wouldn't fret about it too much to start with. It's more important to get things working correctly and reliably. Unless you're doing something seriously sub-optimal and targeting an unusually weak platform, you'll be able to hit monitor refresh rate without trying.

"Premature optimization is the root of all evil (or at least most of it) in programming." --Donald Knuth

User avatar
Posts: 8
Joined: Sun May 19, 2013 1:01 am

Re: Complex pattern realization (C++)

Postby Square » Wed May 22, 2013 5:49 pm

Looking at the original post, I have to say I disagree.

There is nothing wrong with optimisation at this point. The quote by Donald Knuth I believe is not in terms of "don't optimise" and I don't believe Noro's solution is "premature optimization", from what I gather he can think about it a lot.

I'm just thinking about influence and what we are saying here, which is why I suggested he investigates the solutions.

User avatar
Posts: 7
Joined: Mon Aug 13, 2012 7:40 am

Re: Complex pattern realization (C++)

Postby jbadams » Thu May 23, 2013 10:24 am

Absolutely, investigating alternatives is a great idea, but Noro has stated that he doesn't have enough experience to estimate performance impact, in which case the only way he can investigate the alternatives is to actually implement them and measure the performance of each. Given that option, it's sensible to start by implementing the simplest solution, and then only attempting further optimization if the initial implementation is found to be insufficiently performant or if he simply wants to do so as an educational experience.

It's always good to use the most optimal solution possible, but this has to be balanced against the cost in development time, and it's certainly misleading to say that "there's always danger in taking the non-optimised route"; a non-optimal solution is very often fast enough when running on modern hardware, and when this is the case there's absolutely no "danger" in using that implementation. Unless you actually measure the performance of an implementation and find it to be insufficient you may well simply be wasting time by searching for a more efficient alternative. That's fine if you're happy to spend the time, if performance is particularly important to you, or if you just want the educational experience, but it's simply not something you need to do every time.


I'm not saying not to optimize at all -- I'm simply recommending that unless Noro wishes to do so to learn he should optimize intelligently by first measuring his performance so that he can base his decisions on actual data rather than guesses. Many developers -- especially those who are less experienced -- will discover through profiling that the places they were originally intending to optimize aren't the parts of their program that will offer the most benefit.



Nolo: If you'd like to learn more about optimization and how to estimate the relative performance impact of different solutions I would recommend researching "algorithmic complexity" and "Big O notation", as well as learning to use a code profiler to actually measure performance. It may be a valuable learning exercise to implement both solutions in this case and measure the performance of each.

If however you're less interested in the learning experience and more interested in producing a working program I stand by my original suggestion; implement the simplest solution that correctly solves your problem, and then optimize only if you find the performance to be insufficient.


For reference, the Donald Knuth quote Square mentions above is:
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.


It's likely that your solution to implementing bullet patterns falls into that 97% of cases where optimization would be unnecessary; if you want to optimize anyway as a learning experience that's fine, but otherwise you shouldn't waste your time unless you have evidence that it falls into that other 3% where it can make a noticeable difference.

Posts: 2
Joined: Wed May 15, 2013 12:49 pm

Re: Complex pattern realization (C++)

Postby Noro » Mon May 27, 2013 3:39 pm

(Have been on my leave, away from computer)

Thank you all for great advices. Have to remember em all if I'll be working on commercial project some day. But now I'm doing it for the fun of it and yes, for learning experience.
I think I'll try simplified examples of both approaches and look what I get from profiler, after all.

Posts: 1
Joined: Sat Jun 29, 2013 2:52 pm

Re: Complex pattern realization (C++)

Postby Odessi » Sat Jul 06, 2013 1:12 pm

I created a language for scripting bullet patterns a few years ago. It's here if you want to take a look: http://code.google.com/p/bulletscript/

Return to Development

Who is online

Users browsing this forum: No registered users and 6 guests