Skip to content

Commit

Permalink
chore: add bincode test and benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Feb 5, 2023
1 parent 9801b35 commit b94c1a1
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ authors = ["Saif Eddin Gmati <azjezz@protonmail.com>"]
[lib]
doctest = false

[dev-dependencies]
pretty_assertions = { version = "1.3.0" }

[[bin]]
name = "ara-internal-snapshot"
path = "bin/snapshot.rs"
Expand All @@ -25,6 +22,14 @@ serde = { version = "1.0.149", features = ["derive"] }
serde_json = { version = "1.0.89" }
bincode = { version = "2.0.0-rc.2" }

[dev-dependencies]
criterion = "0.4"
pretty_assertions = { version = "1.3.0" }

[[bench]]
name = "bincode"
harness = false

[profile.release]
opt-level = 3
debug = false
Expand Down
207 changes: 207 additions & 0 deletions benches/bincode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use ara_source::source::Source;
use ara_source::source::SourceKind;

use ara_parser::parser;
use ara_parser::tree::Tree;

fn decode_benchmark(criterion: &mut Criterion) {
let source = Source::inline(SourceKind::Script, CODE_SAMPLE);
let tree = parser::parse(&source).unwrap();
let config = bincode::config::standard();
let encoded_tree = bincode::encode_to_vec(&tree, config).unwrap();

criterion
.bench_function("decode tree", |bencher| {
bencher.iter(|| {
black_box(bincode::decode_from_slice::<Tree, _>(
black_box(&encoded_tree[..]),
black_box(config),
))
})
})
.bench_function("parse code", |bencher| {
bencher.iter(|| black_box(parser::parse(black_box(&source))))
});
}

criterion_group!(benches, decode_benchmark);

criterion_main!(benches);

static CODE_SAMPLE: &'static str = r#"
namespace A\B\C\D\E;
function foo(string $s): self {
exit(0);
}
function println(string $format, string|int|float ...$args): void {
printf($format, ...$args);
printf("\n");
}
function main(): void {
$a = 1;
$b = 2;
$c = $a + $b;
println("a = %d", $a);
println("b = %d", $b);
println("c = a + b = %d", $c);
}
interface Foo<S> {
public static function baz<T, U>(T $a, U $b): Q<T, (U, T)> where
S is T|U,
S is nonnnull
;
}
final class Bar implements Foo<string> {
public static function baz<T, U>(T $a, U $b): Q<T, (U, T)>
{
return new Q::<T, (U, T)>($a, ($b, $a));
}
}
final class Q<T, U> {
public function __construct(
public readonly T $a,
public readonly U $b
) {}
}
function baz<T, U>(T $a, U $b): Q<T, (U, T)> {
return new Q::<T, (U, T)>($a, ($b, $a));
}
function foo(): void {
$a?->b($c);
}
function q(): void {
$a = 2 ** 2;
$b = 1 ? 2 : 3;
$c = 1 ? 2 ? 3 : 4 : 5;
$d = 1 ?: 2 ?: 3;
$e = 1 ?? 2;
$f = 1 ?? 2 ?? 3;
}
function h(): void {
$foo['bar'];
$foo['bar']['baz'];
$foo['bar'] = 'baz';
}
function q(): void {
$a = new Foo();
$b = +1;
$c = ~2;
$d = --$b;
$e = ++$d;
}
#[A, B]
#[C, D]
interface A extends B, C {
#[R]
const int F = 344;
#[R]
public const int O = 344;
#[R]
#[P]
final public const int R = 344;
#[R]
#[P]
final const int M = 34;
#[M]
public function bar(): void;
#[Q]
#[S]
public static function baz(): void;
}
interface A extends B, C {
#[R]
const u16 F = 344;
#[R]
public const u16 O = 344;
#[R]
#[P]
final public const u16 R = 344;
#[R(), P()]
final public const u16 P = 214;
}
const u8 FOO = 1;
const u8 BAR = FOO;
type Predicate<T> = Closure<(T), bool>;
function filter<T>(vec<T> $vec, Predicate<T> $predicate): vec<T> {
$result = vec[];
foreach $vec as $item {
if $predicate($item) {
$result[] = $item;
}
}
return $result;
}
type Mapper<K, V, U> = Closure<(K, V), U>|SomeOtherType<K, V, U>;
function map<K, V, U>(dict<K, V> $dict, Mapper<K, V, U> $mapper): dict<K, U> {
$result = dict[];
if $mapper is SomeOtherType<_, _, _> {
foreach $dict as $key => $value {
$result[$key] = $mapper->doMap($key, $value);
}
} else {
foreach $dict as $key => $value {
$result[$key] = $mapper($key, $value);
}
}
return $result;
}
function foo(): void {
using {
}
using if $a {
}
using $a = foo() if $a {
}
using $a = foo() {
}
}
function main(): void {
$foo->bar;
$foo->bar->baz;
}
"#;
43 changes: 43 additions & 0 deletions tests/bincode_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::io;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use ara_source::source::Source;
use ara_source::source::SourceKind;

use ara_parser::parser;
use ara_parser::tree::Tree;

#[test]
fn test_bincode() -> io::Result<()> {
let code = r#"
function println(string $format, string|int|float ...$args): void {
printf($format, ...$args);
printf("\n");
}
function main(): void {
$a = 1;
$b = 2;
$c = $a + $b;
println("a = %d", $a);
println("b = %d", $b);
println("c = a + b = %d", $c);
}
"#;

let source = Source::inline(SourceKind::Script, code);
let tree = parser::parse(&source).unwrap();

let config = bincode::config::standard();
let encoded_tree = bincode::encode_to_vec(&tree, config).unwrap();
let decoded_tree = bincode::decode_from_slice::<Tree, _>(&encoded_tree[..], config)
.unwrap()
.0;

dbg!(encoded_tree);
dbg!(decoded_tree);

Ok(())
}

0 comments on commit b94c1a1

Please sign in to comment.