sos-os/kernel

Benchmark our powers-of-two impl against libcore's

Closed this issue · 1 comments

hawkw commented

Our memory allocator uses a custom-written math module for calculating powers of two. This code was written before I was aware that powers of two functions exist in the standard library.

I'd like to refactor the memory allocator just to use the standard library functions, to avoid duplicating this functionality. However, if our implementation, which is different from the stdlib's, offers better performance, I'd like to prefer using it, since the allocator is a performance-critical component.

I should write a handful of benchmarks to determine which is faster.

hawkw commented

Okay, here are some bench results:

Discovery-One :: Development/sos-kernel/alloc ‹±paging-dev↑1●› » cargo bench --features bench                                                                                                                                                                      [10:18:23]
   Compiling alloc v0.1.0 (file:///Users/eliza/Development/sos-kernel/alloc)
warning: unused import: `core::ptr`, #[warn(unused_imports)] on by default
 --> src/buddy/test.rs:5:5
  |
5 | use core::ptr;
  |     ^^^^^^^^^

    Finished release [optimized] target(s) in 2.84 secs
     Running target/release/deps/alloc-169c74eae7b6b9ac

running 10 tests
test buddy::math::tests::test_is_pow2 ... ignored
test buddy::math::tests::test_log2 ... ignored
test buddy::math::tests::test_next_pow2 ... ignored
test buddy::test::test_alloc_and_dealloc ... ignored
test buddy::test::test_allocation_size_and_order ... ignored
test buddy::test::test_get_buddy ... ignored
test buddy::math::tests::our_is_pow2           ... bench:      56,283 ns/iter (+/- 12,009)
test buddy::math::tests::our_next_pow2         ... bench:     196,357 ns/iter (+/- 92,106)
test buddy::math::tests::std_is_power_of_two   ... bench:     111,829 ns/iter (+/- 19,359)
test buddy::math::tests::std_next_power_of_two ... bench:     110,760 ns/iter (+/- 21,945)

test result: ok. 0 passed; 0 failed; 6 ignored; 4 measured

Discovery-One :: Development/sos-kernel/alloc ‹±paging-dev↑1●› »                

It looks like the standard lib's next_power_of_two() is faster than ours (unsurprising, IIRC it uses intrinsics), but our is_pow2() is faster than the standard library's is_power_of_two().