Perf: Use a computed cache for masked shape page bounds (#3460)

This PR adds a computed cache for masked shape page bounds, which speeds
up visibility checks (a lot!).

### Change Type

- [x] `sdk` — Changes the tldraw SDK
- [x] `improvement` — Improving existing features
pull/3453/head^2
Steve Ruiz 2024-04-13 21:07:10 +01:00 zatwierdzone przez GitHub
rodzic 143755fda0
commit 87f70b7de5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 18 dodań i 15 usunięć

Wyświetl plik

@ -4057,22 +4057,25 @@ export class Editor extends EventEmitter<TLEventMap> {
*/
getShapeMaskedPageBounds(shape: TLShapeId | TLShape): Box | undefined {
if (typeof shape !== 'string') shape = shape.id
const pageBounds = this._getShapePageBoundsCache().get(shape)
if (!pageBounds) return
const pageMask = this._getShapeMaskCache().get(shape)
if (pageMask) {
if (pageMask.length === 0) return undefined
return this._getShapeMaskedPageBoundsCache().get(shape)
}
const { corners } = pageBounds
if (corners.every((p, i) => p && Vec.Equals(p, pageMask[i]))) return pageBounds.clone()
// todo: find out why intersect polygon polygon for identical polygons produces zero w/h intersections
const intersection = intersectPolygonPolygon(pageMask, corners)
if (!intersection) return
return Box.FromPoints(intersection)
}
return pageBounds
/** @internal */
@computed private _getShapeMaskedPageBoundsCache(): ComputedCache<Box, TLShape> {
return this.store.createComputedCache('shapeMaskedPageBoundsCache', (shape) => {
const pageBounds = this._getShapePageBoundsCache().get(shape.id)
if (!pageBounds) return
const pageMask = this._getShapeMaskCache().get(shape.id)
if (pageMask) {
if (pageMask.length === 0) return undefined
const { corners } = pageBounds
if (corners.every((p, i) => p && Vec.Equals(p, pageMask[i]))) return pageBounds.clone()
const intersection = intersectPolygonPolygon(pageMask, corners)
if (!intersection) return
return Box.FromPoints(intersection)
}
return pageBounds
})
}
/**