Bun

Bun v0.1.9


Jarred Sumner · 2022 年 8 月 18 日

升級

bun upgrade

安裝

curl https://bun.dev.org.tw/install | bash

如果您在升級時遇到任何問題

新功能

  • @zhuzilin 修復了眾多崩潰問題!! 非常棒的工作。
  • [Linux] 改善了事件循環的可靠性,並降低了並行/非同步 IO 的 CPU 使用率 (主要影響 bun installfetch())
  • 由於優化了 Headers 的複製/讀取方式以及更快速產生的綁定,Bun.serve 在「Hello World」基準測試之外的速度提升約 20%
  • require("buffer")require("process") 現在指向 Bun 的實作,而不是 browserify polyfill (感謝 @zhuzilin)
  • 修復了可能導致 setTimeoutsetInterval 無法保持進程運作的錯誤 (感謝 @zhuzilin)
  • 已更新至最新的 WebKit
  • bun:ffi 中的 ptr() 速度提升 6 倍 - 10 倍

JIT 優化的 TextEncoder.encodeInto 效能提升可達 1.5 倍至 5 倍左右

image

此微基準測試中的 hash() 函數呼叫了 ptr()

image

內部原理

Bun 學習如何進行 JIT 編譯

DOMJIT 是一個 JavaScriptCore API,它為第三方嵌入器提供對 JIT 編譯器/組譯器的底層存取權,以優化原生函數和 getter/setter。 Safari 利用 DOMJIT 使常用屬性 (如 element.parentNode) 更快

Bun 現在開始使用 DOMJIT,首先從兩個地方開始

為了更好地支援 Bun 的使用案例,我擴展了 DOMJIT 以支援類型化陣列引數,並增加了對指定更多種類副作用的支援,這些副作用可以啟用/停用優化

  • https://github.com/oven-sh/WebKit/commit/d2ef2fd828761778a3b04289563946354a457e29
  • https://github.com/oven-sh/WebKit/commit/3262da8fd77667aaf95e75c7efa501dc135bc0ac
  • https://github.com/oven-sh/WebKit/commit/4ac5b04047a53ce2363495a79770a2ae6d67761b#diff-90a5d15b3faae39b6e5075902ae5fd2e0b7f7f40bb0590f3e298e7715db048e5

更快且更可靠的 JSC <> Zig 綁定

在 Bun 的編譯時,Bun 現在為 Zig 中實作的 JavaScript 物件程式碼產生 C++ 綁定類別。 以前,Bun 主要使用 JavaScriptCore C API。

使用 JavaScriptCore 的 C++ API 可以提高效能,並且對於使垃圾收集器更好地了解 Bun 的使用情況非常重要。 但是,手動編寫綁定可能會非常重複。

給定 JavaScript 中的類別定義 像這樣

define({
  name: "Response",
  construct: true,
  finalize: true,
  JSType: "0b11101110",
  klass: {
    json: {
      fn: "constructJSON",
    },
    // rest of the code
  },
  proto: {
    url: {
      getter: "getURL",
      cache: true,
    },

    text: { fn: "getText" },
    json: { fn: "getJSON" },
    arrayBuffer: { fn: "getArrayBuffer" },
    blob: { fn: "getBlob" },
    clone: { fn: "doClone", length: 1 },
    // rest of the code
  },
});

Bun 會產生對應的

這種方法的靈感來自 WebIDL 綁定,Safari 和 Chromium 都在使用它。

Linux 上更可靠的事件循環

此螢幕截圖是在模擬頻寬限制且未限制最大 http 連線數的情況下拍攝的

以前,bun 在這種情況下往往會掛起

image

變更內容

  • @zhuzilin 在 https://github.com/oven-sh/bun/pull/1050 中修復了 MD5 長度和編譯錯誤
  • @zhuzilin 在 https://github.com/oven-sh/bun/pull/1052 中修復了 appendFile 權限
  • 當表格的架構可能變更時,使欄位名稱快取失效,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1056 中完成
  • 重構 (src/tagged_pointer):IntPrimtiive -> IntPrimitive,由 @ryanrussell 在 https://github.com/oven-sh/bun/pull/1046 中完成
  • 移除 js 中的欄位名稱快取,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1057 中完成
  • [bun error] 修復 markdown.ts 中的錯字,由 @eltociear 在 https://github.com/oven-sh/bun/pull/995 中完成
  • [WIP] 原生 buffer 模組,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1067 中完成
  • 改善 Linux 上事件循環的可靠性,由 @Jarred-Sumner 在 https://github.com/oven-sh/bun/pull/1066 中完成
  • 新增合成 buffer 模組,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1076 中完成
  • Deps - 更新 libarchive,由 @jonacollazo 在 https://github.com/oven-sh/bun/pull/1078 中完成
  • 修復 timeout 任務的 active_task 計數,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1081 中完成
  • 修復伺服器區段錯誤,方法是不讓控制器過早被 GC,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1090 中完成
  • 新增原生 process 模組,由 @zhuzilin 在 https://github.com/oven-sh/bun/pull/1095 中完成

新貢獻者

  • @jonacollazo 在 https://github.com/oven-sh/bun/pull/1078 中做出了他們的首次貢獻

完整變更日誌