Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

探索 Kotlin 中的隐性成本(第二部分) #1878

Merged
merged 6 commits into from
Jul 20, 2017

Conversation

Feximin
Copy link
Contributor

@Feximin Feximin commented Jul 11, 2017

@phxnirvana
Copy link
Contributor

领了红包的来校对@sqrthree

@linhe0x0
Copy link
Member

@phxnirvana 哈哈哈哈,好好干活~

Copy link
Contributor

@phxnirvana phxnirvana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

完成-。-


This is part 2 of an ongoing series about the Kotlin programming language. Don’t forget to read [part 1](https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62) if you haven’t already.
本文是正在进行中的 Kotlin 编程语言系列的第二部分。别忘了阅读[第一部分](https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62)如果你还未读过的话。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一部分的地址可以改成翻译后的地址

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的哈


Let’s take a new look behind the curtain and discover the implementation details of more Kotlin features.
让我们重新看一下 Kotlin 的本质,去发现更多 Kotlin 特性的实现细节。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

让我们掀起Kotlin的盖头来,细细看看她的脸(逃

@@ -50,7 +50,7 @@ public static final int someMath(final int a) {
}
```

There is however one less performance hit compared to lambdas: because the actual instance of the function is known from the caller, its *specific* method will be called directly instead of its *generic* synthetic method from the `Function` interface. This means that **no casting or boxing of primitive types will occur when calling a local function from the outer function**. We can verify this by looking at the bytecode:
但是与 lambdas 相比有一个小的性能损失:由于调用者是知道这个函数的真正实例的,它的**特定**方法将被直接调用,而不是调用来自 `Function` 接口的通用合成方法。这意味着**当从外部函数调用局部函数的时候不会有强制类型转换或者基础类型装包现象发生**。我们可以通过查看字节码来验证这一点:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

装箱-。-

@@ -63,9 +63,9 @@ IADD
IRETURN
```

We can see that the method being invoked twice is the one accepting an `**int**` and returning an `**int**`, and that the addition is performed immediately without any intermediate unboxing operation.
我们可以看到那个被调用了两次的方法就是那个接收一个 **`int`** 参数并且返回一个 **`int`** 的方法,那个加法被立即执行并且没有任何中间的拆包操作。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拆箱-。-||

@@ -75,27 +75,27 @@ fun someMath(a: Int): Int {
}
```

Now the same `Function` instance will be reused an still no casting or boxing will occur. The only penalty of this local function compared to a classic private function will be the generation of an extra class with a few methods.
现在这个相同的 `Function` 实例将被重用,仍然没有强制类型转换或者装包情况发生。与典型的私有函数相比,局部函数唯一的缺点就是会额外生成一个有几个方法的的类。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重用 or 复用
然后装箱。。


One of the best features of the Kotlin language is that it makes a clear distinction between [nullable and non-null types](https://kotlinlang.org/docs/reference/null-safety.html). This enables the compiler to effectively prevent unexpected `NullPointerException`s at runtime by forbidding any code assigning a `**null**` or nullable value to a non-null variable.
Kotlin 语言中最好的特性之一就是明确区分了[可空与非空类型](https://kotlinlang.org/docs/reference/null-safety.html)。这可以使编译器在运行时通过禁止任何代码将 `null` 或者可空值分配给非空变量来有效地阻止意想不到的 `NullPointerException`。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得非空不等于不可空。。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你说的有道理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不可空是一种要求,非空是一种状态,不知道这么理解有没有问题。。。


This seems obvious but needs to be reminded: a nullable type is always a reference type. Declaring a variable for a primitive type as **nullable** prevents Kotlin from using the Java primitive value types like `**int**` or `**float**` and instead the **boxed reference types** like `Integer` or `Float` will be used, involving the extra cost of boxing and unboxing operations.
虽然显而易见,但仍需谨记:可空类型都是引用类型。将基础类型变量声明为 **可空**的话,会阻止 Kotlin 使用 Java 中类似 **`int`** 或者 **`float`** 那样的基础类型,相应的类似 `Integer` 或者 `Float` 那样的**装包引用类型**会被使用,这就引起了额外的装包或拆包成本。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

箱-。-


Contrary to Java which allows you to be sloppy and use an `Integer` variable almost exactly like an `**int**` variable, thanks to [autoboxing](http://docs.oracle.com/javase/8/docs/technotes/guides/language/autoboxing.html) and disregard of null safety, Kotlin forces you to write safe code when using nullable types so the benefits of using non-null types become clearer:
Java 中允许草率地使用与 **`int`** 变量几乎完全一样的 **`Integer`** 变量相反,由于[自动装包](http://docs.oracle.com/javase/8/docs/technotes/guides/language/autoboxing.html)和不需要考虑空值安全的原因,在使用可空类型时 Kotlin 会迫使你编写安全的代码,因此使用非空类型的好处变得越来越清晰:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

==我觉得草率地->随意地更好

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以及箱


> If you need an array for a non-null primitive type, prefer using `IntArray` than `Array<Int>` for example, to avoid boxing.
> 如果你需要一个非空的基础类型数组,最好用 `IntArray` 而不是 `Array<Int>` 来避免装包(操作)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有箱


```
printDouble(new int[]{1, 2, 3});
```

So there is the **overhead of the creation of a new array**, but this is nothing new compared to Java.
因此有创建一个新数组的开销,但与 Java 相比这并不是什么新鲜事。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边加粗

@tanglie1993
Copy link
Contributor

认领校对。

@linhe0x0
Copy link
Member

@tanglie1993 妥妥哒 🍻


> Local functions are an alternative to private functions with the added benefit of being able to access local variables of the outer function. That benefit comes with the hidden cost of the creation of a `Function` object for each call of the outer function, so non-capturing local functions are preferred.
> 局部函数有私有函数的一和替代,其优点是可以访问外部函数的局部变量。但是这些优点附带着隐性成本,那就是每次调用外部函数的时都会生成一个 `Function` 对象,所以最好用非捕获性的函数。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调用外部函数的时-->调用外部函数的时候

@@ -219,15 +216,15 @@ var10000.add(42);
printDouble(var10000.toArray());
```

In addition to the **creation of a new array**, a **temporary builder object** is used to compute the final array size and populate it. This adds another small cost to the method call.
除了**创建新数组**外,一个**临时的 builder 对象**被用来计算最终的数组大小并填充它。就就使得这个方法调用又增加了另一个小的成本。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

就就使得-->这就使得


```
fun printDouble(vararg values: Int) {
values.forEach { println(it * 2) }
}
```

Just like in Java, the `**vararg**` argument actually gets compiled to an **array** argument of the given type. You can then call these functions in three different ways:
就像 Java 中那样,**`vararg`** 参数实际上被编译为一个给定类型的 **`array`** 参数。你可以用三种不同的方式来调用这些函数:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里array要不要翻译成数组?

@tanglie1993
Copy link
Contributor

好了。

@linhe0x0
Copy link
Member

@Feximin 两位校对者都已经校对好了~ 可以来根据校对意见进行调整了哈 ┏ (゜ω゜)=☞

@Feximin
Copy link
Contributor Author

Feximin commented Jul 19, 2017

@phxnirvana @tanglie1993 @sqrthree 调整完了哈,感谢两位校对同学~

Copy link
Member

@linhe0x0 linhe0x0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一丢丢小问题辛苦调整下哈。

@@ -3,23 +3,23 @@
> * 原文作者:[Christophe B.](https://medium.com/@BladeCoder)
> * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner)
> * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO/exploring-kotlins-hidden-costs-part-2.md](https://github.com/xitu/gold-miner/blob/master/TODO/exploring-kotlins-hidden-costs-part-2.md)
> * 译者:
> * 译者:[Feximin](https://github.com/Feximin)
> * 校对者:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

校对者信息要加一下哈


This is part 2 of an ongoing series about the Kotlin programming language. Don’t forget to read [part 1](https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62) if you haven’t already.
本文是正在进行中的 Kotlin 编程语言系列的第二部分。别忘了阅读[第一部分](https://juejin.im/post/596774c96fb9a06bb95ae46a)如果你还未读过的话。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

『别忘了阅读第一部分如果你还未读过的话。』这句话语序更换一下是不是更符合中文的表达习惯?


Keep reading by heading to [part 3](https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-3-3bf6e0dbf0a4): *delegated properties* and *ranges*.
继续阅读[第三部分](https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-3-3bf6e0dbf0a4):**委派属性**和**范围**。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还没翻译完就把链接放上去,这样真的好吗?哈哈哈

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没关系,这样的话待译者翻译完合并之后这个链接就不需要更新就能正确访问了。

@Feximin
Copy link
Contributor Author

Feximin commented Jul 19, 2017

@sqrthree 麻烦再看一下哈

@linhe0x0 linhe0x0 merged commit 2bb6733 into xitu:master Jul 20, 2017
@linhe0x0
Copy link
Member

已经 merge 啦~ 快快麻溜发布到掘金专栏然后给我发下链接,方便及时添加积分哟。

cdadar pushed a commit to cdadar/gold-miner that referenced this pull request Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants