Skip to content

Commit

Permalink
Merge pull request #11 from ratel-rust/test-n-benches
Browse files Browse the repository at this point in the history
No Rust CLI, added benchmarks
  • Loading branch information
maciejhirsz authored Oct 16, 2016
2 parents 172ba62 + 086346c commit e2f91f9
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 164 deletions.
10 changes: 1 addition & 9 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
[package]
name = "ratel"
version = "0.3.0"
version = "0.4.0"
authors = ["Maciej Hirsz <[email protected]>"]
license = "MIT"
description = "JavaScript transpiler in Rust"
repository = "https://github.com/ratel-rust/ratel-core"
documentation = "https://github.com/ratel-rust/ratel-core"

[profile.release]
panic = "abort"

[dependencies]
docopt = "0.6.78"
rustc-serialize = "0.3.16"
itoa = "0.1.1"
15 changes: 15 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# the `ratel` crate

## Test:

```
cargo test
```

## Benchmark:

```
cargo bench
```

Benchmarks require a nightly version of Rust to work.
135 changes: 135 additions & 0 deletions core/benches/colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#![feature(test)]

extern crate test;
extern crate ratel;

use test::Bencher;

static SOURCE: &'static str = r#"
'use strict';
/**
* Extranct red color out of a color integer:
*
* 0x00DEAD -> 0x00
*
* @param {Number} color
* @return {Number}
*/
function red( color )
{
let foo = 3.14;
return color >> 16;
}
/**
* Extranct red color out of a color integer:
*
* 0x00DEAD -> 0xDE
*
* @param {Number} color
* @return {Number}
*/
function green( color )
{
return ( color >> 8 ) & 0xFF;
}
/**
* Extranct red color out of a color integer:
*
* 0x00DEAD -> 0xAD
*
* @param {Number} color
* @return {Number}
*/
function blue( color )
{
return color & 0xFF;
}
/**
* Converts an integer containing a color such as 0x00DEAD to a hex
* string, such as '#00DEAD';
*
* @param {Number} int
* @return {String}
*/
function intToHex( int )
{
const mask = '#000000';
const hex = int.toString( 16 );
return mask.substring( 0, 7 - hex.length ) + hex;
}
/**
* Converts a hex string containing a color such as '#00DEAD' to
* an integer, such as 0x00DEAD;
*
* @param {Number} num
* @return {String}
*/
function hexToInt( hex )
{
return parseInt( hex.substring( 1 ), 16 );
}
module.exports = {
red,
green,
blue,
intToHex,
hexToInt,
};
"#;

#[bench]
fn parse_to_ast(b: &mut Bencher) {
b.bytes = SOURCE.len() as u64;

b.iter(|| {
let ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse");

ast
});
}


#[bench]
fn parse_to_ast_and_transform_es5(b: &mut Bencher) {
b.bytes = SOURCE.len() as u64;

b.iter(|| {
let mut ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse");

let settings = ratel::transformer::Settings::target_es5();

ratel::transformer::transform(&mut ast, settings);

ast
});
}

#[bench]
fn codegen_from_ast(b: &mut Bencher) {
let mut ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse");

let settings = ratel::transformer::Settings::target_es5();

ratel::transformer::transform(&mut ast, settings);

let output = ratel::codegen::generate_code(&ast, true);

b.bytes = output.len() as u64;

b.iter(|| {
ratel::codegen::generate_code(&ast, true)
});
}
11 changes: 5 additions & 6 deletions core/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extern crate itoa;

use std::ptr;
use std::io::Write;

use grammar::*;
use grammar::OperatorType::*;
Expand Down Expand Up @@ -129,7 +128,7 @@ trait Code {
impl Code for u64 {
#[inline]
fn to_code(&self, gen: &mut Generator) {
itoa::write(&mut gen.code, *self).expect("Can't fail on a Vec");
write!(&mut gen.code, "{}", *self).expect("Can't fail on a Vec");
}
}

Expand Down Expand Up @@ -716,11 +715,11 @@ impl Code for Statement {
}
}

pub fn generate_code(program: Program, minify: bool) -> String {
pub fn generate_code(program: &Program, minify: bool) -> String {
let mut gen = Generator::new(minify);

for statement in program.body {
gen.write(&statement);
for statement in &program.body {
gen.write(statement);
gen.new_line();
}

Expand Down
147 changes: 0 additions & 147 deletions core/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion core/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use ratel::grammar::OperatorType::*;
fn output_program(input_program: &str) -> String {
let mut ast = parser::parse(input_program.to_string()).expect("Must compile");
transformer::transform(&mut ast, transformer::Settings::target_es5());
codegen::generate_code(ast, true)
codegen::generate_code(&ast, true)
}

macro_rules! assert_compile {
Expand Down
1 change: 1 addition & 0 deletions ffi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
npm-debug.log
Cargo.lock
native/target
native/index.node
Expand Down
2 changes: 1 addition & 1 deletion ffi/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn transform(call: Call) -> JsResult<JsString> {
};

transformer::transform(&mut ast, transformer::Settings::target_es5());
let out = codegen::generate_code(ast, minify.value());
let out = codegen::generate_code(&ast, minify.value());

Ok(JsString::new(scope, &out).unwrap())
}
Expand Down

0 comments on commit e2f91f9

Please sign in to comment.