Skip to content

Commit

Permalink
Adds some array benchmarks to the ones tracked in CI (#2067)
Browse files Browse the repository at this point in the history
* Rewrite the array sum benchmark to be more idiomatic

* Add some array benchmarks to ci

* Silence warnings
  • Loading branch information
jneem authored Oct 10, 2024
1 parent a6825db commit 983529f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
--branch master \
--testbed ubuntu-latest \
--adapter rust_criterion \
nix develop --command cargo bench --package nickel-lang-core --bench numeric
nix develop --command cargo bench --package nickel-lang-core --features=benchmark-ci --bench numeric --bench arrays
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
nix_path: "nixpkgs=channel:nixos-unstable"
- name: run benchmarks
run: |
nix develop --command cargo bench --package nickel-lang-core --bench numeric > criterion-output.txt
nix develop --command cargo bench --package nickel-lang-core --features=benchmark-ci --bench numeric --bench arrays > criterion-output.txt
- name: upload results
uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ format = ["dep:topiary-core", "dep:topiary-queries", "dep:tree-sitter-nickel"]
metrics = ["dep:metrics"]
nix-experimental = [ "dep:cxx", "dep:cxx-build", "dep:pkg-config" ]
spanned-deser = ["dep:serde-untagged"]
benchmark-ci = []

[build-dependencies]
lalrpop.workspace = true
Expand Down
63 changes: 63 additions & 0 deletions core/benches/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(feature = "benchmark-ci", allow(unused_imports))]

use std::rc::Rc;

use criterion::criterion_main;
Expand All @@ -9,6 +11,7 @@ use nickel_lang_utils::{bench::criterion_config, bench::EvalMode, ncl_bench_grou
use pretty::{BoxAllocator, DocBuilder, Pretty};

/// Generates a pseaudo-random Nickel array as a string.
#[cfg(not(feature = "benchmark-ci"))]
fn ncl_random_array(len: usize) -> String {
let m = 2_u64.pow(32);
let a = 1664525;
Expand All @@ -32,6 +35,7 @@ fn ncl_random_array(len: usize) -> String {
String::from_utf8(out).unwrap()
}

#[cfg(not(feature = "benchmark-ci"))]
ncl_bench_group! {
name = benches;
config = criterion_config();
Expand Down Expand Up @@ -168,4 +172,63 @@ config = criterion_config();
eval_mode = EvalMode::DeepSeq,
}
}

#[cfg(feature = "benchmark-ci")]
ncl_bench_group! {
name = benches;
config = criterion_config();
{
name = "foldr strings 50",
path = "arrays/fold",
subtest = "right.strings",
args = (50),
}, {
name = "foldr strings 500",
path = "arrays/fold",
subtest = "right.strings",
args = (500),
}, {
name = "foldl arrays 50",
path = "arrays/fold",
subtest = "left.arrays",
args = (50),
}, {
name = "foldl arrays 500",
path = "arrays/fold",
subtest = "left.arrays",
args = (500),
}, {
name = "generate normal 50",
path = "arrays/generate",
subtest = "checked",
args = (50),
}, {
name = "generate normal 250",
path = "arrays/generate",
subtest = "checked",
// Most other benchmarks have a factor of 10 between
// the small and large sizes, but this one is slow so
// use a factor of 5.
args = (250),
}, {
name = "generate normal unchecked 200",
path = "arrays/generate",
subtest = "unchecked",
args = (200),
}, {
name = "generate normal unchecked 1000",
path = "arrays/generate",
subtest = "unchecked",
args = (1000),
}, {
name = "pipe normal 20",
path = "arrays/pipe",
args = (20),
}, {
name = "pipe normal 200",
path = "arrays/pipe",
args = (200),
},
}

criterion_main!(benches);
9 changes: 4 additions & 5 deletions core/benches/arrays/sum.ncl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
let rec sum | Array Number -> Number
= fun xs =>
if std.array.length xs == 0 then
0
else
std.array.first xs + sum (std.array.drop_first xs)
= match {
[] => 0,
[x, ..xs] => x + sum xs,
}
in
{
run = fun n => std.array.generate (fun x => x + 1) n |> sum
Expand Down

0 comments on commit 983529f

Please sign in to comment.