Giter Site home page Giter Site logo

Improve speed about objectbuffer HOT 4 OPEN

bnaya avatar bnaya commented on July 18, 2024
Improve speed

from objectbuffer.

Comments (4)

Bnaya avatar Bnaya commented on July 18, 2024

Hey Vladislav!
Thanks for your feedback!

Can you share some code?
I want to try and figure if there's any specific slowdown cause in your use case
Currently the object is implement using simple linked list, and there's inherit overhead of working with arraybuffer that way, but it can be improved

from objectbuffer.

darky avatar darky commented on July 18, 2024
import * as objectbufferModule from "@bnaya/objectbuffer";
import { job, start, stop } from "microjob";

(async () => {
  await start({ maxWorkers: 1 });
  // cache require
  await job(() => {
    require("@bnaya/objectbuffer");
  });

  console.time("buffer");
  const externalArgs: objectbufferModule.ExternalArgs = {
    textEncoder: new TextEncoder(),
    textDecoder: new TextDecoder(),
    arrayAdditionalAllocation: 20,
    minimumStringAllocation: 0
  };

  const myObjectBufferOnMainThread = objectbufferModule.createObjectBuffer(
    externalArgs,
    1024,
    {
      some: {
        nested: [
          {
            thing: "im the value to follow"
          }
        ]
      }
    },
    { useSharedArrayBuffer: true }
  );

  const shared = objectbufferModule.getUnderlyingArrayBuffer(
    myObjectBufferOnMainThread
  );

  await job(
    async () => {
      const objectbufferModule = require("@bnaya/objectbuffer");
      const myObjectBufferOnMainThread = objectbufferModule.loadObjectBuffer(
        {
          textEncoder: new TextEncoder(),
          textDecoder: new TextDecoder(),
          arrayAdditionalAllocation: 20,
          minimumStringAllocation: 0
        },
        shared
      );
      myObjectBufferOnMainThread.some.nested.push(13);
    },
    { shared }
  );
  console.log(myObjectBufferOnMainThread.some.nested[1]);
  console.timeEnd("buffer");

  console.time("simple");
  let data = {
    some: {
      nested: [
        {
          thing: "im the value to follow"
        }
      ]
    }
  };
  const newData = await job(
    async data => {
      data.some.nested.push(13);
      return data;
    },
    { data }
  );
  console.log(newData.some.nested[1]);
  console.timeEnd("simple");

  // stop();
})();

Please, install microjob from here: darky/microjob#a00474ef0e833c3d09435f080b6ff9aae9b342f8
Shared memory support not merged at this moment

from objectbuffer.

Bnaya avatar Bnaya commented on July 18, 2024

For small objects, the speed of serialize + deserialize is expected to always be faster than using that library.
The overhead of writing all the logic in javascript, has it price.

But for example, if you want to sort big array on a worker,
that serialize + deserialize will be expensive from CPU and memory wise.

A side note: you are not using locks, you might get data races.

from objectbuffer.

darky avatar darky commented on July 18, 2024

Ok, thanks! I try to benchmark on larger data later :)

from objectbuffer.

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.