<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ashiqul Islam Shaon</title>
    <description>The latest articles on Forem by Ashiqul Islam Shaon (@shaon2016).</description>
    <link>https://forem.com/shaon2016</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F727883%2F1aa7ec68-2f77-45f7-b34a-6309793d0e61.png</url>
      <title>Forem: Ashiqul Islam Shaon</title>
      <link>https://forem.com/shaon2016</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shaon2016"/>
    <language>en</language>
    <item>
      <title>Kotlin-inline, crossline, noinline function and reified: Everything you need to know(Android) — Part 1</title>
      <dc:creator>Ashiqul Islam Shaon</dc:creator>
      <pubDate>Thu, 06 Apr 2023 16:55:19 +0000</pubDate>
      <link>https://forem.com/shaon2016/kotlin-inline-crossline-noinline-function-and-reified-everything-you-need-to-knowandroid-part-1-3cnb</link>
      <guid>https://forem.com/shaon2016/kotlin-inline-crossline-noinline-function-and-reified-everything-you-need-to-knowandroid-part-1-3cnb</guid>
      <description>&lt;p&gt;From Doc,&lt;/p&gt;

&lt;p&gt;"Using &lt;a href="https://kotlinlang.org/docs/lambdas.html"&gt;higher-order functions&lt;/a&gt; imposes certain runtime penalties: each function is an object, and it captures a closure. A closure is a scope of variables that can be accessed in the body of the function. Memory allocations (both for function objects and classes) and virtual calls introduce runtime overhead.&lt;/p&gt;

&lt;p&gt;But it appears that in many cases this kind of overhead can be eliminated by inlining the lambda expressions."&lt;br&gt;
Let's explore together…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main() {
    me({
        println(it)
    }, {
        println(it)
    })
}

fun me(myName: (String) -&amp;gt; Unit, myAge: (Int) -&amp;gt; Unit) {
    myName("Christiano")
    myAge(36)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without inline keyword, if you see Kotlin byte code, you will notice two new instances of special function types is created. Two new instances will allocate memory.&lt;/p&gt;

&lt;p&gt;However, here extra method call also happens. See the Kotlin decompile code carefully.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myName.invoke("Christiano");
myAge.invoke(36);
// (Function1)null.INSTANCE, (Function1)null.INSTANCE 
// instance of two higher order function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When these above code execute it also create two new function. Hence memory overhead.&lt;/p&gt;

&lt;p&gt;Kotlin byte code decompile (Java code)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public final class TestInlineKt {
   public static final void main() {
      me((Function1)null.INSTANCE, (Function1)null.INSTANCE);
   }

   // $FF: synthetic method
   public static void main(String[] var0) {
      main();
   }

   public static final void me(@NotNull Function1 myName, @NotNull Function1 myAge) {
      Intrinsics.checkNotNullParameter(myName, "myName");
      Intrinsics.checkNotNullParameter(myAge, "myAge");
      myName.invoke("Christiano");
      myAge.invoke(36);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;New special instance of special type is created and allocate memory.&lt;/li&gt;
&lt;li&gt;Extra method call always happens.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With inline function, there is no extra object allocation and no extra method calls. Let’s see Decompile code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static final void main() {
      int $i$f$me = false;
      String it = "Christiano";
      int var2 = false;
      System.out.println(it);
      int it = 36;
      var2 = false;
      System.out.println(it);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the help of inline function there is no extra method call but it substitute directly into the main code.&lt;/p&gt;

&lt;p&gt;Because of the inline keyword, compiler copies the content of the inline function to the call site avoiding creating a new function object.&lt;/p&gt;

&lt;p&gt;Decompile code using inline keyword&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public final class TestInlineKt {
   public static final void main() {
      int $i$f$me = false;
      String it = "Christiano";
      int var2 = false;
      System.out.println(it);
      int it = 36;
      var2 = false;
      System.out.println(it);
   }

   // $FF: synthetic method
   public static void main(String[] var0) {
      main();
   }

   public static final void me(@NotNull Function1 myName, @NotNull Function1 myAge) {
      int $i$f$me = 0;
      Intrinsics.checkNotNullParameter(myName, "myName");
      Intrinsics.checkNotNullParameter(myAge, "myAge");
      myName.invoke("Christiano");
      myAge.invoke(36);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see that there is not object allocation. Go to tools-&amp;gt;Kotlin-&amp;gt;Show Kotlin ByteCode. Now search for new keyword. Hence you will find no new keyword in the bytecode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bEkUGgVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxunfypyfwha0dl8hpml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bEkUGgVh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxunfypyfwha0dl8hpml.png" alt="Image description" width="880" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Don't use inline keyword for large function.&lt;/li&gt;
&lt;li&gt;Don't use inline keyword where there is no higher order function as parameter. Because you won't get significant performance benefits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Thanks for reading. Show me love by following and applause.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
    </item>
  </channel>
</rss>
