Hey everyone, today we’ll be going over inline in Haxe and how it can help you optimize your code. With that said, let’s get started by answering the question of what is inlining?

Inlining

In Haxe inlining allows you to take methods, properties, and module functions and turn them into the value of the identifier or the body of the function. Inlining lets you turn the function or property inside out. For example, something like the below function:

 public static inline function clampf(num: Float, min: Float,
   max: Float): Float {
  return Math.min(Math.max(num, min), max);
 }

When used in your code will just become this instead:

 Math.min(Math.max(num, min), max)

Here’s an example output from the try haxe website. https://try.haxe.org/#b53cC535

Now you might think that’s cool, but how does it benefit you?Let me explain that part right now.

The Power Of Inlining – Understanding Computers

As you know Haxe is a compiled language. It compiles your Haxe code into one of several different target languages (JavaScript, PHP, Java, C#, and so on). Each language has its own specification and optimization that are made when they’re either executing or running the code. Now, because Haxe compiles to other languages before being executed in most cases, you can make optimizations before the code is optimized again for execution. Computers have an easier time running values rather than finding out where that data is stored in memory with the said identifier.

Now, this is not a hard rule; it doesn’t apply in all cases. Those other cases brings me to my next point, which I’ll mention now.

Don’t Inline Everything

Inlining is useful in optimizing your code and improving code clarity. Using them on simple things such as constants in your code or simple functions. I would not use them on long stretches of code. It would be the same as copying code all over the place in JavaScript manually. That’s never good and you shouldn’t be doing that unless you want your program to run slower.

As you can see inlining gives you options that you’ve never had before; that’s something special and should be used wisely.

Conclusion

With that said, follow the blog for consistent weekly updates. I hope this helps with your game. I use inlining all the time in mine. Good luck game making!!

References

https://haxe.org/manual/expression-inline.html

https://haxe.org/manual/class-field-inline.html

%d bloggers like this: