libfringe/benches/context_new.rs

42 lines
969 B
Rust
Raw Normal View History

2015-04-16 20:06:57 +08:00
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) edef <edef@edef.eu>
2015-04-16 18:08:44 +08:00
// See the LICENSE file included in this distribution.
2015-04-16 03:54:07 +08:00
#![feature(test)]
extern crate test;
2015-04-16 20:06:57 +08:00
extern crate fringe;
2016-03-22 14:35:25 +08:00
use fringe::Context;
2015-04-16 03:54:07 +08:00
static mut ctx_slot: *mut Context<'static, SliceStack<'static>> = 0 as *mut Context<_>;
static mut stack_buf: [u8; 1024] = [0; 1024];
2015-04-16 03:54:07 +08:00
#[bench]
fn context_new(b: &mut test::Bencher) {
b.iter(|| unsafe {
let stack = SliceStack(&mut stack_buf);
let mut ctx = Context::new(stack, move || {
2015-04-16 03:54:07 +08:00
let ctx_ptr = ctx_slot;
loop {
Context::swap(ctx_ptr, ctx_ptr);
2015-04-16 03:54:07 +08:00
}
});
ctx_slot = &mut ctx;
Context::swap(ctx_slot, ctx_slot);
2015-04-16 03:54:07 +08:00
})
}
struct SliceStack<'a>(&'a mut [u8]);
2015-04-16 20:06:57 +08:00
impl<'a> fringe::Stack for SliceStack<'a> {
fn top(&mut self) -> *mut u8 {
unsafe {
self.0.as_mut_ptr().offset(self.0.len() as isize)
}
}
fn limit(&self) -> *const u8 {
self.0.as_ptr()
}
}