理解动态语言inline cache优化
首先我們理解Why?
# Why
動態語言跟靜態語言的一個區別就是類型是動態決定的,也就是在運行時才能知道類型。
這帶來什么問題呢?
比如最簡單的取值,table.height,靜態語言,編譯時就能知道offset_heigth,動態語言,編譯時不知道offset_heigth,需要運行時獲取。
class Thing {Thing(int) {height = 1}void get();int height;}Thing table = new Thing(1); table.height;?
如果是靜態語言,取值非常快,因為height的索引在編譯時就知道了,因此只需要一個load指令即可。
r0 = #table_ptr; $height = load(r0, #offset_height = 0)如果時動態語言,取值非常慢,編譯時不知道height的索引,需要進入運行時判斷height的索引,因此會需要一個call runtime,然后一個map查找索引。
r0 = #table_ptroffset_height = call_runtime(table_ptr, "height")$height = load(r0, #offset_height)void call_runtime(table_ptr, "height") {if (table_ptr has no type of "height") {map, index = new_map("height")table_ptr.map = map;return index;}if (table_ptr has type of "height") {map = table_ptr.mapindex = map["height"]return index} }可以看到,差了很多代碼。
?
# How
那么怎么改進呢?
引入fast path和slow path。非常經典。
因為第一次我們一定不知道類型,是必須進入runtime的,第一次是改進不了的。
但是第二次就可以。因為我們有了第一次運行,知道了第一次運行的結果,我們可以用這次結果設計為fast path。
這個改進的前提之一就是,動態類型對象的類型不會一直在改變,否則我們就一直進入不了fast path。
所以,假設第二次訪問時,類型沒有改變,因為我們在第一次存儲了類型信息,即,我們知道了offset_height,因此,訪問的代碼如下:
int get_height() {r0 = #table_ptr; if ( no change of type ) // the same map {// fast path$height = load(r0, #offset_height = 0) // we know the offset_height from pre-run.return $height } else {// slow pathreturn call_runtime_get_height(); }}在假設類型不是經常變化成立的情況下,那么動態類型語言和靜態類型語言在這里的差距就只有一個if語句的差距,而不是整個runtime了。
?
?
?
# 參考
https://en.wikipedia.org/wiki/Inline_caching
https://mrale.ph/blog/2012/06/03/explaining-js-vms-in-js-inline-caches.html
https://wingolog.org/archives/2018/02/07/design-notes-on-inline-caches-in-guile
?
轉載于:https://www.cnblogs.com/simpleminds/p/9956774.html
總結
以上是生活随笔為你收集整理的理解动态语言inline cache优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让女朋友能懂的网络技术篇之动态代理
- 下一篇: Spring MVC请求处理流程