-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from madsmtm/criterion-benchmarking
Start benchmarking
- Loading branch information
Showing
7 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use core::ffi::c_void; | ||
use std::mem::ManuallyDrop; | ||
use std::ptr::NonNull; | ||
|
||
use objc2::ffi; | ||
use objc2::rc::{autoreleasepool, Id, Shared}; | ||
use objc2::runtime::{Class, Object, Sel}; | ||
use objc2::{class, msg_send, sel}; | ||
|
||
#[cfg(apple)] | ||
#[link(name = "Foundation", kind = "framework")] | ||
extern "C" {} | ||
|
||
#[cfg(gnustep)] | ||
#[link(name = "gnustep-base", kind = "dylib")] | ||
extern "C" {} | ||
|
||
const BYTES: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
|
||
fn empty() { | ||
#[cfg(gnustep)] | ||
unsafe { | ||
objc2::__gnustep_hack::get_class_to_force_linkage() | ||
}; | ||
} | ||
|
||
fn pool_cleanup() { | ||
autoreleasepool(|_| {}) | ||
} | ||
|
||
fn class() -> &'static Class { | ||
class!(NSObject) | ||
} | ||
|
||
fn sel() -> Sel { | ||
sel!(alloc) | ||
} | ||
|
||
fn send_message() -> &'static Class { | ||
unsafe { msg_send![class!(NSObject), class] } | ||
} | ||
|
||
fn alloc_nsobject() -> *mut Object { | ||
unsafe { msg_send![class!(NSObject), alloc] } | ||
} | ||
|
||
fn new_nsobject() -> Id<Object, Shared> { | ||
let obj = alloc_nsobject(); | ||
let obj: *mut Object = unsafe { msg_send![obj, init] }; | ||
unsafe { Id::new(NonNull::new_unchecked(obj)) } | ||
} | ||
|
||
fn new_nsdata() -> Id<Object, Shared> { | ||
let bytes_ptr = BYTES.as_ptr() as *const c_void; | ||
let obj: *mut Object = unsafe { msg_send![class!(NSData), alloc] }; | ||
let obj: *mut Object = unsafe { | ||
msg_send![ | ||
obj, | ||
initWithBytes: bytes_ptr, | ||
length: BYTES.len(), | ||
] | ||
}; | ||
unsafe { Id::new(NonNull::new_unchecked(obj)) } | ||
} | ||
|
||
fn new_leaked_nsdata() -> *mut Object { | ||
ManuallyDrop::new(new_nsdata()).as_ptr() | ||
} | ||
|
||
fn autoreleased_nsdata() -> *mut Object { | ||
// let bytes_ptr = BYTES.as_ptr() as *const c_void; | ||
// unsafe { | ||
// msg_send![ | ||
// class!(NSData), | ||
// dataWithBytes: bytes_ptr, | ||
// length: BYTES.len(), | ||
// ] | ||
// } | ||
unsafe { msg_send![new_leaked_nsdata(), autorelease] } | ||
} | ||
|
||
fn new_nsstring() -> Id<Object, Shared> { | ||
let obj: *mut Object = unsafe { msg_send![class!(NSString), alloc] }; | ||
let obj: *mut Object = unsafe { msg_send![obj, init] }; | ||
unsafe { Id::new(NonNull::new_unchecked(obj)) } | ||
} | ||
|
||
fn new_leaked_nsstring() -> *mut Object { | ||
ManuallyDrop::new(new_nsstring()).as_ptr() | ||
} | ||
|
||
fn autoreleased_nsstring() -> *mut Object { | ||
// unsafe { msg_send![class!(NSString), string] } | ||
unsafe { msg_send![new_leaked_nsstring(), autorelease] } | ||
} | ||
|
||
fn retain_autoreleased(obj: *mut Object) -> Id<Object, Shared> { | ||
let obj = unsafe { ffi::objc_retainAutoreleasedReturnValue(obj.cast()) }; | ||
unsafe { Id::new(NonNull::new_unchecked(obj).cast()) } | ||
} | ||
|
||
fn autoreleased_nsdata_pool_cleanup() -> *mut Object { | ||
autoreleasepool(|_| autoreleased_nsdata()) | ||
} | ||
|
||
fn autoreleased_nsdata_fast_caller_cleanup() -> Id<Object, Shared> { | ||
retain_autoreleased(autoreleased_nsdata()) | ||
} | ||
|
||
fn autoreleased_nsdata_fast_caller_cleanup_pool_cleanup() -> Id<Object, Shared> { | ||
autoreleasepool(|_| retain_autoreleased(autoreleased_nsdata())) | ||
} | ||
|
||
fn autoreleased_nsstring_pool_cleanup() -> *mut Object { | ||
autoreleasepool(|_| autoreleased_nsstring()) | ||
} | ||
|
||
fn autoreleased_nsstring_fast_caller_cleanup() -> Id<Object, Shared> { | ||
retain_autoreleased(autoreleased_nsstring()) | ||
} | ||
|
||
fn autoreleased_nsstring_fast_caller_cleanup_pool_cleanup() -> Id<Object, Shared> { | ||
autoreleasepool(|_| retain_autoreleased(autoreleased_nsstring())) | ||
} | ||
|
||
macro_rules! main_with_warmup { | ||
($($f:ident,)+) => { | ||
mod warmup_fns { | ||
$( | ||
#[inline(never)] | ||
pub fn $f() { | ||
let _ = iai::black_box(super::$f()); | ||
} | ||
)+ | ||
} | ||
|
||
fn warmup() { | ||
$( | ||
warmup_fns::$f(); | ||
)+ | ||
} | ||
|
||
iai::main! { | ||
warmup, | ||
$( | ||
$f, | ||
)+ | ||
} | ||
}; | ||
} | ||
|
||
main_with_warmup! { | ||
// Baseline | ||
empty, | ||
pool_cleanup, | ||
class, | ||
sel, | ||
send_message, | ||
alloc_nsobject, | ||
new_nsobject, | ||
// NSData | ||
new_nsdata, | ||
new_leaked_nsdata, | ||
autoreleased_nsdata, | ||
autoreleased_nsdata_pool_cleanup, | ||
autoreleased_nsdata_fast_caller_cleanup, | ||
autoreleased_nsdata_fast_caller_cleanup_pool_cleanup, | ||
// NSString | ||
new_nsstring, | ||
new_leaked_nsstring, | ||
autoreleased_nsstring, | ||
autoreleased_nsstring_pool_cleanup, | ||
autoreleased_nsstring_fast_caller_cleanup, | ||
autoreleased_nsstring_fast_caller_cleanup_pool_cleanup, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters