From b09ddd13b449b0361b2bb02fe11e29d081f107c3 Mon Sep 17 00:00:00 2001 From: Grant Date: Wed, 26 Jun 2024 18:38:46 -0600 Subject: [PATCH] massive performance rewrite - main canvas & blank canvas drawing moved to separate worker thread (if possible) - server jobs moved to separate process (fixing CPU leak on heatmap generation) - pixels now store if they are on top reducing db queries - remove various methods to store pixel data in redis, reducing delay for various actions additional fixed: (came up during performance fixes) - added square fill (fixes #15) - redraw loop (fixes #59) - added keybind to deselect current color (fixes #54) - pixel undos no longer delete the pixel from the db - server logging now indicates what module triggered the log --- Dockerfile | 2 +- docker-compose.yml | 11 + docker-start-worker.sh | 3 + .../client/src/components/CanvasWrapper.tsx | 86 ++++-- .../src/components/Moderation/ModModal.tsx | 3 +- .../src/components/Overlay/BlankOverlay.tsx | 48 +-- .../client/src/components/Toolbar/Palette.tsx | 25 +- packages/client/src/contexts/AppContext.tsx | 16 +- packages/client/src/lib/canvas.ts | 124 ++++---- packages/client/src/lib/canvasRenderer.ts | 264 ++++++++++++++++ packages/client/src/lib/keybinds.ts | 5 + packages/client/src/lib/network.ts | 9 + packages/client/src/lib/renderer.ts | 160 ++++++++++ packages/client/src/lib/utils.ts | 23 ++ packages/client/src/worker/render.worker.ts | 81 +++++ packages/lib/src/debug.ts | 12 +- packages/lib/src/net.ts | 9 +- packages/lib/src/renderer/lib/zoom.utils.ts | 1 - packages/server/prisma/dbml/schema.dbml | 5 +- .../migration.sql | 2 + .../20240620192400_is_top_pixel/migration.sql | 2 + .../migration.sql | 2 + packages/server/prisma/schema.prisma | 15 +- packages/server/src/api/admin.ts | 118 ++++++- packages/server/src/api/client.ts | 4 +- packages/server/src/index.ts | 7 +- packages/server/src/{lib => jobs}/Jobs.ts | 13 +- packages/server/src/lib/Canvas.ts | 291 +++++++++++++----- packages/server/src/lib/Express.ts | 4 +- packages/server/src/lib/Logger.ts | 39 ++- packages/server/src/lib/Settings.ts | 9 +- packages/server/src/lib/SocketServer.ts | 30 +- packages/server/src/lib/redis.ts | 21 +- packages/server/src/lib/utils.ts | 16 + packages/server/src/models/User.ts | 4 +- packages/server/src/tools/canvas_cache.ts | 14 - packages/server/src/tools/start_job_worker.ts | 6 + 37 files changed, 1244 insertions(+), 240 deletions(-) create mode 100644 docker-start-worker.sh create mode 100644 packages/client/src/lib/canvasRenderer.ts create mode 100644 packages/client/src/lib/renderer.ts create mode 100644 packages/client/src/worker/render.worker.ts create mode 100644 packages/server/prisma/migrations/20240619231750_add_pixel_mod_flag/migration.sql create mode 100644 packages/server/prisma/migrations/20240620192400_is_top_pixel/migration.sql create mode 100644 packages/server/prisma/migrations/20240625192346_pixel_deleted_at/migration.sql rename packages/server/src/{lib => jobs}/Jobs.ts (64%) create mode 100644 packages/server/src/lib/utils.ts delete mode 100644 packages/server/src/tools/canvas_cache.ts create mode 100644 packages/server/src/tools/start_job_worker.ts diff --git a/Dockerfile b/Dockerfile index 0c9e0a5..490df03 100644 --- a/Dockerfile +++ b/Dockerfile @@ -70,7 +70,7 @@ RUN npm -w packages/server run build FROM base as run WORKDIR /home/node/app COPY --from=dep /home/node/app/ ./ -COPY package*.json docker-start.sh .git ./ +COPY package*.json docker-start*.sh .git ./ # --- prepare lib --- diff --git a/docker-compose.yml b/docker-compose.yml index 91e7b34..27ff8ee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,6 +19,17 @@ services: condition: service_healthy postgres: condition: service_healthy + worker: + image: sc07/canvas + build: . + environment: + - REDIS_HOST=redis://redis + - DATABASE_URL=postgres://postgres@postgres/canvas + env_file: + - .env.local + depends_on: + - canvas + command: ./docker-start-worker.sh redis: restart: always image: redis:7-alpine diff --git a/docker-start-worker.sh b/docker-start-worker.sh new file mode 100644 index 0000000..cf4853f --- /dev/null +++ b/docker-start-worker.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +npm -w packages/server run tool start_job_worker \ No newline at end of file diff --git a/packages/client/src/components/CanvasWrapper.tsx b/packages/client/src/components/CanvasWrapper.tsx index e869fca..9bed88c 100644 --- a/packages/client/src/components/CanvasWrapper.tsx +++ b/packages/client/src/components/CanvasWrapper.tsx @@ -1,4 +1,4 @@ -import { useCallback, useContext, useEffect, useRef } from "react"; +import { useCallback, useContext, useEffect, useRef, useState } from "react"; import { Canvas } from "../lib/canvas"; import { useAppContext } from "../contexts/AppContext"; import { PanZoomWrapper } from "@sc07-canvas/lib/src/renderer"; @@ -23,29 +23,56 @@ export const CanvasWrapper = () => { {config &&