|
| 1 | +use core::ffi::c_void; |
| 2 | +use std::mem::ManuallyDrop; |
| 3 | +use std::ptr::NonNull; |
| 4 | + |
| 5 | +use objc2::ffi; |
| 6 | +use objc2::rc::{autoreleasepool, Id, Shared}; |
| 7 | +use objc2::runtime::{Class, Object, Sel}; |
| 8 | +use objc2::{class, msg_send, sel}; |
| 9 | + |
| 10 | +#[cfg(apple)] |
| 11 | +#[link(name = "Foundation", kind = "framework")] |
| 12 | +extern "C" {} |
| 13 | + |
| 14 | +#[cfg(gnustep)] |
| 15 | +#[link(name = "gnustep-base", kind = "dylib")] |
| 16 | +extern "C" {} |
| 17 | + |
| 18 | +const BYTES: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 19 | + |
| 20 | +fn empty() { |
| 21 | + #[cfg(gnustep)] |
| 22 | + unsafe { |
| 23 | + objc2::__gnustep_hack::get_class_to_force_linkage() |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +fn pool_cleanup() { |
| 28 | + autoreleasepool(|_| {}) |
| 29 | +} |
| 30 | + |
| 31 | +fn class() -> &'static Class { |
| 32 | + class!(NSObject) |
| 33 | +} |
| 34 | + |
| 35 | +fn sel() -> Sel { |
| 36 | + sel!(alloc) |
| 37 | +} |
| 38 | + |
| 39 | +fn send_message() -> &'static Class { |
| 40 | + unsafe { msg_send![class!(NSObject), class] } |
| 41 | +} |
| 42 | + |
| 43 | +fn alloc_nsobject() -> *mut Object { |
| 44 | + unsafe { msg_send![class!(NSObject), alloc] } |
| 45 | +} |
| 46 | + |
| 47 | +fn new_nsobject() -> Id<Object, Shared> { |
| 48 | + let obj = alloc_nsobject(); |
| 49 | + let obj: *mut Object = unsafe { msg_send![obj, init] }; |
| 50 | + unsafe { Id::new(NonNull::new_unchecked(obj)) } |
| 51 | +} |
| 52 | + |
| 53 | +fn new_nsdata() -> Id<Object, Shared> { |
| 54 | + let bytes_ptr = BYTES.as_ptr() as *const c_void; |
| 55 | + let obj: *mut Object = unsafe { msg_send![class!(NSData), alloc] }; |
| 56 | + let obj: *mut Object = unsafe { |
| 57 | + msg_send![ |
| 58 | + obj, |
| 59 | + initWithBytes: bytes_ptr, |
| 60 | + length: BYTES.len(), |
| 61 | + ] |
| 62 | + }; |
| 63 | + unsafe { Id::new(NonNull::new_unchecked(obj)) } |
| 64 | +} |
| 65 | + |
| 66 | +fn new_leaked_nsdata() -> *mut Object { |
| 67 | + ManuallyDrop::new(new_nsdata()).as_ptr() |
| 68 | +} |
| 69 | + |
| 70 | +fn autoreleased_nsdata() -> *mut Object { |
| 71 | + // let bytes_ptr = BYTES.as_ptr() as *const c_void; |
| 72 | + // unsafe { |
| 73 | + // msg_send![ |
| 74 | + // class!(NSData), |
| 75 | + // dataWithBytes: bytes_ptr, |
| 76 | + // length: BYTES.len(), |
| 77 | + // ] |
| 78 | + // } |
| 79 | + unsafe { msg_send![new_leaked_nsdata(), autorelease] } |
| 80 | +} |
| 81 | + |
| 82 | +fn new_nsstring() -> Id<Object, Shared> { |
| 83 | + let obj: *mut Object = unsafe { msg_send![class!(NSString), alloc] }; |
| 84 | + let obj: *mut Object = unsafe { msg_send![obj, init] }; |
| 85 | + unsafe { Id::new(NonNull::new_unchecked(obj)) } |
| 86 | +} |
| 87 | + |
| 88 | +fn new_leaked_nsstring() -> *mut Object { |
| 89 | + ManuallyDrop::new(new_nsstring()).as_ptr() |
| 90 | +} |
| 91 | + |
| 92 | +fn autoreleased_nsstring() -> *mut Object { |
| 93 | + // unsafe { msg_send![class!(NSString), string] } |
| 94 | + unsafe { msg_send![new_leaked_nsstring(), autorelease] } |
| 95 | +} |
| 96 | + |
| 97 | +fn retain_autoreleased(obj: *mut Object) -> Id<Object, Shared> { |
| 98 | + let obj = unsafe { ffi::objc_retainAutoreleasedReturnValue(obj.cast()) }; |
| 99 | + unsafe { Id::new(NonNull::new_unchecked(obj).cast()) } |
| 100 | +} |
| 101 | + |
| 102 | +fn autoreleased_nsdata_pool_cleanup() -> *mut Object { |
| 103 | + autoreleasepool(|_| autoreleased_nsdata()) |
| 104 | +} |
| 105 | + |
| 106 | +fn autoreleased_nsdata_fast_caller_cleanup() -> Id<Object, Shared> { |
| 107 | + retain_autoreleased(autoreleased_nsdata()) |
| 108 | +} |
| 109 | + |
| 110 | +fn autoreleased_nsdata_fast_caller_cleanup_pool_cleanup() -> Id<Object, Shared> { |
| 111 | + autoreleasepool(|_| retain_autoreleased(autoreleased_nsdata())) |
| 112 | +} |
| 113 | + |
| 114 | +fn autoreleased_nsstring_pool_cleanup() -> *mut Object { |
| 115 | + autoreleasepool(|_| autoreleased_nsstring()) |
| 116 | +} |
| 117 | + |
| 118 | +fn autoreleased_nsstring_fast_caller_cleanup() -> Id<Object, Shared> { |
| 119 | + retain_autoreleased(autoreleased_nsstring()) |
| 120 | +} |
| 121 | + |
| 122 | +fn autoreleased_nsstring_fast_caller_cleanup_pool_cleanup() -> Id<Object, Shared> { |
| 123 | + autoreleasepool(|_| retain_autoreleased(autoreleased_nsstring())) |
| 124 | +} |
| 125 | + |
| 126 | +macro_rules! main_with_warmup { |
| 127 | + ($($f:ident,)+) => { |
| 128 | + mod warmup_fns { |
| 129 | + $( |
| 130 | + #[inline(never)] |
| 131 | + pub fn $f() { |
| 132 | + let _ = iai::black_box(super::$f()); |
| 133 | + } |
| 134 | + )+ |
| 135 | + } |
| 136 | + |
| 137 | + fn warmup() { |
| 138 | + $( |
| 139 | + warmup_fns::$f(); |
| 140 | + )+ |
| 141 | + } |
| 142 | + |
| 143 | + iai::main! { |
| 144 | + warmup, |
| 145 | + $( |
| 146 | + $f, |
| 147 | + )+ |
| 148 | + } |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +main_with_warmup! { |
| 153 | + // Baseline |
| 154 | + empty, |
| 155 | + pool_cleanup, |
| 156 | + class, |
| 157 | + sel, |
| 158 | + send_message, |
| 159 | + alloc_nsobject, |
| 160 | + new_nsobject, |
| 161 | + // NSData |
| 162 | + new_nsdata, |
| 163 | + new_leaked_nsdata, |
| 164 | + autoreleased_nsdata, |
| 165 | + autoreleased_nsdata_pool_cleanup, |
| 166 | + autoreleased_nsdata_fast_caller_cleanup, |
| 167 | + autoreleased_nsdata_fast_caller_cleanup_pool_cleanup, |
| 168 | + // NSString |
| 169 | + new_nsstring, |
| 170 | + new_leaked_nsstring, |
| 171 | + autoreleased_nsstring, |
| 172 | + autoreleased_nsstring_pool_cleanup, |
| 173 | + autoreleased_nsstring_fast_caller_cleanup, |
| 174 | + autoreleased_nsstring_fast_caller_cleanup_pool_cleanup, |
| 175 | +} |
0 commit comments