git.delta.rocks / jrsonnet / refs/commits / 4f26f96ee098

difftreelog

perf remove AST (de)serialization

Yaroslav Bolyukin2024-05-19parent: #0ae36ba.patch.diff
in: master

12 files changed

modifiedCargo.lockdiffbeforeafterboth
543 "hashbrown 0.14.5",543 "hashbrown 0.14.5",
544 "jrsonnet-gcmodule",544 "jrsonnet-gcmodule",
545 "rustc-hash",545 "rustc-hash",
546 "serde",
547 "structdump",
548]546]
549547
550[[package]]548[[package]]
563 "jrsonnet-gcmodule",561 "jrsonnet-gcmodule",
564 "jrsonnet-interner",562 "jrsonnet-interner",
565 "peg",563 "peg",
566 "serde",
567 "static_assertions",564 "static_assertions",
568 "structdump",
569]565]
570566
571[[package]]567[[package]]
586version = "0.5.0-pre96"582version = "0.5.0-pre96"
587dependencies = [583dependencies = [
588 "base64",584 "base64",
589 "bincode",
590 "jrsonnet-evaluator",585 "jrsonnet-evaluator",
591 "jrsonnet-gcmodule",586 "jrsonnet-gcmodule",
592 "jrsonnet-macros",587 "jrsonnet-macros",
602 "sha1",597 "sha1",
603 "sha2",598 "sha2",
604 "sha3",599 "sha3",
605 "structdump",
606]600]
607601
608[[package]]602[[package]]
1103source = "registry+https://github.com/rust-lang/crates.io-index"1097source = "registry+https://github.com/rust-lang/crates.io-index"
1104checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"1098checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
1105
1106[[package]]
1107name = "structdump"
1108version = "0.2.0"
1109source = "registry+https://github.com/rust-lang/crates.io-index"
1110checksum = "b0570327507bf281d8a6e6b0d4c082b12cb6bcee27efce755aa5efacd44076c1"
1111dependencies = [
1112 "proc-macro2",
1113 "quote",
1114 "structdump-derive",
1115]
1116
1117[[package]]
1118name = "structdump-derive"
1119version = "0.2.0"
1120source = "registry+https://github.com/rust-lang/crates.io-index"
1121checksum = "29cc0b59cfa11f1bceda09a9a7e37e6a6c3138575fd24ade8aa9af6d09aedf28"
1122dependencies = [
1123 "proc-macro2",
1124 "quote",
1125 "syn 1.0.109",
1126]
11271099
1128[[package]]1100[[package]]
1129name = "syn"1101name = "syn"
modifiedCargo.tomldiffbeforeafterboth
79num-bigint = "0.4.5"79num-bigint = "0.4.5"
80derivative = "2.2.0"80derivative = "2.2.0"
81strsim = "0.11.0"81strsim = "0.11.0"
82structdump = "0.2.0"
83proc-macro2 = "1.0"82proc-macro2 = "1.0"
84quote = "1.0"83quote = "1.0"
85syn = "2.0"84syn = "2.0"
modifiedcrates/jrsonnet-interner/Cargo.tomldiffbeforeafterboth
10[lints]10[lints]
11workspace = true11workspace = true
12
13[features]
14default = []
15# Implement value serialization using structdump
16structdump = ["dep:structdump"]
17# Implement value serialization using serde
18#
19# Warning: serialized values won't be deduplicated
20serde = ["dep:serde"]
2112
22[dependencies]13[dependencies]
23jrsonnet-gcmodule.workspace = true14jrsonnet-gcmodule.workspace = true
24
25serde = { workspace = true, optional = true }
26structdump = { workspace = true, optional = true }
2715
28rustc-hash.workspace = true16rustc-hash.workspace = true
29hashbrown = { workspace = true, features = ["inline-more"] }17hashbrown = { workspace = true, features = ["inline-more"] }
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
219 }219 }
220}220}
221
222#[cfg(feature = "serde")]
223impl serde::Serialize for IStr {
224 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
225 where
226 S: serde::Serializer,
227 {
228 self.as_str().serialize(serializer)
229 }
230}
231
232#[cfg(feature = "serde")]
233impl<'de> serde::Deserialize<'de> for IStr {
234 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
235 where
236 D: serde::Deserializer<'de>,
237 {
238 let str = <&str>::deserialize(deserializer)?;
239 Ok(intern_str(str))
240 }
241}
242
243#[cfg(feature = "structdump")]
244impl structdump::Codegen for IStr {
245 fn gen_code(
246 &self,
247 res: &mut structdump::CodegenResult,
248 _unique: bool,
249 ) -> structdump::TokenStream {
250 let s: &str = self;
251 res.add_code(
252 structdump::quote! {
253 structdump_import::IStr::from(#s)
254 },
255 Some(structdump::quote![structdump_import::IStr]),
256 false,
257 )
258 }
259}
260221
261thread_local! {222thread_local! {
262 static POOL: RefCell<HashMap<Inner, (), BuildHasherDefault<FxHasher>>> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));223 static POOL: RefCell<HashMap<Inner, (), BuildHasherDefault<FxHasher>>> = RefCell::new(HashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));
modifiedcrates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth
11default = []11default = []
12exp-destruct = []12exp-destruct = []
13exp-null-coaelse = []13exp-null-coaelse = []
14# Implement serialization of AST using structdump
15#
16# Structdump generates code, which exactly replicated passed AST
17# Contrary to serde, has no code bloat problem, and is recommended
18#
19# The only limitation is serialized form is only useable if built from build script
20structdump = ["dep:structdump", "jrsonnet-interner/structdump"]
21# Implement serialization of AST using serde
22#
23# Warning: as serde doesn't deduplicate strings, `Source` struct will bloat
24# output binary with repeating source code. To resolve this issue, you should either
25# override serialization of this struct using custom `Serializer`/`Deserializer`,
26# not rely on Source, and fill its `source_code` with empty value, or use `structdump`
27# instead
28serde = ["dep:serde"]
2914
30[dependencies]15[dependencies]
31jrsonnet-interner.workspace = true16jrsonnet-interner.workspace = true
3520
36peg.workspace = true21peg.workspace = true
37
38serde = { workspace = true, features = ["derive", "rc"], optional = true }
39structdump = { workspace = true, features = ["derive"], optional = true }
4022
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
66
7use jrsonnet_gcmodule::Trace;7use jrsonnet_gcmodule::Trace;
8use jrsonnet_interner::IStr;8use jrsonnet_interner::IStr;
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11#[cfg(feature = "structdump")]
12use structdump::Codegen;
139
14use crate::source::Source;10use crate::source::Source;
1511
16#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
17#[cfg_attr(feature = "structdump", derive(Codegen))]
18#[derive(Debug, PartialEq, Trace)]12#[derive(Debug, PartialEq, Trace)]
19pub enum FieldName {13pub enum FieldName {
20 /// {fixed: 2}14 /// {fixed: 2}
23 Dyn(LocExpr),17 Dyn(LocExpr),
24}18}
2519
26#[cfg_attr(feature = "structdump", derive(Codegen))]
27#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]20#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
29#[repr(u8)]21#[repr(u8)]
30pub enum Visibility {22pub enum Visibility {
42 }34 }
43}35}
4436
45#[cfg_attr(feature = "structdump", derive(Codegen))]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47#[derive(Clone, Debug, PartialEq, Trace)]37#[derive(Clone, Debug, PartialEq, Trace)]
48pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);38pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);
4939
50#[cfg_attr(feature = "structdump", derive(Codegen))]
51#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
52#[derive(Debug, PartialEq, Trace)]40#[derive(Debug, PartialEq, Trace)]
53pub struct FieldMember {41pub struct FieldMember {
54 pub name: FieldName,42 pub name: FieldName,
58 pub value: LocExpr,46 pub value: LocExpr,
59}47}
6048
61#[cfg_attr(feature = "structdump", derive(Codegen))]
62#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63#[derive(Debug, PartialEq, Trace)]49#[derive(Debug, PartialEq, Trace)]
64pub enum Member {50pub enum Member {
65 Field(FieldMember),51 Field(FieldMember),
66 BindStmt(BindSpec),52 BindStmt(BindSpec),
67 AssertStmt(AssertStmt),53 AssertStmt(AssertStmt),
68}54}
6955
70#[cfg_attr(feature = "structdump", derive(Codegen))]
71#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]56#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
73pub enum UnaryOpType {57pub enum UnaryOpType {
74 Plus,58 Plus,
93 }77 }
94}78}
9579
96#[cfg_attr(feature = "structdump", derive(Codegen))]
97#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]80#[derive(Debug, Clone, Copy, PartialEq, Eq, Trace)]
99pub enum BinaryOpType {81pub enum BinaryOpType {
100 Mul,82 Mul,
164}146}
165147
166/// name, default value148/// name, default value
167#[cfg_attr(feature = "structdump", derive(Codegen))]
168#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
169#[derive(Debug, PartialEq, Trace)]149#[derive(Debug, PartialEq, Trace)]
170pub struct Param(pub Destruct, pub Option<LocExpr>);150pub struct Param(pub Destruct, pub Option<LocExpr>);
171151
172/// Defined function parameters152/// Defined function parameters
173#[cfg_attr(feature = "structdump", derive(Codegen))]
174#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
175#[derive(Debug, Clone, PartialEq, Trace)]153#[derive(Debug, Clone, PartialEq, Trace)]
176pub struct ParamsDesc(pub Rc<Vec<Param>>);154pub struct ParamsDesc(pub Rc<Vec<Param>>);
177155
182 }160 }
183}161}
184162
185#[cfg_attr(feature = "structdump", derive(Codegen))]
186#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
187#[derive(Debug, PartialEq, Trace)]163#[derive(Debug, PartialEq, Trace)]
188pub struct ArgsDesc {164pub struct ArgsDesc {
189 pub unnamed: Vec<LocExpr>,165 pub unnamed: Vec<LocExpr>,
195 }171 }
196}172}
197173
198#[cfg_attr(feature = "structdump", derive(Codegen))]
199#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
200#[derive(Debug, Clone, PartialEq, Eq, Trace)]174#[derive(Debug, Clone, PartialEq, Eq, Trace)]
201pub enum DestructRest {175pub enum DestructRest {
202 /// ...rest176 /// ...rest
205 Drop,179 Drop,
206}180}
207181
208#[cfg_attr(feature = "structdump", derive(Codegen))]
209#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
210#[derive(Debug, Clone, PartialEq, Trace)]182#[derive(Debug, Clone, PartialEq, Trace)]
211pub enum Destruct {183pub enum Destruct {
212 Full(IStr),184 Full(IStr),
268 }240 }
269}241}
270242
271#[cfg_attr(feature = "structdump", derive(Codegen))]
272#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
273#[derive(Debug, Clone, PartialEq, Trace)]243#[derive(Debug, Clone, PartialEq, Trace)]
274pub enum BindSpec {244pub enum BindSpec {
275 Field {245 Field {
291 }261 }
292}262}
293263
294#[cfg_attr(feature = "structdump", derive(Codegen))]
295#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
296#[derive(Debug, PartialEq, Trace)]264#[derive(Debug, PartialEq, Trace)]
297pub struct IfSpecData(pub LocExpr);265pub struct IfSpecData(pub LocExpr);
298266
299#[cfg_attr(feature = "structdump", derive(Codegen))]
300#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
301#[derive(Debug, PartialEq, Trace)]267#[derive(Debug, PartialEq, Trace)]
302pub struct ForSpecData(pub Destruct, pub LocExpr);268pub struct ForSpecData(pub Destruct, pub LocExpr);
303269
304#[cfg_attr(feature = "structdump", derive(Codegen))]
305#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
306#[derive(Debug, PartialEq, Trace)]270#[derive(Debug, PartialEq, Trace)]
307pub enum CompSpec {271pub enum CompSpec {
308 IfSpec(IfSpecData),272 IfSpec(IfSpecData),
309 ForSpec(ForSpecData),273 ForSpec(ForSpecData),
310}274}
311275
312#[cfg_attr(feature = "structdump", derive(Codegen))]
313#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
314#[derive(Debug, PartialEq, Trace)]276#[derive(Debug, PartialEq, Trace)]
315pub struct ObjComp {277pub struct ObjComp {
316 pub pre_locals: Vec<BindSpec>,278 pub pre_locals: Vec<BindSpec>,
319 pub compspecs: Vec<CompSpec>,281 pub compspecs: Vec<CompSpec>,
320}282}
321283
322#[cfg_attr(feature = "structdump", derive(Codegen))]
323#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
324#[derive(Debug, PartialEq, Trace)]284#[derive(Debug, PartialEq, Trace)]
325pub enum ObjBody {285pub enum ObjBody {
326 MemberList(Vec<Member>),286 MemberList(Vec<Member>),
327 ObjComp(ObjComp),287 ObjComp(ObjComp),
328}288}
329289
330#[cfg_attr(feature = "structdump", derive(Codegen))]
331#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
332#[derive(Debug, PartialEq, Eq, Clone, Copy, Trace)]290#[derive(Debug, PartialEq, Eq, Clone, Copy, Trace)]
333pub enum LiteralType {291pub enum LiteralType {
334 This,292 This,
339 False,297 False,
340}298}
341299
342#[cfg_attr(feature = "structdump", derive(Codegen))]
343#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
344#[derive(Debug, PartialEq, Trace)]300#[derive(Debug, PartialEq, Trace)]
345pub struct SliceDesc {301pub struct SliceDesc {
346 pub start: Option<LocExpr>,302 pub start: Option<LocExpr>,
349}305}
350306
351/// Syntax base307/// Syntax base
352#[cfg_attr(feature = "structdump", derive(Codegen))]
353#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
354#[derive(Debug, PartialEq, Trace)]308#[derive(Debug, PartialEq, Trace)]
355pub enum Expr {309pub enum Expr {
356 Literal(LiteralType),310 Literal(LiteralType),
420 Slice(LocExpr, SliceDesc),374 Slice(LocExpr, SliceDesc),
421}375}
422376
423#[cfg_attr(feature = "structdump", derive(Codegen))]
424#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
425#[derive(Debug, PartialEq, Trace)]377#[derive(Debug, PartialEq, Trace)]
426pub struct IndexPart {378pub struct IndexPart {
427 pub value: LocExpr,379 pub value: LocExpr,
430}382}
431383
432/// file, begin offset, end offset384/// file, begin offset, end offset
433#[cfg_attr(feature = "structdump", derive(Codegen))]
434#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
435#[derive(Clone, PartialEq, Eq, Trace)]385#[derive(Clone, PartialEq, Eq, Trace)]
436#[trace(skip)]386#[trace(skip)]
437#[repr(C)]387#[repr(C)]
452}402}
453403
454/// Holds AST expression and its location in source file404/// Holds AST expression and its location in source file
455#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
456#[cfg_attr(feature = "structdump", derive(Codegen))]
457#[derive(Clone, PartialEq, Trace)]405#[derive(Clone, PartialEq, Trace)]
458pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);406pub struct LocExpr(pub Rc<Expr>, pub ExprLocation);
459407
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
88
9use jrsonnet_gcmodule::{Trace, Tracer};9use jrsonnet_gcmodule::{Trace, Tracer};
10use jrsonnet_interner::{IBytes, IStr};10use jrsonnet_interner::{IBytes, IStr};
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13#[cfg(feature = "structdump")]
14use structdump::Codegen;
1511
16use crate::location::{location_to_offset, offset_to_location, CodeLocation};12use crate::location::{location_to_offset, offset_to_location, CodeLocation};
1713
133 }129 }
134}130}
135
136#[cfg(feature = "structdump")]
137impl Codegen for SourcePath {
138 fn gen_code(
139 &self,
140 res: &mut structdump::CodegenResult,
141 unique: bool,
142 ) -> structdump::TokenStream {
143 let source_virtual = self
144 .0
145 .as_any()
146 .downcast_ref::<SourceVirtual>()
147 .expect("can only codegen for virtual source paths!")
148 .0
149 .clone();
150 let val = res.add_value(source_virtual, false);
151 res.add_code(
152 structdump::quote! {
153 structdump_import::SourcePath::new(structdump_import::SourceVirtual(#val))
154 },
155 Some(structdump::quote!(SourcePath)),
156 unique,
157 )
158 }
159}
160131
161#[derive(Trace, Hash, PartialEq, Eq, Debug)]132#[derive(Trace, Hash, PartialEq, Eq, Debug)]
162struct SourceDefault;133struct SourceDefault;
237///208///
238/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,209/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,
239/// and user can construct arbitrary values by hand, without asking import resolver210/// and user can construct arbitrary values by hand, without asking import resolver
240#[cfg_attr(feature = "structdump", derive(Codegen))]
241#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]211#[derive(Trace, Hash, PartialEq, Eq, Debug, Clone)]
242pub struct SourceVirtual(pub IStr);212pub struct SourceVirtual(pub IStr);
243impl Display for SourceVirtual {213impl Display for SourceVirtual {
288258
289/// Either real file, or virtual259/// Either real file, or virtual
290/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut260/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
291#[cfg_attr(feature = "structdump", derive(Codegen))]
292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
293#[derive(Clone, PartialEq, Eq, Debug)]261#[derive(Clone, PartialEq, Eq, Debug)]
294pub struct Source(pub Rc<(SourcePath, IStr)>);262pub struct Source(pub Rc<(SourcePath, IStr)>);
295263
modifiedcrates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth
11workspace = true11workspace = true
1212
13[features]13[features]
14default = ["codegenerated-stdlib"]
15# Speed-up initialization by generating code for parsed stdlib,
16# instead of invoking parser for it.
17# This is mutually exclusive with `serialized-stdlib`.
18codegenerated-stdlib = ["jrsonnet-parser/structdump"]
19# Use the embedded serialized stdlib.
20# This is mutually exclusive with `codegenerated-stdlib`.
21serialized-stdlib = []
22# Enables legacy `std.thisFile` support, at the cost of worse caching14# Enables legacy `std.thisFile` support, at the cost of worse caching
23legacy-this-file = []15legacy-this-file = []
24# Add order preservation flag to some functions16# Add order preservation flag to some functions
36jrsonnet-parser.workspace = true28jrsonnet-parser.workspace = true
37jrsonnet-gcmodule.workspace = true29jrsonnet-gcmodule.workspace = true
3830
39# Used for stdlib AST serialization
40bincode = { workspace = true, optional = true }
41# Used both for stdlib AST serialization and std.parseJson/std.parseYaml31# Used for std.parseJson/std.parseYaml
42serde.workspace = true32serde.workspace = true
4333
44# std.md534# std.md5
6555
66[build-dependencies]56[build-dependencies]
67jrsonnet-parser.workspace = true57jrsonnet-parser.workspace = true
68structdump = { workspace = true, features = ["derive"] }
6958
deletedcrates/jrsonnet-stdlib/build.rsdiffbeforeafterboth

no changes

deletedcrates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth

no changes

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
36mod arrays;36mod arrays;
37mod compat;37mod compat;
38mod encoding;38mod encoding;
39mod expr;
40mod hash;39mod hash;
41mod manifest;40mod manifest;
42mod math;41mod math;
55pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {54pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
56 let mut builder = ObjValueBuilder::new();55 let mut builder = ObjValueBuilder::new();
57
58 let expr = expr::stdlib_expr();
59 let eval = jrsonnet_evaluator::evaluate(ContextBuilder::dangerous_empty_state().build(), &expr)
60 .expect("stdlib.jsonnet should have no errors")
61 .as_obj()
62 .expect("stdlib.jsonnet should evaluate to object");
63
64 builder.with_super(eval);
6556
66 // FIXME: Use PHF57 // FIXME: Use PHF
67 for (name, builtin) in [58 for (name, builtin) in [
deletedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth

no changes