Skip to content

Commit 9035ab0

Browse files
authored
Optimize raw Sierra deserialization (#109)
1 parent 8184d41 commit 9035ab0

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ cairo-lang-starknet-sierra-1_0_0 = { package = "universal-sierra-compiler-cairo-
1515
cairo-lang-sierra-to-casm = "2.12.3"
1616
cairo-lang-sierra = "2.12.3"
1717
cairo-lang-starknet-classes = "2.12.3"
18-
serde_json = "1.0.145"
18+
serde_core = "1"
19+
serde_json = "1"
1920
clap = "4.5.48"
2021
anyhow = "1.0.100"
2122
console = "0.16.1"

src/commands/compile_raw.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Context, Result};
1+
use anyhow::Result;
22
use cairo_lang_sierra::program::Program;
33
use cairo_lang_sierra_to_casm::compiler::{CairoProgramDebugInfo, SierraToCasmConfig};
44
use cairo_lang_sierra_to_casm::metadata::{calc_metadata, MetadataComputationConfig};
@@ -22,26 +22,19 @@ pub struct CompileRaw {
2222

2323
/// Compiles Sierra of the plain Cairo code.
2424
#[tracing::instrument(skip_all, level = "info")]
25-
pub fn compile(sierra_program: Value) -> Result<Value> {
26-
let span = trace_span!("deserialize_sierra");
27-
let sierra_program: Program = {
28-
let _g = span.enter();
29-
serde_json::from_value(sierra_program)
30-
.context("Unable to deserialize Sierra program. Make sure it is in a correct format")?
31-
};
32-
25+
pub fn compile(sierra_program: &Program) -> Result<Value> {
3326
let metadata_config = MetadataComputationConfig::default();
3427
let span = trace_span!("calc_metadata");
3528
let metadata = {
3629
let _g = span.enter();
37-
calc_metadata(&sierra_program, metadata_config)?
30+
calc_metadata(sierra_program, metadata_config)?
3831
};
3932

4033
let span = trace_span!("compile_sierra_to_casm");
4134
let cairo_program = {
4235
let _g = span.enter();
4336
cairo_lang_sierra_to_casm::compiler::compile(
44-
&sierra_program,
37+
sierra_program,
4538
&metadata,
4639
SierraToCasmConfig {
4740
gas_usage_check: true,

src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use anyhow::{Context, Error, Result};
2+
use cairo_lang_sierra::program::Program;
23
use clap::{Parser, Subcommand};
34
use console::style;
45
use serde_json::{to_writer, Value};
@@ -33,10 +34,9 @@ fn print_error_message(error: &Error) {
3334
}
3435

3536
#[tracing::instrument(skip_all, level = "info")]
36-
fn read_json(file_path: PathBuf) -> Result<Value> {
37+
fn read_json<T: for<'de> serde_core::de::Deserialize<'de>>(file_path: PathBuf) -> Result<T> {
3738
let sierra_file = File::open(file_path).context("Unable to open json file")?;
3839
let sierra_file_reader = BufReader::new(sierra_file);
39-
4040
serde_json::from_reader(sierra_file_reader).context("Unable to read json file")
4141
}
4242

@@ -71,9 +71,11 @@ fn main_execution() -> Result<bool> {
7171
output_casm(&casm_json, compile_contract.output_path)?;
7272
}
7373
Commands::CompileRaw(compile_raw) => {
74-
let sierra_json = read_json(compile_raw.sierra_path)?;
74+
let sierra_json: Program = read_json(compile_raw.sierra_path).context(
75+
"Unable to deserialize Sierra program. Make sure it is in a correct format",
76+
)?;
7577

76-
let cairo_program_json = commands::compile_raw::compile(sierra_json)?;
78+
let cairo_program_json = commands::compile_raw::compile(&sierra_json)?;
7779

7880
output_casm(&cairo_program_json, compile_raw.output_path)?;
7981
}

tests/integration/compile_raw.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1+
use cairo_lang_sierra::program::Program;
12
use std::fs::File;
23
use test_case::test_case;
34
use universal_sierra_compiler::compile_raw;
45

5-
#[test]
6-
fn wrong_json() {
7-
let sierra_json = serde_json::json!({
8-
"wrong": "data"
9-
});
10-
11-
let cairo_program = compile_raw(sierra_json);
12-
assert!(cairo_program.is_err());
13-
}
146
#[test_case("1_7_0_trace_hint"; "sierra 1.7.0 with trace hint")]
157
#[test_case("1_7_0"; "sierra 1.7.0")]
168
#[test_case("1_6_0"; "sierra 1.6.0")]
@@ -19,8 +11,8 @@ fn wrong_json() {
1911
fn compile_raw_sierra(sierra_version: &str) {
2012
let file =
2113
File::open("tests/data/sierra_raw/sierra_".to_string() + sierra_version + ".json").unwrap();
22-
let artifact = serde_json::from_reader(file).unwrap();
23-
let compiled = compile_raw(artifact);
14+
let artifact: Program = serde_json::from_reader(file).unwrap();
15+
let compiled = compile_raw(&artifact);
2416

2517
assert!(compiled.is_ok());
2618
}

0 commit comments

Comments
 (0)