Opt-in destroy() alongside FinalizationRegistry?
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 20/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- nodejs, typescript, wasm
- 领域
- api, performance
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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 thatusing 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.
- 主要语言
- TypeScript
- 星标
- 297
- 派生
- 53
- 平均合并
- 36 分钟
- 30 天内合并 PR
- 1
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
harfbuzz/harfbuzzjs 的其他 Issue
-
harfbuzz-subset.wasm has a fixed 65 MiB heap; instancing a large variable font silently drops `gvar`可能重新可做 @khaledhosny 于 29 天前认领,目前没有进行中的 PR。 未关闭
难度 4/5 1-2 天 新手友好度 55/100
harfbuzz/harfbuzzjs#246 · 1 条评论 · 已指派 1 人 ·
查看 harfbuzz/harfbuzzjs 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
MTES-MCT/monitorenv#3257 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
salesforce/sa11y#1022 ·
-
area/web platform/ios type/bug
难度 2/5 1-3 小时 新手友好度 68/100
anomalyco/opencode#67504 · 1 条评论 ·
维护者通常 1 天内回复
-
Kartographer map markers lose their geo: link on non-English wikis可能已有人在做 @playlikeboy 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
openzim/mwoffliner#3014 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 1 天内回复