⚡ perf(vscode): 代码阅读预览发给页面的块带内容指纹:扩展端按卡片内容、在源码里占的地方连同子块的指纹算摘要,页面按指纹认出没变的块沿用原来的对象,不再逐块深比内容,5126 行的文档到了比对 10.6ms → 0.4ms

This commit is contained in:
cruldra committed 2026-10-13 16:39:59 +11:00
1 parent ad5f559e92
commit 266dc42bc5
13 files changed
+194 -26

No files matched your search

@@ -1,5 +1,43 @@
import type { UnhashedBlock } from './testBlocks'
import { describe, expect, it } from 'vitest'
import { shareUnchanged } from './sharing'
import { shareBlocks, shareUnchanged } from './sharing'
import { hashed, NO_SPAN } from './testBlocks'
describe('新文档里指纹没变的块沿用上一份的对象', () => {
function card(id: string, text: string, children: UnhashedBlock[] = []): UnhashedBlock {
return { id, anchor: id, kind: 'statement', span: NO_SPAN, attributes: {}, concepts: [], children, translation: [{ text, role: 'plain' }] }
}
const PREV = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e')])
it('整份一样:交回上一份本身;没有上一份时原样交回', () => {
const same = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e')])
expect(shareBlocks(PREV, same)).toBe(PREV)
expect(shareBlocks(undefined, same)).toBe(same)
})
it('子块改了:它和包着它的块是新的,别的块(连同改了的块旁边没变的子块)还是原来的对象', () => {
const next = hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'D')]), card('2', 'e')])
const shared = shareBlocks(PREV, next)
expect(shared).toEqual(next)
expect(shared).not.toBe(PREV)
expect(shared[0]).toBe(PREV[0])
expect(shared[2]).toBe(PREV[2])
expect(shared[1]).not.toBe(PREV[1])
expect(shared[1].children[0]).toBe(PREV[1].children[0])
expect(shared[1].children[1]).not.toBe(PREV[1].children[1])
})
it('多了、少了块:那一层是新的数组,没变的块照样沿用', () => {
const longer = shareBlocks(PREV, hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c'), card('1.1', 'd')]), card('2', 'e'), card('3', 'f')]))
expect(longer).not.toBe(PREV)
expect(longer.slice(0, 3)).toEqual(PREV)
longer.slice(0, 3).forEach((block, index) => expect(block).toBe(PREV[index]))
const shorter = shareBlocks(PREV, hashed([card('0', 'a'), card('1', 'b', [card('1.0', 'c')]), card('2', 'e')]))
expect(shorter[1]).not.toBe(PREV[1])
expect(shorter[1].children[0]).toBe(PREV[1].children[0])
expect(shorter[2]).toBe(PREV[2])
})
})
describe('新消息里没变的部分沿用上一份的对象', () => {
const prev = {