【发布时间】:2019-05-26 11:27:24
【问题描述】:
我想知道,内联小型私有函数是否是个好主意?
在我的例子中,这些函数只是为了可读性而存在的,我知道它们只被调用了几次,所以更大的字节码大小是无关紧要的。
我知道,性能提升也可能微不足道,因为我没有传递函数类型(编译器实际上会警告我),但我们假设它是我们应用程序中的热点。
实际示例:
我有一个理论上无限的符号带(例如图灵机的带),它由两个数组(位置 = 0 的左侧和右侧)建模。现在我已经进行了读取和写入操作,这些操作被认为被调用了很多次。
在java中我有:
/**
* @param cell the cell
* @return the symbol at the specified cell
*/
public char read(int cell) {
char[] tape;
if (cell < 0) {
tape = left;
cell = -cell - 1;
} else {
tape = right;
}
return cell < tape.length ? tape[cell] : blank;
}
/**
* Writes a symbol to the specified cell.
* @param c the symbol
* @param cell the cell
*/
public void write(char c, int cell) {
char[] tape;
if (cell < 0) {
cell = -cell - 1;
if (cell >= left.length) left = expandArray(left, cell, blank);
tape = left;
} else {
if (cell >= right.length) right = expandArray(right, cell, blank);
tape = right;
}
tape[cell] = c;
}
现在我想将 sn-p 翻译成 kotlin,阅读内联函数并想出这个:
fun read(cell: Int = headPosition) = when {
cell < 0 -> read(left, -cell - 1)
else -> read(right, cell)
}
private inline fun read(tape: CharArray, cell: Int): Char {
return if (cell < tape.size) tape[cell] else blank
}
fun write(c: Char, cell: Int = headPosition) = when {
cell < 0 -> left = write(c, left, -cell - 1)
else -> right = write(c, right, cell)
}
private inline fun write(c: Char, tape: CharArray, cell: Int): CharArray = when {
cell >= tape.size -> expandArray(tape, cell, blank)
else -> tape
}.also { it[cell] = c }
我个人认为,尤其是阅读功能,很容易阅读。
那么这是一个好主意吗?我可以忽略 IDE 的警告吗?还是我错过了什么?也许有一个最佳实践或其他模式来编写这些函数而无需重复(几乎)相同的行两次(对于位置 = 0)。
【问题讨论】:
-
您必须进行测量(这在 Java 中很难正确完成)。但我会遵循 IDE 的建议。如果它是热点,JIT 可能无论如何都会内联代码。
标签: kotlin inline readability