• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

JakeWharton/confundus: Kotlin compiler plugin which brings Kotlin/JS's unsaf ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

JakeWharton/confundus

开源软件地址(OpenSource Url):

https://github.com/JakeWharton/confundus

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

Confundus: Unsafe Cast for Kotlin/JVM

A Kotlin compiler plugin which brings Kotlin/JS's unsafeCast to Kotlin/JVM.

The unsafeCast method bypasses the normal safety of the type system allowing you to reinterpret a nullable reference as non-nullable or a type as a subtype without the overhead of Kotlin's regular check.

Okay but why?

Sometimes you know more than the compiler. If you know a nullable reference is actually non-null you can use !! for a non-null reference. If you know an Any is actually a String you can cast with as String for a string reference.

Both of these are runtime-checked operations. This means that there is bytecode which validates the conversion. You'll get a NullPointerException from !! when the reference is null because of the IFNONNULL bytecode and a call to Instrinsics.throwNpe. Similarly, you'll may get a TypeCastException from as String because Kotlin checks the reference is non-null using that IFNONNULL bytecode, even if the Kotlin type is already non-null! Both of these behaviors are desired because they help maintain Kotlin's type system at runtime.

When writing performance-sensitive code these runtime checks can sometimes have a prohibitive cost.

For example, in a doubly-linked structure, invariants mean that if self.next is non-null then self.next.prev is also non-null. These next and prev properties are required to be marked as nullable to support leaf nodes (such as the head or tail in a list). Thus, we have to use !! even after checking that self.next is non-null. This requires an additional method call and null check which will always succeed.

unsafeCast allows you to perform the same operation as !! but with zero runtime overhead. You can use your invariants as a guarantee to bypass the safety the compiler otherwise provides. Similarly, for as casts, unsafeCast produces minimal bytecode (just the CHECKCAST bytecode) to reinterpret a reference as a new type.

With unsafeCast, you can take performance-sensitive code where the overhead of !! or as has been measured to produce overhead and reclaim that performance at the expense of safety. Basically, you go back to the reduced safety of Java.

Supported conversions

Nullable to non-null type

 val o1: String? = someFunction()
-val o2: String = o1!!
+val o2: String = o1.unsafeCast<String>()
  ALOAD 0
- DUP
- IFNONNULL L1
- INVOKESTATIC kotlin/jvm/internal/Intrinsics.throwNpe ()V
-L1
  ASTORE 1

Non-null to non-null subtype

 val o1: Any = someFunction()
-val o2: String = o1 as String
+val o2: String = o1.unsafeCast<String>()
  ALOAD 0
- DUP
- IFNONNULL L1
- NEW kotlin/TypeCastException
- DUP
- LDC "null cannot be cast to non-null type kotlin.String"
- INVOKESPECIAL kotlin/TypeCastException.<init> (Ljava/lang/String;)V
- ATHROW
-L1
  CHECKCAST java/lang/String
  ASTORE 1

Note: The CHECKCAST bytecode is required by the VM otherwise it will produce a VerifyError.

Nullable to non-null subtype

 val o1: Any? = someFunction()
-val o2: String = o1 as String
+val o2: String = o1.unsafeCast<String>()
  ALOAD 0
- DUP
- IFNONNULL L1
- NEW kotlin/TypeCastException
- DUP
- LDC "null cannot be cast to non-null type kotlin.String"
- INVOKESPECIAL kotlin/TypeCastException.<init> (Ljava/lang/String;)V
- ATHROW
-L1
  CHECKCAST java/lang/String
  ASTORE 1

Note: The CHECKCAST bytecode is required by the VM otherwise it will produce a VerifyError.

Nullable to nullable subtype

Note: No change in bytecode for this case!

 val o1: Any? = someFunction()
-val o2: String? = o1 as String?
+val o2: String? = o1.unsafeCast<String?>()
 ALOAD 0
 CHECKCAST java/lang/String
 ASTORE 1

Usage

buildscript {
  dependencies {
    classpath 'com.jakewharton.confundus:confundus-gradle:1.0.0'
  }
}

apply plugin: 'org.jetbrains.kotlin.jvm' // or .multiplatform
apply plugin: 'com.jakewharton.confundus'

The unsafeCast API will be made available in your main and test source sets but will not be shipped as a dependency of the module.

License

Copyright 2020 Jake Wharton

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
coding-blocks-archives/Conduit_Android_Kotlin发布时间:2022-08-13
下一篇:
abos3d/CleanArchitectKotlinFlowHiltSimplestway发布时间:2022-08-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap