mirror of
https://github.com/2234839/web-font.git
synced 2026-11-30 03:04:07 +08:00
refactor: 裁剪产物统一为单一共享 LRU 缓存,消除 glyph-match 双份缓存
- shared.ts 的 subsetCache 条目改为 {bytes, ttf},/api 裁剪与找字体共用同一份产物
- 容量计费含 ttf 对象开销(17 倍 bytes 估算),仍是 subsetCacheMaxSize 统一限额
- key 命名空间统一:版本指纹:进程启动时间:字体路径:outType:文本
This commit is contained in:
parent
053cfe5bc7
commit
d5ba12b2d9
@ -5,38 +5,13 @@
|
||||
* 不缓存完整字体对象,也不为了生成预览再发起第二轮全库裁剪。
|
||||
*/
|
||||
import { Font } from "../../vendor/fonteditor-core/lib/ttf/font";
|
||||
import { jsonResponse, findFontPath } from "../shared";
|
||||
import { jsonResponse, findFontPath, subsetCache } from "../shared";
|
||||
import { SUBSET_CACHE_KEY } from "./subset";
|
||||
import { readdir } from "../interface";
|
||||
import { fontDirs, subsetQueueTimeoutSeconds } from "../config";
|
||||
import { getGlyphBitmap, glyphSimilarity, hasGlyph, type SubsetTtfObject } from "../font_util/glyph_bitmap";
|
||||
import { estimateSubsetMemory, subsetFontFile } from "../font_util/subset_file";
|
||||
import { withMemoryGate } from "../subset_queue";
|
||||
import { LruCache } from "../lru_cache";
|
||||
|
||||
/** 进程启动时刻,重启即变化 —— 与 subset.ts 同机制,避免开发态旧缓存复活 */
|
||||
const PROCESS_START_TIME = Date.now();
|
||||
/** 版本指纹(构建期由 tsdown define 注入);发版后旧缓存自动失效 */
|
||||
const RESOLVED_VERSION = typeof PACKAGE_VERSION !== "undefined" ? PACKAGE_VERSION : "dev";
|
||||
|
||||
/** 字形匹配裁剪产物的内存估计系数:ttf 对象(字形数组/typed array 开销)相对序列化 bytes 的倍数 */
|
||||
const TTF_OBJECT_OVERHEAD = 16;
|
||||
|
||||
interface CachedSubset {
|
||||
/** 序列化裁剪字体(发前端预览) */
|
||||
bytes: Uint8Array;
|
||||
/** 评分用内存子集对象(OTF 输入时为 null);已重建子集 cmap */
|
||||
ttf: SubsetTtfObject | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字形匹配专用的裁剪产物 LRU 缓存:key 为 字体路径+文本+版本指纹。
|
||||
* 相同字体+相同文字的重复搜索直接命中,不再读源文件、不裁剪、不占内存闸门槽位。
|
||||
* 按内存容量淘汰(bytes 字节 + ttf 对象估算开销)。
|
||||
*/
|
||||
const glyphMatchSubsetCache = new LruCache<CachedSubset>({
|
||||
maxBytes: 64 * 1024 * 1024,
|
||||
sizeFn: (v) => v.bytes.byteLength * (1 + TTF_OBJECT_OVERHEAD),
|
||||
});
|
||||
/** 单个查询字形 */
|
||||
interface QueryGlyph {
|
||||
char: string;
|
||||
@ -132,12 +107,12 @@ async function* streamMatches(
|
||||
yield { type: "fontDone", font };
|
||||
continue;
|
||||
}
|
||||
const cacheKey = `${RESOLVED_VERSION}:${PROCESS_START_TIME}:${fontPath}:${text}`;
|
||||
const cached = glyphMatchSubsetCache.get(cacheKey);
|
||||
const cacheKey = `${SUBSET_CACHE_KEY}:${fontPath}:ttf:${text}`;
|
||||
const cached = subsetCache.get(cacheKey);
|
||||
let subsetBytes: Uint8Array;
|
||||
let ttf: SubsetTtfObject;
|
||||
if (cached) {
|
||||
/** 缓存命中:不读源文件、不裁剪、不占重任务槽位 */
|
||||
/** 缓存命中:不读源文件、不裁剪、不占重任务槽位(与 /api 裁剪接口共享同一份产物) */
|
||||
subsetBytes = cached.bytes;
|
||||
ttf = cached.ttf ?? readSubsetObject(cached.bytes);
|
||||
} else {
|
||||
@ -151,7 +126,7 @@ async function* streamMatches(
|
||||
subsetBytes = subset.bytes;
|
||||
/** 优先用裁剪管线返回的内存 ttf 对象(重建子集 cmap),免去序列化再解析(部分字体 re-parse 会失败) */
|
||||
ttf = subset.ttf ? buildSubsetCmapView(subset.ttf) : readSubsetObject(subset.bytes);
|
||||
glyphMatchSubsetCache.set(cacheKey, { bytes: subsetBytes, ttf: subset.ttf ? ttf : null });
|
||||
subsetCache.set(cacheKey, { bytes: subsetBytes, ttf: subset.ttf ? ttf : null });
|
||||
}
|
||||
let started = false;
|
||||
let hasMatch = false;
|
||||
|
||||
@ -33,7 +33,8 @@ const PROCESS_START_TIME = Date.now();
|
||||
*
|
||||
* 杜绝「子集化代码已修但缓存返回旧错误结果」的陷阱。
|
||||
*/
|
||||
const SUBSET_CACHE_KEY = `${RESOLVED_VERSION}:${PROCESS_START_TIME}`;
|
||||
/** 导出供 glyph-match 复用同一缓存命名空间(版本指纹+进程启动时间) */
|
||||
export const SUBSET_CACHE_KEY = `${RESOLVED_VERSION}:${PROCESS_START_TIME}`;
|
||||
|
||||
/** GET /api?font=...&text=... — 字体裁剪 */
|
||||
export async function handleFontSubset(req: Request, res: Response) {
|
||||
@ -80,7 +81,7 @@ export async function handleFontSubset(req: Request, res: Response) {
|
||||
const contentTypes: Record<string, string> = { ttf: "font/ttf", woff2: "font/woff2" };
|
||||
return {
|
||||
req,
|
||||
res: new Response(cached, {
|
||||
res: new Response(cached.bytes as unknown as BodyInit, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": contentTypes[outType] || "font/ttf",
|
||||
@ -118,8 +119,8 @@ export async function handleFontSubset(req: Request, res: Response) {
|
||||
const newFont = subsetResult.bytes;
|
||||
const t3 = Date.now();
|
||||
|
||||
/** 写入裁剪结果缓存 */
|
||||
subsetCache.set(cacheKey, newFont as unknown as ArrayBuffer);
|
||||
/** 写入裁剪结果缓存(与找字体共享;ttf 对象一并缓存供评分直接复用) */
|
||||
subsetCache.set(cacheKey, { bytes: newFont, ttf: subsetResult.ttf ?? null });
|
||||
|
||||
const contentTypes: Record<string, string> = { ttf: "font/ttf", woff2: "font/woff2" };
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { fontDirs, subsetCacheMaxSize } from "./config";
|
||||
import { LruCache } from "./lru_cache";
|
||||
import type { SubsetTtfObject } from "./font_util/glyph_bitmap";
|
||||
import { path_join, readFile, stat, readdir } from "./interface";
|
||||
import { loadPersistedStats, scheduleStatsFlush, type PersistedStats } from "./stats_store";
|
||||
|
||||
@ -80,8 +81,27 @@ export function markStatsDirty(): void {
|
||||
/** 字体文件 LRU 缓存,最多保留 3 个最近使用的字体 buffer(按条目数淘汰) */
|
||||
export const fontBufferCache = new LruCache<ArrayBuffer>({ maxSize: 3 });
|
||||
|
||||
/** 字体裁剪结果 LRU 缓存(按字节容量淘汰) */
|
||||
export const subsetCache = new LruCache<ArrayBuffer>({ maxBytes: subsetCacheMaxSize, sizeFn: (v) => v.byteLength });
|
||||
/**
|
||||
* 字体裁剪产物缓存条目:bytes 供 HTTP 下载/前端预览;
|
||||
* ttf 为内存子集对象(cmap 已重建,可直接光栅化评分),OTF 输入时为 null。
|
||||
* 不为序列化已产出的对象单独留副本,两处消费方共享同一份产物。
|
||||
*/
|
||||
export interface SubsetCacheEntry {
|
||||
/** 序列化裁剪字体(供 HTTP 下载/前端预览) */
|
||||
bytes: Uint8Array;
|
||||
/** 评分用内存子集对象(cmap 已重建,可直接光栅化),OTF 输入时为 null */
|
||||
ttf: SubsetTtfObject | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字体裁剪结果共享 LRU 缓存(按内存容量淘汰)。
|
||||
* key 由消费方自定(版本指纹:字体路径:outType:文本);
|
||||
* 容量计费 = 序列化 bytes + ttf 对象估算开销(16 倍),避免缓存对象本身把容器排薄。
|
||||
*/
|
||||
export const subsetCache = new LruCache<SubsetCacheEntry>({
|
||||
maxBytes: subsetCacheMaxSize,
|
||||
sizeFn: (v) => v.bytes.byteLength * (v.ttf ? 17 : 1),
|
||||
});
|
||||
|
||||
/**
|
||||
* 在所有字体目录中查找字体文件
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "webfont",
|
||||
"private": true,
|
||||
"version": "1.14.3",
|
||||
"version": "1.14.4",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "pnpx tsx scripts/dev-all.ts",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user