2015-04-16 03:54:07 +08:00
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
|
|
|
extern crate lwkt;
|
2015-04-16 10:01:08 +08:00
|
|
|
use lwkt::{Context, Stack};
|
2015-04-16 03:54:07 +08:00
|
|
|
|
2015-04-16 10:01:08 +08:00
|
|
|
static mut ctx_slot: *mut Context<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 {
|
2015-04-16 10:01:08 +08:00
|
|
|
let stack = SliceStack(&mut stack_buf);
|
2015-04-16 04:25:09 +08:00
|
|
|
|
|
|
|
let mut ctx = Context::new(stack, move || {
|
2015-04-16 03:54:07 +08:00
|
|
|
let ctx_ptr = ctx_slot;
|
|
|
|
loop {
|
|
|
|
(*ctx_ptr).swap()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx_slot = &mut ctx;
|
|
|
|
|
|
|
|
ctx.swap();
|
|
|
|
})
|
|
|
|
}
|
2015-04-16 10:01:08 +08:00
|
|
|
|
|
|
|
struct SliceStack<'a>(&'a mut [u8]);
|
|
|
|
impl<'a> lwkt::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()
|
|
|
|
}
|
|
|
|
}
|