我有一个内联函数,可以接受两个lambdas。这个函数在我的代码的热点中调用,尽管它是内联的,但是为这些函数创建了数千个对象。
有趣的是评论,或用println
等简单的调用替换身体,一切都很完美。但由于某些原因我的具体用途,似乎内联未能完成其工作!
我该如何解决这个问题?内联体的相关限制是什么?这是一个错误吗?
我的用例:
fun PlayerSend.sync() = reusable({
val packet = this
it.syncMovement(packet)
reusable({ if (it.updateRequired) it.sync(this) }, {
packet.bits(8, 0 /* amount of players to update */)
if (readable > 0) {
packet.bits(11, 2047).byteAccess() + this
} else packet.byteAccess()
})
}, { ses + 81.byte + readable.short + this })
方法标题:
inline fun <R1, R2> reusable(use: ByteBufPacketeer.() -> R1, after: ByteBufPacketeer.() -> R2) {
val reusables = Reusables.get()
val count = ReuseablesCount.get()
ReuseablesCount.set(if (count + 1 >= reusables.size) 0 else count + 1)
with(reusables[count]) {
use()
after()
clear()
}
}