Opt-in destroy() alongside FinalizationRegistry?

Đang mở
#239 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

@dannyball710 đang làm issue này rồi.

Từ ngày 13/10/2026.

  • #240 của @dannyball710 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
20/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
nodejs, typescript, wasm
Lĩnh vực
api, performance

Hướng nghiên cứu

This is an API design question, so the first step is to read MIGRATING.md and the discussion in #177 to see why destroy() was removed in favour of FinalizationRegistry. The proposal touches the owning classes (Blob, Face, Font, FontFuncs, Buffer, DrawFuncs, PaintFuncs) and needs a maintainer decision on whether an opt-in destroy() belongs in the API before any code is written. Done would mean that decision is recorded and, if accepted, the idempotent destroy and use-after-destroy error are tested.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I understand that v1 replaced the destroy() methods with FinalizationRegistry on purpose (#177, MIGRATING.md), and automatic cleanup is the right default. I'd like to ask whether an opt-in way to free an object immediately would be welcome alongside it, for workloads where finalization comes too late.

The problem. My use case is an application that shapes text with many user-supplied fonts in one process. FinalizationRegistry callbacks only run on an event loop turn after a garbage collection, so code that creates large objects without yielding keeps all of them alive, and the WASM memory fills up even though only one object is live at a time. Test case: make a Blob + Face + Font from the same 13 MB font data, drop them, and repeat 400 times. Results with upstream main plus #238:

between rounds result GCs (--trace-gc)
nothing (synchronous loop) WASM memory full at round 157 (2 GiB): Failed to allocate 13631488 bytes 21
await Promise.resolve() same, round 157 21
await setImmediate 400 rounds ok, peak RSS 974 MiB 16
await setTimeout(1) 400 rounds ok, peak RSS 856 MiB 17

So the garbage collector does run, but the memory only comes back after a macrotask turn, and even then the dropped 13 MB faces pile up to almost 1 GiB at the peak while only one face is live. The WASM memory never shrinks, so the process keeps that peak. A server that builds a face per request from a font cache, or a batch job that goes through a font collection in one synchronous pass, runs into this.

Without #238 it is worse: the failed allocation is not detected, the font data is copied over the start of the WASM memory, and the next finalizer that runs traps with RuntimeError: memory access out of bounds inside FinalizationRegistry.cleanupSome. The error is uncaught and exits Node.js with code 1. That PR makes the failure a catchable error, but the memory still cannot be released early.

Proposal. A strict addition, nothing changes for code that doesn't use it:

  • destroy() on the classes that own native memory: Blob, Face, Font, FontFuncs, Buffer, DrawFuncs and PaintFuncs. It frees the native object right away and unregisters it from the registry (registry.register(obj, cleanup, obj) + registry.unregister(obj)), so the finalizer never runs a second time. Calling it again does nothing.
  • Using an object after destroy() throws a JS error instead of passing a freed pointer to HarfBuzz.
  • Optionally the same method as [Symbol.dispose](), so that using face = new hb.Face(blob); frees the face at the end of the scope, in keeping with the "nothing to remember" spirit of v1.

Objects that depend on others (a Face on its Blob, a Font on its Face) already hold their own HarfBuzz references, so destroying in any order is safe.

We already carry the core of this (destroy + unregister, idempotent) as a small local patch, tested in our application. If you're open to the idea, I'd be happy to send the full version above as a PR, with tests in the test suite and a README/MIGRATING update. If you'd rather keep it out of the API, that's fine too, and good to know.

Ngôn ngữ chính
TypeScript
Star
297
Fork
53
Merge trung bình
36 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của harfbuzz/harfbuzzjs

Tất cả issue của harfbuzz/harfbuzzjs

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.