Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native Buffer whenever available #1032

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open

Conversation

appurva21
Copy link
Member

@appurva21 appurva21 commented Aug 27, 2024

Context and fix:

Use the native Buffer when available. This is done to get better performance and system stability when operating on large buffers

For example, when converting a large buffer to a string, the native Buffer is much faster than the isomorphic buffer. It supports toString() operation on buffers of size up to 512MB, while the isomorphic buffer errors out after 100MB.

Additional Changes

  • Added support to provide different package resolvers for node and browser while creating the bundle.
  • Now, buffer not does not get packaged for node environment, reducing compressed bundle size by ~26KB.

Performance benchmarking results for toString() (Node):

NOTE : On develop + Node v20 and below, the process crashes for size greater than ~120MB. For v22 we get "invalid array length" error (maked by undefined in the results below)

Script for benchmarking
const Sandbox = require('./lib');
const inputs = [1, 10, 100, 200, 300, 400, 500, 600]; // In MB
const results = [];

const run = async (size) => {
    return new Promise((resolve, reject) => {
        Sandbox.createContext(function (err, ctx) {
            if (err) { return reject(err); }

            let time;

            ctx.on('console', function (_, __, ...args) {
                time = Number(args[0]);
            });

            ctx.execute(`
                const buf = Buffer.alloc(${size * 1024 * 1024}, 'a');

                performance.mark('start');
                buf.toString();
                performance.mark('end');

                const duration = performance.measure('toString', 'start', 'end').duration.toFixed('2');

                // send out the data
                console.log(duration);
            `, () => {
                ctx.dispose();
                resolve(time);
            });
        });
    });

}

(async () => {
    for (let i = 0; i < inputs.length; i++) {
        const time = await run(inputs[i]);
        results.push({ 'Size(MB)': inputs[i], 'Time(ms)': time });
    }
    console.table(results);
})();

Node v16

develop

image

This branch

image

Node v18

develop

image

This branch

image

Node v20

develop

image

This branch

image

Node v22

develop

image

This branch

image

Copy link

codecov bot commented Aug 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@8d5424d). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #1032   +/-   ##
==========================================
  Coverage           ?   52.77%           
==========================================
  Files              ?       12           
  Lines              ?      612           
  Branches           ?      146           
==========================================
  Hits               ?      323           
  Misses             ?      249           
  Partials           ?       40           
Flag Coverage Δ
unit 52.77% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@appurva21 appurva21 force-pushed the feature/fix-buffer branch 4 times, most recently from 1b10a07 to bff7c31 Compare August 29, 2024 09:18
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.

1 participant