Git Product home page Git Product logo

Comments (3)

kleisauke avatar kleisauke commented on September 27, 2024 1

It looks like you initialize wasm-vips on every createMultiPageImage() call. I would initialize wasm-vips only once and reuse its instance. For example:

Details
--- a/handle-multi-image.mjs
+++ b/handle-multi-image.mjs
@@ -12,6 +12,10 @@ function log(msg) {
   console.log(msg, "\n" + JSON.stringify(obj, null, 4) + "\n");
 }
 
+const vips = await Vips();
+vips.concurrency(1);
+vips.Cache.max(0);
+
 async function createMultiPageImage(buffer, op, ...params) {
   const buffers = [];
   const { pages: pageCount } = await sharp(buffer, { pages: -1 }).metadata();
@@ -31,10 +35,6 @@ async function createMultiPageImage(buffer, op, ...params) {
     }
   }
 
-  const vips = await Vips();
-  vips.concurrency(1);
-  vips.Cache.max(0);
-
   const vipsPages = pages.map((page) => vips.Image.newFromBuffer(page, "", { access: vips.Access.sequential }));
   const vipsImage = vips.Image.arrayjoin(vipsPages, { across: 1 });
 
@@ -44,7 +44,6 @@ async function createMultiPageImage(buffer, op, ...params) {
 
   vipsPages.map((vipsPage) => vipsPage.delete());
   vipsImage.delete();
-  vips.shutdown();
 
   return Buffer.from(vipsBuffer);
 }

With that, I see:

flop 
{
    "rss": 123,
    "heapTotal": 31,
    "heapUsed": 13,
    "external": 7,
    "arrayBuffers": 5
}

negate 
{
    "rss": 125,
    "heapTotal": 31,
    "heapUsed": 13,
    "external": 7,
    "arrayBuffers": 5
}

flip 
{
    "rss": 242,
    "heapTotal": 31,
    "heapUsed": 14,
    "external": 9,
    "arrayBuffers": 7
}

rotate 
{
    "rss": 263,
    "heapTotal": 31,
    "heapUsed": 14,
    "external": 14,
    "arrayBuffers": 11
}

trim 
{
    "rss": 275,
    "heapTotal": 31,
    "heapUsed": 14,
    "external": 19,
    "arrayBuffers": 16
}

Which is probably reasonable for image processing in WebAssembly.

from wasm-vips.

Akash-Panigrahi avatar Akash-Panigrahi commented on September 27, 2024 1

Just implemented the above logic and working flawlessly. Apart from the initial memory jump, all other subsequent vips calls are within constraint. Thank you @kleisauke for the fix and also for the amazing package. πŸ™‡πŸΎβ€β™‚οΈ

from wasm-vips.

Akash-Panigrahi avatar Akash-Panigrahi commented on September 27, 2024

This definitely looks promising. Currently busy with work, but will definitely check this out and revert back. Thank you for checking the issue. πŸ™πŸΎ

from wasm-vips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.