Skip to content

Commit

Permalink
add basic rayon demo
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino committed Jun 27, 2024
1 parent c83d97e commit 795e1b2
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 33 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wasm/prover/pkg
wasm/prover/target
wasm/prover/Cargo.lock
wasm/**/pkg
wasm/**/target
wasm/**/Cargo.lock
wasm-pack.log
node_modules/
.idea/
Expand Down
2 changes: 1 addition & 1 deletion demo/react-ts-webpack/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function App(): ReactElement {

const onClick = useCallback(async () => {
setProcessing(true);
await set_logging_filter('info,tlsn_extension_rs=debug');
await set_logging_filter('debug,tlsn_extension_rs=debug');
const p = await prove('https://swapi.dev/api/people/1', {
method: 'GET',
maxTranscriptSize: 16384,
Expand Down
11 changes: 11 additions & 0 deletions demo/react-ts-webpack/basic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Comlink from 'comlink';

const TestSum: any = Comlink.wrap(
// @ts-ignore
new Worker(new URL('./worker.ts', import.meta.url)),
);

(async function () {
const inst = await new TestSum();
console.log(await inst.test());
})();
3 changes: 2 additions & 1 deletion demo/react-ts-webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"comlink": "^4.4.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-loader-spinner": "^6.1.6",
Expand All @@ -27,4 +28,4 @@
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
}
}
}
4 changes: 3 additions & 1 deletion demo/react-ts-webpack/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ var options = {
/ResizeObserver loop completed with undelivered notifications/,
],
mode: 'development',
target: 'web',
entry: {
app: path.join(__dirname, 'app.tsx'),
// app: path.join(__dirname, 'app.tsx'),
basic: path.join(__dirname, 'basic.tsx'),
},
output: {
filename: '[name].bundle.js',
Expand Down
18 changes: 18 additions & 0 deletions demo/react-ts-webpack/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Comlink from 'comlink';

import init, { sum, initThreadPool } from '../../wasm/basic-rayon/pkg';

class TestSum {
async test() {
console.log('init');
await init();
console.log('initThreadPool');
// @ts-ignore
await initThreadPool(navigator.hardwareConcurrency * 2);
console.log('sum');
const arr = Int32Array.from({ length: 100 }, (_, i) => i + 1);
return sum(arr);
}
}

Comlink.expose(TestSum);
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import TLSN from './tlsn';
import { DEFAULT_LOGGING_FILTER } from './tlsn';
import { Proof } from './types';

let _tlsn: TLSN;
import * as Comlink from 'comlink';

const TLSN: any = Comlink.wrap(
new Worker(new URL('./worker.ts', import.meta.url)),
);

let _tlsn: any;
const current_logging_filter = DEFAULT_LOGGING_FILTER;

async function getTLSN(logging_filter?: string): Promise<TLSN> {
async function getTLSN(logging_filter?: string): Promise<any> {
const logging_filter_changed =
logging_filter && logging_filter == current_logging_filter;

Expand Down
59 changes: 36 additions & 23 deletions src/tlsn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ export default class TLSN {
this.start();
}

async start() {
// console.log('start');
const numConcurrency = navigator.hardwareConcurrency;
// console.log('!@# navigator.hardwareConcurrency=', numConcurrency);
await init();
setup_tracing_web(this.logging_filter);
// const res = await init();
// console.log('!@# res.memory=', res.memory);
// 6422528 ~= 6.12 mb
// console.log('!@# res.memory.buffer.length=', res.memory.buffer.byteLength);
await initThreadPool(numConcurrency);
this.resolveStart();
async start(currencyOverride?: number) {
try {
console.log('start');
const numConcurrency =
typeof currencyOverride === 'number'
? currencyOverride
: navigator.hardwareConcurrency;
console.log('!@# navigator.hardwareConcurrency=', numConcurrency);
// await init();
const res = await init();
setup_tracing_web(this.logging_filter);

console.log('!@# res.memory=', res.memory);
// 6422528 ~= 6.12 mb
console.log(
'!@# res.memory.buffer.length=',
res.memory.buffer.byteLength,
);
await initThreadPool(numConcurrency);
this.resolveStart();
console.log('init thread pool success');
} catch (e) {
console.error(e);
throw e;
}
}

async waitForStart() {
Expand All @@ -61,11 +74,11 @@ export default class TLSN {
},
) {
await this.waitForStart();
// console.log('worker', url, {
// ...options,
// notaryUrl: options?.notaryUrl,
// websocketProxyUrl: options?.websocketProxyUrl,
// });
console.log('worker', url, {
...options,
notaryUrl: options?.notaryUrl,
websocketProxyUrl: options?.websocketProxyUrl,
});
const resProver = await prover(
url,
{
Expand All @@ -77,13 +90,13 @@ export default class TLSN {
options?.secretResps || [],
);
const resJSON = JSON.parse(resProver);
// console.log('!@# resProver,resJSON=', { resProver, resJSON });
// console.log('!@# resAfter.memory=', resJSON.memory);
console.log('!@# resProver,resJSON=', { resProver, resJSON });
console.log('!@# resAfter.memory=', resJSON.memory);
// 1105920000 ~= 1.03 gb
// console.log(
// '!@# resAfter.memory.buffer.length=',
// resJSON.memory?.buffer?.byteLength,
// );
console.log(
'!@# resAfter.memory.buffer.length=',
resJSON.memory?.buffer?.byteLength,
);

return resJSON;
}
Expand Down
2 changes: 1 addition & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Comlink from 'comlink';
import TLSN from './tlsn';

export default TLSN;
// export default TLSN;

Comlink.expose(TLSN);
16 changes: 16 additions & 0 deletions wasm/basic-rayon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
authors = ["The tlsn-extension Developers"]
description = "tlsn-js library for using TLSNotary in browsers"
edition = "2018"
license = "MIT OR Apache-2.0"
name = "tlsn-extension-rs"
rust-version = "1.56"
version = "0.1.0"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2"
rayon = "1.8"
wasm-bindgen-rayon = "1.2"
13 changes: 13 additions & 0 deletions wasm/basic-rayon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub use wasm_bindgen_rayon::init_thread_pool;

use wasm_bindgen::prelude::wasm_bindgen;

use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;



#[wasm_bindgen]
pub fn sum(numbers: &[i32]) -> i32 {
numbers.par_iter().sum()
}

0 comments on commit 795e1b2

Please sign in to comment.