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

feat(v8/scripting): swap from msgpack-lite to msgpackr #3018

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AvarianKnight
Copy link
Contributor

Goal of this PR

Swap JS to msgpackr to allow serializing classes & other js builtin objects (i.e. Map, Set, Error, etc) and also increase performance.

How is this PR achieving the goal

Swap out the custom version of msgpack-lite to use msgpackr

This PR applies to the following area(s)

ScRT: JS

Successfully tested on

Game builds: N/A

Platforms: Windows, Linux

Checklist

  • Code compiles and has been tested successfully.
  • Code explains itself well and/or is documented.
  • My commit message explains what the changes do and what they are for.
  • No extra compilation warnings are added by these changes.

Fixes issues

Supersedes PR #2931

@github-actions github-actions bot added ScRT: JS Issues/PRs related to the JavaScript scripting runtime triage Needs a preliminary assessment to determine the urgency and required action labels Dec 19, 2024
Comment on lines 846 to 847
throw new Error('Unknown extension type ' + type)

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should probably just handle the conversions here anyways, since sending vectors to/from scrts is currently very hacky and I doubt may people do it currently.

Copy link
Contributor

@thelindat thelindat Dec 19, 2024

Choose a reason for hiding this comment

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

Created an array containing 100 objects and another with 1000 objects. Performed 1000 iterations.

msgpack-lite

Running benchmark for packing...
Pack - Min: 0.0568 ms, Max: 1.1691 ms, Avg: 0.080365 ms
Running benchmark for unpacking...
Unpack - Min: 0.0482 ms, Max: 1.1444 ms, Avg: 0.061173 ms

Running benchmark for packing...
Pack - Min: 5.2618 ms, Max: 13.8522 ms, Avg: 5.99034 ms
Running benchmark for unpacking...
Unpack - Min: 5.0637 ms, Max: 20.6582 ms, Avg: 5.366889 ms

msgpackr

Running benchmark for packing...
Pack - Min: 0.0185 ms, Max: 4.2132 ms, Avg: 0.03549 ms
Running benchmark for unpacking...
Unpack - Min: 0.035 ms, Max: 1.9161 ms, Avg: 0.059167 ms

Running benchmark for packing...
Pack - Min: 1.8215 ms, Max: 10.3579 ms, Avg: 1.995244 ms
Running benchmark for unpacking...
Unpack - Min: 3.4534 ms, Max: 18.5718 ms, Avg: 3.717805 ms

Edit

const curtime = process.hrtime.bigint;
const results = [];

function runBenchmark(iterations, title, func) {
  const start = curtime();

  for (let i = 0; i < iterations; i++) {
    func();
  }

  results.push([Number(curtime() - start) / iterations, title]);
}

function showResults(iterations) {
  console.log(`Average results from ${iterations} iterations (ms)`);
  results.sort((a, b) => a[1] < b[1]);

  for (let i = 0; i < results.length; i++) {
    const result = results[i];
    console.log(`#${i + 1} - ${(result[0] / 1e6).toFixed(4)}\t(${result[1]})`);
  }

  const copy = [...results];
  results.length = 0;

  return copy;
}

async function benchmark(iterations, obj) {
  for (k in obj) {
    await new Promise((r) => setTimeout(r, 100));
    runBenchmark(iterations, k, obj[k]);
  }

  return showResults(iterations);
}

setTimeout(async () => {
  const data = new Array(100000);
  data.fill("avarian smells");

  const packed = pack(data);

  const fn = {
    Packing: () => pack(data),
    Unpacking: () => unpack(packed),
  };

  const result = await benchmark(100, fn);
}, 500);

msgpack-lite
Average results from 100 iterations (ms)
1 - 10.5856 (Packing)
2 - 26.7245 (Unpacking)

msgpackr
Average results from 100 iterations (ms)
1 - 3.9559 (Packing)
2 - 9.0236 (Unpacking)

For a ~62-66% increase in speed. For a larger array (1000000 elements) this was about ~45-53%.

@AvarianKnight
Copy link
Contributor Author

When testing this caused crashing for RedM due to how it's session manager works, I'll look into this when I have the time

@AvarianKnight AvarianKnight marked this pull request as draft January 21, 2025 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ScRT: JS Issues/PRs related to the JavaScript scripting runtime triage Needs a preliminary assessment to determine the urgency and required action
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants