Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,148 @@
|
||||
#! @file memory.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the probe of how fast a program's memory is, as `docs/performance.md`
|
||||
#! records it: a list of 1,000,000 nodes built and summed, ten times over, its
|
||||
#! nodes held each way std holds memory. Build it as an executable and run it on
|
||||
#! one core:
|
||||
#!
|
||||
#! sakura build --lib std=../std -O2 -o memory tools/memory.sakura
|
||||
#! taskset -c 2 ./memory
|
||||
#!
|
||||
#! A `box` should cost no more than a tenth more than `raw_allocate` does.
|
||||
|
||||
import std::allocators;
|
||||
import std::io;
|
||||
import std::time;
|
||||
|
||||
const nodes: u64 = 1000000;
|
||||
const rounds: u64 = 10;
|
||||
|
||||
struct boxed {
|
||||
var value: u64;
|
||||
var next: maybe<box<boxed>>;
|
||||
}
|
||||
|
||||
struct raw {
|
||||
var value: u64;
|
||||
var next: *mut raw;
|
||||
}
|
||||
|
||||
# Nodes in a `box` each, owned by the node before, and taken out one by one from
|
||||
# the front, since closing the first would close the rest within it, a frame each.
|
||||
def with_boxes(): u64 {
|
||||
var sum: u64 = 0;
|
||||
for _ in 0..rounds {
|
||||
var front: maybe<box<boxed>> = nothing {};
|
||||
for i in 0..nodes {
|
||||
front = box::<boxed>::new(boxed { value = i, next = move(&mut front) });
|
||||
}
|
||||
var at: maybe<&boxed> = nothing {};
|
||||
if front is some(&first) {
|
||||
at = first.get();
|
||||
}
|
||||
while at is some(&each) {
|
||||
sum += each.value;
|
||||
at = nothing {};
|
||||
if each.next is some(&next) {
|
||||
at = next.get();
|
||||
}
|
||||
}
|
||||
while front is some(&mut first) {
|
||||
var node := first.take();
|
||||
front = move(&mut node.next);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
# Nodes `raw_allocate` gives, each released by itself.
|
||||
def with_blocks(): u64 {
|
||||
var sum: u64 = 0;
|
||||
for _ in 0..rounds {
|
||||
var front: *mut raw = null;
|
||||
for i in 0..nodes {
|
||||
val made := raw_allocate(size_of::<raw>()) as *mut raw;
|
||||
made[0] = raw { value = i, next = front };
|
||||
front = made;
|
||||
}
|
||||
var at := front;
|
||||
while at != null {
|
||||
sum += at[0].value;
|
||||
at = at[0].next;
|
||||
}
|
||||
while front != null {
|
||||
val next := front[0].next;
|
||||
raw_release(front as *mut u8);
|
||||
front = next;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
# Nodes of an arena, freed together as it resets.
|
||||
def with_an_arena(): u64 {
|
||||
var sum: u64 = 0;
|
||||
var a := arena::new();
|
||||
val alloc: maybe<&mut allocator> = &mut a;
|
||||
for _ in 0..rounds {
|
||||
var front: *mut raw = null;
|
||||
for i in 0..nodes {
|
||||
val made := acquire::<raw>(alloc, 1);
|
||||
made[0] = raw { value = i, next = front };
|
||||
front = made;
|
||||
}
|
||||
var at := front;
|
||||
while at != null {
|
||||
sum += at[0].value;
|
||||
at = at[0].next;
|
||||
}
|
||||
a.reset();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
# Nodes of a pool, each given back by itself.
|
||||
def with_a_pool(): u64 {
|
||||
var sum: u64 = 0;
|
||||
var p := pool::of::<raw>();
|
||||
for _ in 0..rounds {
|
||||
var front: *mut raw = null;
|
||||
for i in 0..nodes {
|
||||
front = p.make(raw { value = i, next = front });
|
||||
}
|
||||
var at := front;
|
||||
while at != null {
|
||||
sum += at[0].value;
|
||||
at = at[0].next;
|
||||
}
|
||||
while front != null {
|
||||
val next := front[0].next;
|
||||
p.unmake(front);
|
||||
front = next;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
# Runs a way of holding the nodes, printing how long it took.
|
||||
def timed(name: strview, way: def(): u64): f64 {
|
||||
val start := time::instant::now();
|
||||
val sum := way();
|
||||
val took := (time::instant::now() - start) / time::milliseconds(1);
|
||||
if sum != rounds * nodes * (nodes - 1) / 2 {
|
||||
println($"{name}: the sum is {sum}, which is wrong");
|
||||
}
|
||||
println($"{name}: {took} ms");
|
||||
return took;
|
||||
}
|
||||
|
||||
def main(): void {
|
||||
val boxes := timed("box, released one by one", with_boxes);
|
||||
val blocks := timed("raw_allocate and raw_release, one by one", with_blocks);
|
||||
_ = timed("an arena, reset once each time", with_an_arena);
|
||||
_ = timed("a pool, each given back by itself", with_a_pool);
|
||||
println($"box / raw_allocate: {boxes / blocks}");
|
||||
}
|
||||
Reference in new issue
Block a user