Skip to content
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

Using mmap to load bytecode #51

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/erars-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ parking_lot = "0.12.1"
unicode-bom = "1.1.4"
encoding_rs = "0.8.31"
rmp-serde = "1.1.1"
memmap2 = "0.5.7"

[features]
multithread = [
Expand Down
26 changes: 17 additions & 9 deletions crates/erars-loader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use parking_lot::Mutex;
#[cfg(feature = "multithread")]
use rayon::prelude::*;
Expand Down Expand Up @@ -76,8 +77,8 @@ fn read_file(path: &Path) -> std::io::Result<String> {
}
}

pub fn save_script(vm: TerminalVm, ctx: VmContext) -> anyhow::Result<()> {
let mut out = BufWriter::new(File::create("game.era")?);
pub fn save_script(vm: TerminalVm, ctx: VmContext, target_path: &str) -> anyhow::Result<()> {
let mut out = BufWriter::new(File::create(Path::new(target_path).join("game.era"))?);
erars_bytecode::write_to(&mut out, &vm.dic)?;
let local_infos: HashMap<StrKey, Vec<(StrKey, &VariableInfo)>> =
ctx.var.local_infos().collect();
Expand All @@ -88,14 +89,15 @@ pub fn save_script(vm: TerminalVm, ctx: VmContext) -> anyhow::Result<()> {

/// SAFETY: Any reference to interner is not exist
pub unsafe fn load_script(
target_path: String,
target_path: &str,
inputs: Vec<Value>,
system: Box<dyn SystemFunctions>,
) -> anyhow::Result<(TerminalVm, VmContext, VirtualConsole)> {
let start = Instant::now();

log::info!("Load config");
let config_path = Path::new(target_path.as_str()).join("emuera.config");
let config_path = Path::new(target_path).join("emuera.config");
let game_path = Path::new(target_path).join("game.era");

let config = if Path::new(&config_path).exists() {
match read_file(config_path.as_ref()) {
Expand All @@ -112,12 +114,18 @@ pub unsafe fn load_script(
log::info!("Config: {config:?}");

log::info!("Load game script");
let mut read = BufReader::new(File::open("game.era").unwrap());
let dic = erars_bytecode::read_from(&mut read)?;

let file = File::open(game_path).context("Open bytecode file")?;
let file = memmap2::MmapOptions::new()
.populate()
.map(&file)
.context("mmap bytecode file")?;
let mut file_bytes = &*file;
let dic = erars_bytecode::read_from(&mut file_bytes)?;

log::info!("Load game data");
let (header, local_infos): (HeaderInfo, HashMap<StrKey, Vec<(StrKey, VariableInfo)>>) =
rmp_serde::decode::from_read(&mut read)?;
rmp_serde::decode::from_read(&mut file_bytes)?;
let mut vconsole = VirtualConsole::new(config.printc_width, config.max_log);

let elapsed = start.elapsed();
Expand All @@ -140,15 +148,15 @@ pub unsafe fn load_script(

#[allow(unused_assignments)]
pub fn run_script(
target_path: String,
target_path: &str,
inputs: Vec<Value>,
system: Box<dyn SystemFunctions>,
) -> anyhow::Result<(TerminalVm, VmContext, VirtualConsole)> {
erars_ast::init_interner();

let mut time = Instant::now();

let config_path = Path::new(target_path.as_str()).join("emuera.config");
let config_path = Path::new(target_path).join("emuera.config");

let config = if Path::new(&config_path).exists() {
match read_file(config_path.as_ref()) {
Expand Down
6 changes: 3 additions & 3 deletions crates/erars-stdio/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ fn main() {
));

let (vm, mut ctx, mut tx) = if args.load {
unsafe { load_script(args.target_path, inputs, system).unwrap() }
unsafe { load_script(&args.target_path, inputs, system).unwrap() }
} else {
run_script(args.target_path, inputs, system).unwrap()
run_script(&args.target_path, inputs, system).unwrap()
};

if args.save {
save_script(vm, ctx).unwrap();
save_script(vm, ctx, &args.target_path).unwrap();
} else {
futures_executor::block_on(async move { vm.start(&mut tx, &mut ctx).await });
}
Expand Down