Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why `Intrinsics.checkParameterIsNotNull` is not inlined?

In Kotlin, if we decompile some Kotlin bytecode to Java, we can often see this statement which does null checks:

Intrinsics.checkParameterIsNotNull(foo, "foo")

If we explore furthur, we can see the decompiled implementation of this method, it's implementation is exactly the same as it's name:

public static void checkParameterIsNotNull(Object value, String paramName) {
    if (value == null) {
        throwParameterIsNullException(paramName);
    }
}

Due to the existense of this class (Intrinsics), I can't use Kotlin without the stdlib, even if I tried my best to avoid using functions from stdlib, it automatically generates calls to Intrinsics.checkParameterIsNotNull.

Since the implementation of this method is very short (and appears very very often), why isn't this function inlined?

Is there some annotations that allow us to prevent Kotlin compiler from generating this null check? (maybe something like @TrustedNotNull)

For ProGuard: I'm worried because I'm working on shared libraries and ProGuard is not suitable for me in this case. I obviously know those code elimination tools (ProGuard, dce-js), and I know how and when to use them.
I'm just asking why is a function not inlined.

like image 729
ice1000 Avatar asked Dec 16 '17 09:12

ice1000


2 Answers

My guess is that a call to this final method is more efficient than inlining it.

Although the code may appear simple, its byte code is rather long. Inlining it would hurt efficiency.

like image 135
voddan Avatar answered Nov 12 '22 11:11

voddan


Hopefully someone will show up and give a nicer and more official technical answer, but for starters, I think avoiding (or even trying to avoid) the standard library is not the right approach in general. It contains essential constructs of the language that you probably should be using in any Kotlin codebase. Even the docs say that:

The Kotlin Standard Library provides living essentials for everyday work with Kotlin.

If you're worried about its size, you can use platform specific tools to strip any parts of it that you don't use - for example, you can use Proguard for Java, and the DCE plugin for JavaScript. Most of its contents aren't inter-dependent, so these tools can drastically reduce the amount of code your final output will contain from the standard library.

like image 23
zsmb13 Avatar answered Nov 12 '22 13:11

zsmb13