Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Mitigate printing bottleneck (Issue#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alonely0 committed Aug 7, 2021
1 parent 095fe43 commit 98454e7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/interpreter/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::utils::path::Path;
use super::variables::Variables;
use super::Literal;
use super::Str;
use std::io::{self, Write};

type Args = Vec<String>;

Expand Down Expand Up @@ -58,9 +59,12 @@ impl Functions for super::Interpreter {

// Functions definitions
fn r#print(&self, args: &Args) {
// mitigate printing bottleneck by using only 1 print
// context: i used to have a for loop
println!("{}", args.join("\n"));
// mitigate printing bottleneck by locking
// the stdout and creating a write buffer
let stdout = io::stdout(); // get the global stdout entity
let mut handle = io::BufWriter::new(stdout.lock());
writeln!(handle, "{}", args.join("\n")).unwrap();
handle.flush().unwrap();

This comment has been minimized.

Copy link
@cg-jl

cg-jl Aug 7, 2021

Contributor

I find it curious that you use a bufwriter and then flush it, is it any faster to calling writeln! on StdoutLock directly? Like it's shown in the CLI Book?

This comment has been minimized.

Copy link
@Alonely0

Alonely0 Aug 7, 2021

Author Owner

#2

This comment has been minimized.

Copy link
@cg-jl

cg-jl Aug 7, 2021

Contributor

ok, thanks!

}

fn r#create(&self, args: &Args) {
Expand Down
2 changes: 0 additions & 2 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ impl Interpreter {
__directory__: dir,
__recursive__: recursive,
__ast__: ast,

// variables needed for runtime
__file__: String::from(""),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod parser;

fn main() {
let cli_args: cli::Cli = cli::get_cli_args();

println_on_debug!(
"\nVoila v{}, DEBUG ENABLED\n
Debug is always enabled on development versions.
Expand All @@ -26,7 +27,6 @@ For more information see the README.\n
);

let tokens: Vec<lexer::Token> = lexer::lex(&cli_args.source);

let ast: parser::ast::AST = parser::parse(tokens);
let interpreter = interpreter::run(ast, cli_args.dir, cli_args.recursive);
block_on(interpreter); // wait interpreter to finish
Expand Down

0 comments on commit 98454e7

Please sign in to comment.