From 0853dc391137c1f5f627a29016155a00a9864e44 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Fri, 26 Jan 2018 09:11:59 +0000 Subject: [PATCH] stop atomics from allocating on every operation --- Sources/NIOConcurrencyHelpers/atomics.swift | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Sources/NIOConcurrencyHelpers/atomics.swift b/Sources/NIOConcurrencyHelpers/atomics.swift index 991730902..ef8e9893b 100644 --- a/Sources/NIOConcurrencyHelpers/atomics.swift +++ b/Sources/NIOConcurrencyHelpers/atomics.swift @@ -36,6 +36,13 @@ public final class Atomic { private let value: OpaquePointer /// Create an atomic object with `value`. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public init(value: T) { self.value = T.atomic_create(value) } @@ -55,6 +62,13 @@ public final class Atomic { /// succeeds. /// - Returns: `True` if the exchange occurred, or `False` if `expected` did not /// match the current value and so no exchange occurred. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func compareAndExchange(expected: T, desired: T) -> Bool { return T.atomic_compare_and_exchange(self.value, expected, desired) } @@ -67,6 +81,13 @@ public final class Atomic { /// /// - Parameter rhs: The value to add to this object. /// - Returns: The previous value of this object, before the addition occurred. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func add(_ rhs: T) -> T { return T.atomic_add(self.value, rhs) } @@ -79,6 +100,13 @@ public final class Atomic { /// /// - Parameter rhs: The value to subtract from this object. /// - Returns: The previous value of this object, before the subtraction occurred. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func sub(_ rhs: T) -> T { return T.atomic_sub(self.value, rhs) } @@ -91,6 +119,13 @@ public final class Atomic { /// /// - Parameter value: The new value to set this object to. /// - Returns: The value previously held by this object. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func exchange(with value: T) -> T { return T.atomic_exchange(self.value, value) } @@ -102,6 +137,13 @@ public final class Atomic { /// event will be ordered before or after this one. /// /// - Returns: The value of this object + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func load() -> T { return T.atomic_load(self.value) } @@ -113,6 +155,13 @@ public final class Atomic { /// event will be ordered before or after this one. /// /// - Parameter value: The new value to set the object to. + @_specialize(where T == Int) + @_specialize(where T == Bool) + @_specialize(where T == UInt64) + // in Swift 4.0 (fixed in 4.0.2), there was a crash that only allowed three specialisations otherwise the compiler would crash + // FIXME: Bring back when we require Swift >= 4.0.2 + // @_specialize(where T == UInt) + // @_specialize(where T == Int64) public func store(_ value: T) -> Void { T.atomic_store(self.value, value) }