Skip to content

Commit e8011e8

Browse files
authored
Initial work towards optimising STG (#158)
-- feature: some more advanced settings (`--stg-xxx`) have been exposed to control the behaviour of the internal STG compiler and evaluator, to allow easier benchmarking and performance comparison. These are heavily subject to change.
1 parent b9ff4cb commit e8011e8

22 files changed

+1228
-557
lines changed

src/driver/eval.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
driver::{error::EucalyptError, options::EucalyptOptions, source::SourceLoader},
99
eval::{
1010
error::ExecutionError,
11-
machines::stg::{self, dump_runtime, RenderType, StgSettings},
11+
machines::stg::{self, make_standard_runtime},
1212
},
1313
export,
1414
};
@@ -51,7 +51,6 @@ fn determine_format(opt: &EucalyptOptions, loader: &SourceLoader) -> String {
5151
opt_format
5252
.or(target_format)
5353
.or(main_format)
54-
.clone()
5554
.unwrap_or(&"yaml".to_string())
5655
.to_string()
5756
}
@@ -134,36 +133,15 @@ impl<'a> Executor<'a> {
134133
let mut emitter = export::create_emitter(&format, output.as_mut())
135134
.ok_or_else(|| ExecutionError::UnknownFormat(format.to_string()))?;
136135

137-
let render_type = if opt.is_fragment() {
138-
RenderType::RenderFragment
139-
} else {
140-
RenderType::RenderDoc
141-
};
142-
143-
let settings = if opt.debug() {
144-
eprintln!("Running in DEBUG mode");
145-
StgSettings {
146-
generate_annotations: true,
147-
render_type,
148-
trace_steps: true,
149-
suppress_updates: false,
150-
}
151-
} else {
152-
StgSettings {
153-
generate_annotations: true,
154-
render_type,
155-
trace_steps: false,
156-
suppress_updates: false,
157-
}
158-
};
136+
let rt = make_standard_runtime(&mut self.source_map);
159137

160138
if opt.dump_runtime() {
161-
println!("{}", dump_runtime());
139+
println!("{}", prettify::prettify(rt.as_ref()));
162140
Ok(None)
163141
} else {
164142
let syn = {
165143
let t = Instant::now();
166-
let syn = stg::compile(&settings, self.evaluand.clone())?;
144+
let syn = stg::compile(opt.stg_settings(), self.evaluand.clone(), rt.as_ref())?;
167145
stats.timings_mut().record("stg-compile", t.elapsed());
168146
syn
169147
};
@@ -174,7 +152,7 @@ impl<'a> Executor<'a> {
174152
} else {
175153
emitter.stream_start();
176154
let mut machine =
177-
stg::standard_machine(&settings, syn, emitter, &mut self.source_map)?;
155+
stg::standard_machine(opt.stg_settings(), syn, emitter, rt.as_ref())?;
178156

179157
let ret = {
180158
let t = Instant::now();

src/driver/options.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Command line argument handling.
2-
use crate::driver::error::EucalyptError;
3-
use crate::syntax::input::{Input, Locator};
2+
use crate::{driver::error::EucalyptError, eval::machines::stg::StgSettings};
3+
use crate::{
4+
eval::machines::stg::RenderType,
5+
syntax::input::{Input, Locator},
6+
};
47
use atty::Stream;
58
use std::path::PathBuf;
69
use std::str::FromStr;
@@ -86,6 +89,9 @@ pub struct EucalyptOptions {
8689
#[structopt(short = "x", long)]
8790
export_type: Option<String>,
8891

92+
#[structopt(flatten)]
93+
stg_settings: StgSettings,
94+
8995
/// Source code / data inputs (in order)
9096
inputs: Vec<Input>,
9197
}
@@ -394,6 +400,10 @@ impl EucalyptOptions {
394400
self.statistics
395401
}
396402

403+
pub fn stg_settings(&self) -> &StgSettings {
404+
&self.stg_settings
405+
}
406+
397407
/// Prepend an input if it is not already in the input list
398408
fn prepend_input(&mut self, input: Input) {
399409
if !self.inputs.iter().any(|i| i == &input) {
@@ -479,6 +489,15 @@ impl EucalyptOptions {
479489
)));
480490
}
481491

492+
// Set default STG settings based on debug & other settings
493+
self.stg_settings.generate_annotations = true;
494+
if self.debug() {
495+
self.stg_settings.trace_steps = true;
496+
}
497+
if self.is_fragment() && self.stg_settings.render_type != RenderType::Headless {
498+
self.stg_settings.render_type = RenderType::RenderFragment;
499+
}
500+
482501
Ok(())
483502
}
484503
}

src/eval/machines/stg/arith.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ pub mod tests {
286286
env,
287287
machine::Machine,
288288
panic::Panic,
289-
runtime,
289+
runtime::{self, Runtime},
290290
syntax::{dsl::*, StgSyn},
291291
},
292292
},
@@ -297,6 +297,7 @@ pub mod tests {
297297
let mut rt = runtime::StandardRuntime::default();
298298
rt.add(Box::new(Add));
299299
rt.add(Box::new(Panic));
300+
rt.prepare(&mut SourceMap::default());
300301
Box::new(rt)
301302
};
302303
}
@@ -307,7 +308,7 @@ pub mod tests {
307308
Machine::new(
308309
syntax,
309310
Rc::new(env),
310-
RUNTIME.globals(&mut SourceMap::default()),
311+
RUNTIME.globals(),
311312
RUNTIME.intrinsics(),
312313
Box::new(DebugEmitter::default()),
313314
true,

0 commit comments

Comments
 (0)