Code Inlining

Code Inlining, also known as inline expansion or simply inlining is a compiler technique that is commonly used to optimize the code that you write. This is done by replacing the call to the target method with the code that we were calling.

Before Inlining:

public static void MyMethod(string parameter) {
  var number = Sum(3, 2);
}

public static int Sum(int a, int b) {
  return a + b;
}

After Inlining:

public static void MyMethod(string parameter) {
  var number = 3 + 2;
}

🤔 When to use Inlining on obfuscation?

To take the most advantage of code inlining, you need to think of the reason behind why functions exist. They were made to make it easier for us to maintain and understand code, it is a common practice for us developers to divide 1 function into multiple methods to improve readability and maintainability.

By inlining a function, you completely erase its existence and thus makes it stealthy against potential attacks that a hacker could perform on that function.

Simultaneously, the callee method becomes larger, making the code harder to analyze.

⚠️ Is it fine to inline everything?

Inlining comes with the downside that if used too much, it stops being a performance benefit. Instead, it will slow down your code.

Inlining your code has two potential issues. First, your application will become slightly bigger, depending on how many times the inlined function is referenced, this difference becomes more or less noticeable. Second, when you inline too many methods into one, the runtime compiler (JIT) will have a worse code generation. Refer to this page if you want to learn more about this subject.

The reason for this decrease in performance is due to the compiler chasing a faster compilation time, by inlining too many functions we make the code harder to analyze for the compiler, and thus disables parts of the optimizations that would be performed in our original application.

Be advised that those are worst-case scenarios, in general terms, you won't have to worry about inlining.

Last updated