Inline Callbacks: More Optimized, More Refactored

What’s cool here is that, unlike traditional inlining, we aren’t just performing inline expansion of some monomorphic function. Instead, via callbacks, we are able to define entire subprograms and sequences of statements to execute. This grants us amazing expressive power. We are able to write clear, readable code without sacrificing performance. Take this very simple example:

JavaScript++:
[js]
runTwice(function(){ log("Hello World!"); });
[/js]

Java:

[cpp]
for (var i = 0; i < 2; i++)
{
log("Hello World!");
}
[/cpp]

Both snippets of code are attempting to log a message twice. However, the JS++ code is much more readable.

At first glance, it would appear the JS++ code will run slower. After all, it’s dealing with two function calls versus the zero for Java. However, let’s take a closer look at how runTwice is actually defined in JavaScript++:

[java]
inline void runTwice(inline void callback())
{
callback();
callback();
}
[/java]

After the compiler desugars and performs inline expansion, our original code becomes:

[js]
log("Hello World!");
log("Hello World!");
[/js]

What you are seeing is that there is ZERO performance penalty.

The benefits are enormous from readability to refactoring to maintainability. Now that you’ve had a brief introduction of inline callbacks and the vast expressive power they offer you, let’s dive into deeper, more real-world examples.

Continue reading Inline Callbacks: More Optimized, More Refactored