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

DO NOT MERGE - testing basic wasm rayon #59

Closed
wants to merge 5 commits into from
Closed
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
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()
}
5 changes: 4 additions & 1 deletion wasm/prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ name = "tlsn-extension-rs"
rust-version = "1.56"
version = "0.1.0"

[patch.crates-io]
yamux = { git = "https://github.com/tlsnotary/rust-yamux", rev = "d8bd97c" }

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

Expand All @@ -32,7 +35,7 @@ pin-project-lite = "0.2.4"
http-body-util = "0.1"
hyper = { version = "1.1", features = ["client", "http1"] }
hyper-util = { version = "0.1", features = ["http1"] }
tracing-subscriber = { version = "0.3", features = ["time","env-filter"] }
tracing-subscriber = { version = "0.3", features = ["time", "env-filter"] }
tracing-web = "0.1.2"

ring = { version = "0.17", features = ["wasm32_unknown_unknown_js"] }
Expand Down