-
Notifications
You must be signed in to change notification settings - Fork 471
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
[Feature] SDK Refactor + Enhanced Multithreading Support #790
base: mainnet
Are you sure you want to change the base?
Conversation
The old code created various variables ( The new code instead bundles those variables inside of a Bundling variables inside of a struct is a very common Rust idiom, it has many advantages:
The The struct is created inside of the This pattern of creating temporary wrapper structs is very common and idiomatic in Rust. Structs are not classes, structs in Rust serve many different purposes, and creating wrapper structs is very common and completely normal. For example, let state = vec![...];
let state = state.into_iter();
let state = state.map(|x| ...);
let state = state.filter(|x| ...);
let state = state.collect(); The https://doc.rust-lang.org/std/vec/struct.IntoIter.html The And then when you call the https://doc.rust-lang.org/std/iter/struct.Map.html The And then the https://doc.rust-lang.org/std/iter/struct.Filter.html The Lastly the All of these structs are temporary, they exist only to describe the business logic, they aren't intended as a long-term storage of state. They are only used locally inside of a method. And when you are finished with them, the structs are simply thrown away. The structs in this pull request behave in the same way: let state = ProgramState::new(program, imports).await?;
let (state, deploy) = state.deploy().await?;
deploy.check_fee(fee_microcredits)?;
let (state, fee) = state
.execute_fee(
deploy.execution_id()?,
url,
private_key.clone(),
fee_microcredits,
fee_record,
fee_proving_key,
fee_verifying_key,
)
.await?;
state.deploy_transaction(deploy, fee).await The For example, the This is the same as how the iterator All of these structs are temporary, and they are thrown away when the Why do the That means that all of the This pattern is very common in functional programming languages, because functional programming languages cannot mutate, so they must always return new state. It is also common in Rust, for example with the builder pattern. Returning And the reason why the If the
The end result is that the new code is exactly the same as the old code, except:
Perhaps the name |
Replaces the
process_inputs
,execute_program
, andexecute_fee
macros with methods.This removes a lot of code duplication and makes the code simpler.
Adds a new
thread_pool::spawn
function which allows for running anySend
+'static
code on theRayon threadpool.
All of the various methods (
execute_function_offline
,execute
,deploy
,split
, etc.) arenow automatically run on the Rayon threadpool, which means everything runs in parallel.
This means the user no longer needs to use multiple Workers for paralellism, they can
run all of their JS code within a single Worker.