Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize `Once::doit`: perform optimistic check that initializtion is already completed. `load` is much cheaper than `fetch_add` at least on x86_64. Verified with this test: ``` static mut o: one::Once = one::ONCE_INIT; unsafe { loop { let start = time::precise_time_ns(); let iters = 50000000u64; for _ in range(0, iters) { o.doit(|| { println!("once!"); }); } let end = time::precise_time_ns(); let ps_per_iter = 1000 * (end - start) / iters; println!("{} ps per iter", ps_per_iter); // confuse the optimizer o.doit(|| { println!("once!"); }); } } ``` Test executed on Mac, Intel Core i7 2GHz. Result is: * 20ns per iteration without patch * 4ns per iteration with this patch applied Once.doit could be even faster (800ps per iteration), if `doit` function was split into a pair of `doit`/`doit_slow`, and `doit` marked as `#[inline]` like this: ``` #[inline(always)] pub fn doit(&self, f: ||) { if self.cnt.load(atomics::SeqCst) < 0 { return } self.doit_slow(f); } fn doit_slow(&self, f: ||) { ... } ```
- Loading branch information
f853cf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw approval from alexcrichton
at stepancheg@f853cf7
f853cf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging stepancheg/rust/once = f853cf7 into auto
f853cf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stepancheg/rust/once = f853cf7 merged ok, testing candidate = f2c4c88
f853cf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests pass:
success: http://buildbot.rust-lang.org/builders/auto-mac-32-opt/builds/5790
success: http://buildbot.rust-lang.org/builders/auto-mac-64-opt/builds/5788
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-c/builds/4883
success: http://buildbot.rust-lang.org/builders/auto-mac-64-nopt-t/builds/4892
success: http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/5887
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-c/builds/4976
success: http://buildbot.rust-lang.org/builders/auto-linux-32-nopt-t/builds/4984
success: http://buildbot.rust-lang.org/builders/auto-linux-64-opt/builds/5890
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-c/builds/4975
success: http://buildbot.rust-lang.org/builders/auto-linux-64-nopt-t/builds/4981
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android/builds/5043
success: http://buildbot.rust-lang.org/builders/auto-linux-64-x-android-t/builds/2776
success: http://buildbot.rust-lang.org/builders/auto-win-32-opt/builds/5883
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-c/builds/4979
success: http://buildbot.rust-lang.org/builders/auto-win-32-nopt-t/builds/4994
f853cf7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fast-forwarding master to auto = f2c4c88