96 lines
3.5 KiB
Plaintext
96 lines
3.5 KiB
Plaintext
#! @file function.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the probe of what a call costs, as `docs/performance.md` records
|
||
|
|
#! it: `t = t ^ f(i)` over many calls, `f` held each way a program holds a
|
||
|
|
#! callable.
|
||
|
|
#! Build it as an executable and run it on one core, or run it in the machine
|
||
|
|
#! with fewer calls:
|
||
|
|
#!
|
||
|
|
#! sakura build --lib std=../std -O2 -o calls tools/function.sakura
|
||
|
|
#! taskset -c 2 ./calls
|
||
|
|
#! taskset -c 2 sakura run --lib std=../std tools/function.sakura -- 10000000
|
||
|
|
#!
|
||
|
|
#! A functor a `function` holds should cost what a function value does.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import std::time;
|
||
|
|
|
||
|
|
# A functor of two words, which a function holds in its buffer.
|
||
|
|
struct small : invokable<def(i64): i64> { var a: i64; var b: i64; }
|
||
|
|
def small::invoke(&mut this, x: i64): i64 { return x + this.a * this.b; }
|
||
|
|
|
||
|
|
# A functor of four words, which a function holds in a box.
|
||
|
|
struct large : invokable<def(i64): i64> { var a: i64; var b: i64; var pad: u64[2]; }
|
||
|
|
def large::invoke(&mut this, x: i64): i64 { return x + this.a * this.b; }
|
||
|
|
|
||
|
|
# Each callable is kept in a global, read where it is called, so that nothing
|
||
|
|
# calling it knows which it is.
|
||
|
|
var a_small: small;
|
||
|
|
var a_value: maybe<def(i64): i64> = nothing {};
|
||
|
|
var a_trait: maybe<&mut invokable<def(i64): i64>> = nothing {};
|
||
|
|
var in_place: function<def(i64): i64>;
|
||
|
|
var boxed: function<def(i64): i64>;
|
||
|
|
var a_lambda: function<def(i64): i64>;
|
||
|
|
|
||
|
|
def with_generic<F>(f: &mut F, calls: i64): i64 where F: invokable<def(i64): i64> {
|
||
|
|
var t: i64 = 0;
|
||
|
|
for i in 0..calls { t = t ^ f(i); }
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
def with_value(f: def(i64): i64, calls: i64): i64 {
|
||
|
|
var t: i64 = 0;
|
||
|
|
for i in 0..calls { t = t ^ f(i); }
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
def with_trait(f: &mut invokable<def(i64): i64>, calls: i64): i64 {
|
||
|
|
var t: i64 = 0;
|
||
|
|
for i in 0..calls { t = t ^ f(i); }
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
def with_function(f: &function<def(i64): i64>, calls: i64): i64 {
|
||
|
|
var t: i64 = 0;
|
||
|
|
for i in 0..calls { t = t ^ f(i); }
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Prints how long each call took, in nanoseconds.
|
||
|
|
def report(name: strview, start: time::instant, calls: i64, t: i64): void {
|
||
|
|
val ns := (time::instant::now() - start) / time::nanoseconds(1);
|
||
|
|
println($"{name}: {ns / (calls as f64)} ns ({t})");
|
||
|
|
}
|
||
|
|
|
||
|
|
@allow def main(args: span<strview>): void {
|
||
|
|
var calls: i64 = 300000000;
|
||
|
|
if args.length != 0 {
|
||
|
|
calls = integer::parse::<i64>(args[0]) else { return; };
|
||
|
|
}
|
||
|
|
val a := 3;
|
||
|
|
val b := 5;
|
||
|
|
a_small = small { a = 3, b = 5 };
|
||
|
|
a_value = def(x: i64): i64 => x + a * b;
|
||
|
|
a_trait = &mut a_small;
|
||
|
|
in_place = function::new(small { a = 3, b = 5 });
|
||
|
|
boxed = function::new(large { a = 3, b = 5 });
|
||
|
|
a_lambda = function::new(def(x: i64): i64 => x + a * b);
|
||
|
|
val value := a_value else { return; };
|
||
|
|
val by_trait := a_trait else { return; };
|
||
|
|
|
||
|
|
var start := time::instant::now();
|
||
|
|
report("a generic F, inlined", start, calls, with_generic(&mut a_small, calls));
|
||
|
|
start = time::instant::now();
|
||
|
|
report("a function value, its lambda using two locals", start, calls, with_value(value, calls));
|
||
|
|
start = time::instant::now();
|
||
|
|
report("a trait reference to invokable", start, calls, with_trait(by_trait, calls));
|
||
|
|
start = time::instant::now();
|
||
|
|
report("a function holding a functor of 16 bytes", start, calls, with_function(&in_place, calls));
|
||
|
|
start = time::instant::now();
|
||
|
|
report("a function holding a functor of 32 bytes, boxed", start, calls, with_function(&boxed, calls));
|
||
|
|
start = time::instant::now();
|
||
|
|
report("a function holding a function value", start, calls, with_function(&a_lambda, calls));
|
||
|
|
}
|