perf: fix O(n²) arrayBufferToBase64 + efficiency audit report#1
Closed
devin-ai-integration[bot] wants to merge 1 commit into
Closed
perf: fix O(n²) arrayBufferToBase64 + efficiency audit report#1devin-ai-integration[bot] wants to merge 1 commit into
devin-ai-integration[bot] wants to merge 1 commit into
Conversation
…ayBufferToBase64 The previous implementation built a binary string by appending one character at a time (binary += String.fromCharCode(bytes[i])), which is O(n²) for large buffers because JavaScript strings are immutable and each += creates a new string copy. The new implementation processes the buffer in 8 KB chunks using String.fromCharCode.apply() and joins them once at the end, reducing the operation to O(n) and significantly improving performance for image payloads (which can be up to 2 MB). Co-Authored-By: 396217818@qq.com <396217818@qq.com>
Deploying text2image-frontend-git with
|
| Latest commit: |
5e3710d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://99404684.text2image-frontend-git.pages.dev |
| Branch Preview URL: | https://devin-1771416356-perf-fix-ba.text2image-frontend-git.pages.dev |
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Author
|
Closing due to inactivity for more than 7 days. Configure here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
perf: fix O(n²) string concatenation in arrayBufferToBase64
Summary
Replaces the byte-by-byte string concatenation in
arrayBufferToBase64(backend/utils/base64.js) with a chunked approach. The old code appended one character at a time viabinary += String.fromCharCode(bytes[i]), which is O(n²) because JS strings are immutable and each+=allocates a new string. For a 2 MB image buffer, this means ~2 million intermediate string copies.The new implementation processes the buffer in 8 KB chunks using
String.fromCharCode.apply(null, slice), collects the chunks in an array, and joins once at the end — making it O(n).This function is called on every image generation request, so the improvement matters at scale.
Efficiency audit (other spots identified but not fixed)
During the audit, these additional inefficiencies were noted for future work:
backend/utils/response.js(lines 14–18, 30–34, 46–50, 78–82)+=concatenation — runs 3–4× per requestbackend/index.js(lines 40–49)+=loop on every incoming requestbackend/utils/cors.jsparseAllowedOriginsALLOWED_ORIGINSenv string on every request; result is constant per worker lifetimebackend/services/generation.jsisPremiumModelArray.includes()on a static list; aSetwould give O(1) lookupbackend/auth.jsresolvePBKDF2Digest(line 158)new Set(["sha256","sha384","sha512"])on every call; could be a module-level constantReview & Testing Checklist for Human
arrayBufferToBase64against a sample ArrayBuffer and confirm the base64 strings match — there are no unit tests covering this functionString.fromCharCode.apply: the 8192 chunk size is well within safeapplyargument limits for V8/Cloudflare Workers, but confirm this holds for the target runtimeNotes