Skip to content

Optimize Bytecode Method

Before loading your bytecode into the VM, it is highly recommended to perform optimization. This ensures that the instructions are streamlined, reducing overhead and maximizing the execution speed of your program.

Using TypeScript

For TypeScript, you can process your bytecode arrays using the optimization utility. This step is essential to ensure the VM handles the most efficient instruction set.

ts
const raw = [
  ['push', 5],
  ['val', 'x'],
  ['set', 'x'],
];
const optimized = tools.optimizeBytecode(raw);
console.log(optimized);

Using Rust

In Rust, you can optimize your bytecode either as raw strings or by leveraging serde_json. This pre-processing step is critical for maintaining high-performance execution within the VM.

rust
let raw = r#"[
  ["push", 5],
  ["val", "x"],
  ["set", "x"]
]"#;
let optimized = tools.optimize_bytecode(raw);
println!(optimized.clone());
rust
let raw = serde_json::json!([
  ["push", 5],
  ["val", "x"],
  ["set", "x"]
]);
let optimized = tools.optimize_bytecode(raw);
println!(optimized.clone());

INFO

Capability Required: no specific capability

TIP

Optimization is non-destructive and highly recommended for complex programs. Consistently optimizing your bytecode helps in identifying potential instruction bottlenecks before the execution phase.

Released under the Apache-2.0 License.