Compare commits

..

1 Commits

Author SHA1 Message Date
ruinivist ad1242ad67 perf: optimize byte length calculation to avoid allocation 2026-06-03 07:55:45 +00:00
4 changed files with 32 additions and 28 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
## 2024-06-25 - Excalidraw Memoization
## 2025-06-03 - Avoid TextEncoder for string byte length
**Learning:** The `@excalidraw/excalidraw` package's `Excalidraw` component is exceptionally expensive to re-render. Even though `EditorCanvas` was wrapped in `React.memo`, passing an inline arrow function to `onExcalidrawAPI` in the parent `App` broke memoization, causing severe input lag when typing in completely independent UI elements like the codeblock editor sidebar due to the entire canvas re-rendering.
**Action:** When passing callbacks to heavy third-party components like Excalidraw, always wrap them in `useCallback` hook to preserve their prop stability and maintain `React.memo` benefits, preventing disastrous performance regressions on typing/input.
**Learning:** `new TextEncoder().encode(text).byteLength` allocates a `Uint8Array` for the entire string, which causes significant memory allocations and is ~100x slower for large JSON payloads in Bun/Node.
**Action:** Use `Buffer.byteLength(text)` when available (with a fallback to `TextEncoder` for browser compatibility) to compute string byte lengths without memory allocation overhead.
+7 -9
View File
@@ -307,10 +307,6 @@ function PrivateApp() {
[],
);
const handleExcalidrawAPI = useCallback((api: ExcalidrawImperativeAPI) => {
excalidrawApiRef.current = api;
}, []);
return (
<div className="app-shell" ref={appShellRef}>
{toastMessage && (
@@ -336,7 +332,9 @@ function PrivateApp() {
onSceneChange={handleSceneChange}
onSelectionStateChange={handleCodeBlockSelectionChange}
onEditorActivity={scheduleThemeTokenSync}
onExcalidrawAPI={handleExcalidrawAPI}
onExcalidrawAPI={(api) => {
excalidrawApiRef.current = api;
}}
renderEmbeddable={renderCodeBlockEmbeddable}
/>
<CodeBlockSidebar
@@ -359,12 +357,12 @@ function PrivateApp() {
onClose={closeSidebar}
onCreate={handleCreateDrawing}
onSelect={handleSelectDrawing}
onDelete={deleteDrawing}
onDelete={(drawingId) => void deleteDrawing(drawingId)}
onTitleChange={setActiveTitle}
onTitleSubmit={submitTitle}
onTitleSubmit={() => void submitTitle()}
onPublicationSlugChange={setPublicationSlug}
onPublish={publishPublication}
onDisablePublication={disablePublication}
onPublish={() => void publishPublication()}
onDisablePublication={() => void disablePublication()}
/>
</div>
);
+14 -15
View File
@@ -1,4 +1,3 @@
import { memo } from "react";
import { type DrawingMeta, type DrawingPublication } from "../../core/shared";
type DrawingSidebarProps = {
@@ -10,14 +9,14 @@ type DrawingSidebarProps = {
publicationSlug: string;
publicationBusy: boolean;
onClose: () => void;
onCreate: () => void | Promise<void>;
onSelect: (drawingId: string) => void | Promise<void>;
onDelete: (drawingId: string) => void | Promise<void>;
onCreate: () => void;
onSelect: (drawingId: string) => void;
onDelete: (drawingId: string) => void;
onTitleChange: (title: string) => void;
onTitleSubmit: () => void | Promise<void>;
onTitleSubmit: () => void;
onPublicationSlugChange: (slug: string) => void;
onPublish: () => void | Promise<void>;
onDisablePublication: () => void | Promise<void>;
onPublish: () => void;
onDisablePublication: () => void;
};
function DrawerIcon() {
@@ -65,7 +64,7 @@ export function DrawingsToggle({ onClick }: { onClick: () => void }) {
);
}
export const DrawingSidebar = memo(function DrawingSidebar({
export function DrawingSidebar({
open,
drawings,
activeId,
@@ -134,7 +133,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
className="title-input"
value={activeTitle}
onChange={(event) => onTitleChange(event.target.value)}
onBlur={() => void onTitleSubmit()}
onBlur={onTitleSubmit}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.currentTarget.blur();
@@ -144,7 +143,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
<button
type="button"
className="icon-button drawing-delete-button"
onClick={() => void onDelete(drawing.id)}
onClick={() => onDelete(drawing.id)}
aria-label={`Delete ${drawing.title}`}
>
×
@@ -172,7 +171,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
<button
type="button"
className="secondary-button"
onClick={() => void onPublish()}
onClick={onPublish}
disabled={publicationBusy}
>
{publication.enabled ? "Update link" : "Publish"}
@@ -180,7 +179,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
<button
type="button"
className="secondary-button"
onClick={() => void onDisablePublication()}
onClick={onDisablePublication}
disabled={!publication.enabled || publicationBusy}
>
Unpublish
@@ -206,7 +205,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
<button
type="button"
className="drawing-link"
onClick={() => void onSelect(drawing.id)}
onClick={() => onSelect(drawing.id)}
>
<span className="drawing-title">{drawing.title}</span>
</button>
@@ -215,7 +214,7 @@ export const DrawingSidebar = memo(function DrawingSidebar({
<button
type="button"
className="icon-button"
onClick={() => void onDelete(drawing.id)}
onClick={() => onDelete(drawing.id)}
aria-label={`Delete ${drawing.title}`}
>
×
@@ -227,4 +226,4 @@ export const DrawingSidebar = memo(function DrawingSidebar({
</aside>
</>
);
});
}
+8 -1
View File
@@ -75,7 +75,14 @@ export function normalizeScene(input: unknown): ScenePayload {
}
export function parseJsonBody<T = unknown>(text: string): T {
if (new TextEncoder().encode(text).byteLength > MAX_SCENE_BYTES) {
// Optimization: TextEncoder.encode creates a new Uint8Array and allocates memory for the entire
// string, which is slow for large JSON payloads. Buffer.byteLength counts bytes without allocation.
const byteLength =
typeof Buffer !== "undefined"
? Buffer.byteLength(text)
: new TextEncoder().encode(text).byteLength;
if (byteLength > MAX_SCENE_BYTES) {
throw new HttpError(413, "Payload too large");
}