Skip to content

Commit

Permalink
Feat/rs lint (#454)
Browse files Browse the repository at this point in the history
* feat: Lint Matrix

* docs: Update FAQs
  • Loading branch information
rrr523 authored Dec 23, 2023
1 parent d4c1e89 commit a95f377
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-cycles-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/reed-solomon': patch
---

feat: Lint Matrix method
14 changes: 14 additions & 0 deletions doc-site/docs/FAQs/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"position": 9,
"label": "FAQs",
"collapsible": true,
"collapsed": false,
"className": "red",
"link": {
"type": "generated-index",
"title": "FAQs"
},
"customProps": {
"description": ""
}
}
106 changes: 106 additions & 0 deletions doc-site/docs/FAQs/caclute-checksum-is-slow.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
id: checksums
title: cacluting checksum is slow
order: 1
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

We're using [reed-solomon](https://github.com/bnb-chain/greenfield-js-sdk/tree/main/packages/reed-solomon).

It is a lightweight implementation of [klauspost/reedsolomon](https://github.com/klauspost/reedsolomon), just to be compatible with [greenfield-common](https://github.com/bnb-chain/greenfield-common/blob/master/go/hash/hash.go).

As we all know, JavaScript is not good at this kind of intensive computing. In fact, the results we tested on local Nodejs were about 10 times slower than Go.

But to be able to use it in the Javascript, we created [reed-solomon](https://github.com/bnb-chain/greenfield-js-sdk/tree/main/packages/reed-solomon), which is the core library.

## Benchmarking

If not counting big files (how big is depending on the user's device), here are the [results](https://github.com/bnb-chain/greenfield-js-sdk/blob/main/packages/reed-solomon/benchmark.md) of our tests on the Apple M2.

Note, you don't have to have webworker or worker_threads on to get faster performance. Because running worker also has performance loss.

When calculating small files, using core is faster than using worker.

## Usage

You can use core lib directly in the browser and Nodejs, or you can use us to run on the worker(browser use [webworker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers), Nodejs use [worker_threads]((https://nodejs.org/api/worker_threads.html))).

<Tabs groupId="example">

<TabItem value="browser" label="Browser">

When you are developing Greenfield and need to create objects, if you are sure that the files are small (maybe less than 5m or 10m), you can directly use the ESM solution:

```javascript title="core lib"
import {ReedSolomon} from '@bnb-chain/reed-solomon'

const rs = new RS.ReedSolomon();
const res = rs.encode(new Uint8Array(fileBuffer))
```

If the file is larger, this method may cause the page to freeze when calculating. In this case, we recommend using the worker mode:

```html title="web worker"
<!-- prefetch js -->
<link rel="prefetch" href="https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js" />
<link rel="prefetch" href="https://unpkg.com/@bnb-chain/reed-solomon/dist/utils.aio.js" />
<script src="https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js"></script>
<script>
const rs = new WebAdapter.WebAdapterReedSolomon()
// will create 6 webworker
rs.initWorkers({
workerNum: 6,
injectWorker,
})
document.getElementById('worker-btn').onclick = async function() {
const selectFile = fileInput.files[0];
const arrBuffer = await selectFile.arrayBuffer()
if (!arrBuffer) alert('no file selected');
const sourceData = new Uint8Array(arrBuffer)
const res = await rs.encodeInWorker(sourceData)
}
// inject worker
function injectWorker() {
// or download this file and put it to your CDN server
importScripts('https://unpkg.com/@bnb-chain/reed-solomon/dist/web.adapter.aio.js');
importScripts('https://unpkg.com/@bnb-chain/reed-solomon/dist/utils.aio.js');
const rs = new WebAdapter.WebAdapterReedSolomon();
onmessage = function (event) {
const { index, chunk } = event.data;
const encodeShard = rs.getEncodeShard(chunk, index)
postMessage(encodeShard);
};
}
</script>
```

</TabItem>
<TabItem value="nodejs" label="Nodejs">

Nodejs can also be used in two ways, directly with core library, or with [worker_threads](https://nodejs.org/api/worker_threads.html) (when calculating large files).

```js title="core lib"
const { ReedSolomon } = require('@bnb-chain/reed-solomon')

const rs = new ReedSolomon();
const res = await rs.encode(Uint8Array.from(fileBuffer));
```

```js title="worker_threads"
const { NodeAdapterReedSolomon } = require('@bnb-chain/reed-solomon/node.adapter');

const fileBuffer = fs.readFileSync('./output_file');

const rs = new NodeAdapterReedSolomon();
const res = await rs.encodeInWorker(__filename, Uint8Array.from(fileBuffer))
```

</TabItem>
</Tabs>

14 changes: 14 additions & 0 deletions doc-site/docs/constants/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"position": 4,
"label": "constants",
"collapsible": true,
"collapsed": false,
"className": "red",
"link": {
"type": "generated-index",
"title": "Constants"
},
"customProps": {
"description": ""
}
}
6 changes: 0 additions & 6 deletions doc-site/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ const config = {
src: 'img/logo.svg',
},
items: [
// {
// type: 'docSidebar',
// sidebarId: 'GettingStartSidebar',
// position: 'left',
// label: 'Getting Started',
// },
{
href: 'https://github.com/bnb-chain/greenfield-js-sdk',
label: 'GitHub',
Expand Down
2 changes: 1 addition & 1 deletion packages/reed-solomon/src/galois.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const galExp = function (a, n) {
}

const logA = logTable[a];
const logResult = Math.floor(logA * n);
let logResult = Math.floor(logA * n);
while (logResult >= 255) {
logResult -= 255;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/reed-solomon/src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function gaussianElimination(m) {
// }
// Scale to 1.
if (m[r][r] !== 1) {
let scale = galDivide(1, m[r][r]);
const scale = galDivide(1, m[r][r]);
for (let c = 0; c < columns; c++) {
m[r][c] = galMultiply(m[r][c], scale);
}
Expand All @@ -149,7 +149,7 @@ function gaussianElimination(m) {
// both exclusive or in the Galois field.)
for (let rowBelow = r + 1; rowBelow < rows; rowBelow++) {
if (m[rowBelow][r] !== 0) {
let scale = m[rowBelow][r];
const scale = m[rowBelow][r];
for (let c = 0; c < columns; c++) {
m[rowBelow][c] ^= galMultiply(scale, m[r][c]);
}
Expand All @@ -161,7 +161,7 @@ function gaussianElimination(m) {
for (let d = 0; d < rows; d++) {
for (let rowAbove = 0; rowAbove < d; rowAbove++) {
if (m[rowAbove][d] !== 0) {
let scale = m[rowAbove][d];
const scale = m[rowAbove][d];
for (let c = 0; c < columns; c++) {
m[rowAbove][c] ^= galMultiply(scale, m[d][c]);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/reed-solomon/src/node.adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class NodeAdapterReedSolomon extends ReedSolomon {
return new Promise((resolve, reject) => {
if (isMainThread) {
// RES is `encodeShards` Array
let RES = [];
const RES = [];
const chunkList = splitPrice(sourceData, this.segmentSize);
const threads = new Set();

Expand All @@ -21,7 +21,7 @@ export class NodeAdapterReedSolomon extends ReedSolomon {
threads.add(worker);
}

for (let w of threads) {
for (const w of threads) {
w.on('error', (err) => {
throw err;
});
Expand Down

0 comments on commit a95f377

Please sign in to comment.