diff --git a/src/libsync/one.rs b/src/libsync/one.rs index 7da6f39b840e5..388c63becbfd2 100644 --- a/src/libsync/one.rs +++ b/src/libsync/one.rs @@ -63,7 +63,17 @@ impl Once { /// /// When this function returns, it is guaranteed that some initialization /// has run and completed (it may not be the closure specified). + #[inline(always)] pub fn doit(&self, f: ||) { + // Optimize common path: load is much cheaper than fetch_add. + if self.cnt.load(atomics::SeqCst) < 0 { + return + } + + self.doit_slow(f); + } + + fn doit_slow(&self, f: ||) { // Implementation-wise, this would seem like a fairly trivial primitive. // The stickler part is where our mutexes currently require an // allocation, and usage of a `Once` should't leak this allocation.