Skip to content

Fix O(n^2) slow fd_write#95

Merged
bjorn3 merged 2 commits intobjorn3:mainfrom
haskell-wasm:fix-slow-fd-write
Jan 19, 2026
Merged

Fix O(n^2) slow fd_write#95
bjorn3 merged 2 commits intobjorn3:mainfrom
haskell-wasm:fix-slow-fd-write

Conversation

@TerrorJack
Copy link
Contributor

Closes #93. Note that I've also taken some time to upgrade all the outdated dependencies and fix the code accordingly, if you wouldn't mind.

@bjorn3
Copy link
Owner

bjorn3 commented Oct 18, 2025

Please move the dependency updates to a separate commit and skip the version bump commit.

@bjorn3
Copy link
Owner

bjorn3 commented Oct 18, 2025

I meant separate PR, not commit. My bad.

@TerrorJack
Copy link
Contributor Author

At the very least, typescript must be bumped, otherwise the baseline 2024 arraybuffer grow type signature as well as the Uint8Array<ArrayBuffer> type would not be available at all. So it's not really feasible to spin out only the changes here and make it typecheck without any bumps.

@bjorn3
Copy link
Owner

bjorn3 commented Oct 18, 2025

The dependency bump PR could be merged first.

Copy link
Owner

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with the following patch (can't push to your branch) Apologies for the long wait.

Author: bjorn3 <17426603+bjorn3@users.noreply.github.com>
Date:   Sun Jan 18 22:48:44 2026 +0100

    Minor style changes

diff --git a/src/fs_mem.ts b/src/fs_mem.ts
index 4399ee6..0551259 100644
--- a/src/fs_mem.ts
+++ b/src/fs_mem.ts
@@ -24,9 +24,9 @@ function dataResize(data: Uint8Array, newDataSize: number): Uint8Array {
   // of old data onto it
   if (data.byteLength > newDataSize) {
     const newBuffer = new ArrayBuffer(newDataSize, {
-        maxByteLength: newDataSize,
-      }),
-      newData = new Uint8Array(newBuffer);
+      maxByteLength: newDataSize,
+    });
+    const newData = new Uint8Array(newBuffer);
     newData.set(new Uint8Array(data.buffer, 0, newDataSize));
     return newData;
   }
@@ -35,9 +35,9 @@ function dataResize(data: Uint8Array, newDataSize: number): Uint8Array {
   // growth of maxByteLength, to avoid O(n^2) overhead of repeatedly
   // concatenating buffers when doing a lot of small writes at the end
   const newBuffer = new ArrayBuffer(newDataSize, {
-      maxByteLength: Math.max(newDataSize, data.buffer.maxByteLength * 2),
-    }),
-    newData = new Uint8Array(newBuffer);
+    maxByteLength: Math.max(newDataSize, data.buffer.maxByteLength * 2),
+  });
+  const newData = new Uint8Array(newBuffer);
   newData.set(data);
   return newData;
 }

@TerrorJack
Copy link
Contributor Author

Apologies for the long wait.

oh never mind! we have our own fork https://github.com/haskell-wasm/browser_wasi_shim anyway, with the intention of gradually upstreaming our fixes, and upstream reviewer should review patches at their own pace without any pressure. again, thank you for this wonderful project :)

Copy link
Owner

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bjorn3 bjorn3 merged commit 85b16d0 into bjorn3:main Jan 19, 2026
1 check passed
@bjorn3
Copy link
Owner

bjorn3 commented Jan 19, 2026

Are there more changes you did like to make? If not I can do a release later today.

@TerrorJack TerrorJack deleted the fix-slow-fd-write branch January 19, 2026 16:31
@TerrorJack
Copy link
Contributor Author

yes, there are some other changes that i'd like to open PRs tomorrow or so, if you're not in a hurry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fd_write is slow because of repeatedly concatenating buffers

3 participants