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

WIP: Extract msgpack ReScript implementation as a seperated package #12

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmarks/array.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { Benchmark } = require('benchmark');

const n = 10000;

function Person(name, age) {
this.name = name;
this.age = age;
}

new Benchmark.Suite()
.add('init by constructor', () => {
const arr = new Array(n);
for (let i = 0; i < n; i++) {
arr[i] = new Person(i.toString(), i);
}
})
.add('init by apply', () => {
const arr = Array(n);
for (let i = 0; i < n; i++) {
arr[i] = new Person(i.toString(), i);
}
})
.add('init by literal + push', () => {
const arr = [];
for (let i = 0; i < n; i++) {
arr.push(new Person(i.toString(), i));
}
})
.add('init by literal + assign', () => {
const arr = [];
for (let i = 0; i < n; i++) {
arr[i] = new Person(i.toString(), i);
}
})
.on('cycle', event => {
console.log(event.target.toString());
})
.run();
22 changes: 11 additions & 11 deletions benchmarks/utf8-decode.js → benchmarks/utf8-decode.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ const { Benchmark } = require('benchmark');

const textDecoder = new TextDecoder();

const utf8DecodeJs = (function (bytes) {
const utf8DecodeJs = (bytes) => {
let offset = 0;
const end = bytes.length;
let end = bytes.length;

const units = [];
let units = [];
let result = "";
while (offset < end) {
const byte1 = bytes[offset++];
let byte1 = bytes[offset++];
if ((byte1 & 0x80) === 0) {
// 1 byte
units.push(byte1);
} else if ((byte1 & 0xe0) === 0xc0) {
// 2 bytes
const byte2 = bytes[offset++] & 0x3f;
let byte2 = bytes[offset++] & 0x3f;
units.push(((byte1 & 0x1f) << 6) | byte2);
} else if ((byte1 & 0xf0) === 0xe0) {
// 3 bytes
const byte2 = bytes[offset++] & 0x3f;
const byte3 = bytes[offset++] & 0x3f;
let byte2 = bytes[offset++] & 0x3f;
let byte3 = bytes[offset++] & 0x3f;
units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
} else if ((byte1 & 0xf8) === 0xf0) {
// 4 bytes
const byte2 = bytes[offset++] & 0x3f;
const byte3 = bytes[offset++] & 0x3f;
const byte4 = bytes[offset++] & 0x3f;
let byte2 = bytes[offset++] & 0x3f;
let byte3 = bytes[offset++] & 0x3f;
let byte4 = bytes[offset++] & 0x3f;
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
if (unit > 0xffff) {
unit -= 0x10000;
Expand All @@ -49,7 +49,7 @@ const utf8DecodeJs = (function (bytes) {
}

return result;
});
};

const textEncoded = new Uint8Array([0xac, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21]);

Expand Down
66 changes: 66 additions & 0 deletions benchmarks/utf8-encode.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { Benchmark } = require('benchmark');

const textEncoder = new TextEncoder();

const utf8EncodeJs = (str) => {
let strLength = str.length;
let output = [];
let offset = 0;
let pos = 0;
while (pos < strLength) {
let value = str.charCodeAt(pos++);

if ((value & 0xffffff80) === 0) {
// 1-byte
output[offset++] = value;
continue;
} else if ((value & 0xfffff800) === 0) {
// 2-bytes
output[offset++] = ((value >> 6) & 0x1f) | 0xc0;
} else {
// handle surrogate pair
if (value >= 0xd800 && value <= 0xdbff) {
// high surrogate
if (pos < strLength) {
let extra = str.charCodeAt(pos);
if ((extra & 0xfc00) === 0xdc00) {
++pos;
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
}
}
}

if ((value & 0xffff0000) === 0) {
// 3-byte
output[offset++] = ((value >> 12) & 0x0f) | 0xe0;
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
} else {
// 4-byte
output[offset++] = ((value >> 18) & 0x07) | 0xf0;
output[offset++] = ((value >> 12) & 0x3f) | 0x80;
output[offset++] = ((value >> 6) & 0x3f) | 0x80;
}
}

output[offset++] = (value & 0x3f) | 0x80;
}

return new Uint8Array(output);
}

const text = '안녕, 세상!';

console.log(textEncoder.encode(text));
console.log(utf8EncodeJs(text));

new Benchmark.Suite()
.add('TextEncoder', () => {
textEncoder.encode(text);
})
.add('utf8EncodeJs', () => {
utf8EncodeJs(text);
})
.on('cycle', event => {
console.log(event.target.toString());
})
.run();
4 changes: 4 additions & 0 deletions packages/msgpack/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
.merlin
.bsb.lock
*.bs.js

/lib/
21 changes: 21 additions & 0 deletions packages/msgpack/bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@urlpack/msgpack",
"sources": [
{
"dir": "src",
"subdirs": true
}
],
"package-specs": [
{
"module": "es6",
"in-source": true
}
],
"suffix": ".bs.js",
"bs-dependencies": [
"rescript-msgpack"
],
"bs-dev-dependencies": [
]
}
13 changes: 9 additions & 4 deletions packages/msgpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,25 @@
"main": "./lib/index.mjs"
},
"files": [
"src",
"lib"
"src/**/*.ts",
"src/*.res",
"lib/*.mjs",
"lib/*.cjs",
"lib/*.d.ts",
"lib/*.map"
],
"scripts": {
"prepack": "yarn build",
"build": "nanobundle build --standalone",
"build": "rescript build && nanobundle build --standalone",
"test": "uvu -r tsm",
"test:watch": "yarn test || true && watchlist src tests -- yarn test"
},
"devDependencies": {
"@rescript/std": "^9.1.3",
"concurrently": "^6.5.1",
"esbuild": "^0.14.9",
"nanobundle": "^0.0.21",
"rescript": "^9.1.4",
"rescript-msgpack": "workspace:^0.1.0",
"tsm": "^2.2.1",
"typescript": "^4.5.4",
"uvu": "^0.5.2",
Expand Down
Loading