Jump to content
  • Advertisement
Sign in to follow this  
draginx

Metaprogramming?

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How many of you apply metaprogramming in your games/applications (I know it isn't used much in applications, more in games). If so, which method of metaprogramming do you do? Also, I was reading that metaprogramming slows down compilation time, yet, less execution time. To me, this doesn't sound logical (unless the compiler is adding more code to the execution file) can someone please help explain this to me? :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by draginx
How many of you apply metaprogramming in your games/applications (I know it isn't used much in applications, more in games)


I doubt that is true.

Anyway, why doesn't it make any sense? Execution time is much more important than compilation time. Or do you not understand why the compile time is slower?

Share this post


Link to post
Share on other sites
Quote:
Original post by draginx
Also, I was reading that metaprogramming slows down compilation time, yet, less execution time.


The most classical examples of this usually have to do with taking some mathetmatical formula, and using metaprogramming in such a manner that the compiler is forced to produce an acceptable calculation at compile-time (thus increasing compiliation time) as a constant, which is then used at run time (instead of calculating it then) - thus decreasing execution time.

Share this post


Link to post
Share on other sites
Thanks Monkey, that cleared it up for me. =) And yer, I didn't understand why execution time would be faster but now it's all clear.

Share this post


Link to post
Share on other sites
I'm no c++ wiz, but I've been slowly working on a collision detection library which uses what I believe are metaprogramming techniques. A comprehensive coldet library can be difficult to code because of the multitude of different tests that can be performed, i.e. distance, intersection, and raytracing tests with many different kinds of objects in both 2d and 3d. So far I've experimented, with some success, with handling all linear component distance tests, and all separating axis tests in 2d and 3d, with single parameterized functions. The trick of course is to coerce the compiler into generating code for each case - ray/AABB, AABB/AABB, OBB/OBB, etc. - that approaches the 'hand rolled' version in terms of efficiency. (This is because most SAT tests can take advantage of certain optimizations depending on what objects are being considered.)

There are also more generalized algorithms such as GJK which will handle most convex objects, not just polytopes. GJK requires less specialization, but I'm also hoping to use template techniques to roll 2d and 3d GJK into one implementation (although certain parts, such as EPA, will have to be specialized by dimension).

Share this post


Link to post
Share on other sites
Quote:
Original post by draginx
How many of you apply metaprogramming in your games/applications (I know it isn't used much in applications, more in games).


This isn't true.

Quote:
Original post by draginx
If so, which method of metaprogramming do you do?


Your question is way to generic, which programming language do you mean, etc, etc.

Quote:
Original post by draginx
Also, I was reading that metaprogramming slows down compilation time, yet, less execution time. To me, this doesn't sound logical (unless the compiler is adding more code to the execution file) can someone please help explain this to me? :)


Okay it sounds like you are referring to template metaprogramming, c++ templates where never originally designed to be used in that manner therefore compilation time is siginificantly longer than for languages that explicitly support metaprogramming at compile-time. The reason why it's slow is because of template instantiation and code generation, template instantiation is typically a recursive process.

Despite that there are methods of reducing compilation time siginificantly but not many people know about this or appreciate it. The gist of it is to only instantiate a template when necessary.

You can view template metaprogramming as form/sort of a purely functional language (i don't mean procedural there not the same) where you have meta-data which is immutable, meta-expressions (compile-time expressions), meta-functions, and the important ingredient of support for recursion.

By default evalution of meta-function arguments is strict (eager evaluation) however with a minor modification they can be evaluted in non-strict manner (lazy evalution). What this means for template metaprogramming is we reduce compilation time by only instantiating templates when necessary, only when they are needed. Also as is the case with lazy languages we can represent infinite (meta) data-structures.

The boost mpl library has been designed with lazy evalution in mind.

On a side note, metaprogramming is not only related to template metaprogramming and is not only for compile-time it can be done at runtime too.

Share this post


Link to post
Share on other sites
A Metaprogram is a program that manipulate programs.

The most common examples of metaprograms are compilers and interpreters - so everybody here uses metaprogramming in every application they write.

Assuming you mean to refer to C++ Template Metaprogramming, then I've never seen it used in any real-world application.

However, most applications I've seen or worked on have used code generators to some extent (most commonly translating a description of something in XML to a corresponding C++ class).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!