Skip to content

Commit 45d3cf3

Browse files
committed
Cleaned up locks and fixed a footprint change bug
1 parent 951293e commit 45d3cf3

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

Sources/Footprint/Footprint.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final class Footprint: @unchecked Sendable {
3939
public struct Memory {
4040

4141
/// State describes how close to app termination your app is based on memory.
42-
public enum State: Comparable, CaseIterable {
42+
public enum State: Int, Comparable, CaseIterable {
4343

4444
/// Everything is good, no need to worry.
4545
case normal
@@ -57,7 +57,10 @@ public final class Footprint: @unchecked Sendable {
5757
/// memory usage behavior.
5858
/// Please revisit memory best practices and profile your app.
5959
case terminal
60-
60+
61+
public static func < (lhs: Self, rhs: Self) -> Bool {
62+
lhs.rawValue < rhs.rawValue
63+
}
6164
/// Init from String value
6265
public init?(_ value: String) {
6366
for c in Self.allCases {
@@ -153,9 +156,7 @@ public final class Footprint: @unchecked Sendable {
153156

154157
/// Returns a copy of the current memory structure.
155158
public var memory: Memory {
156-
_memoryLock.lock()
157-
defer { _memoryLock.unlock() }
158-
return _memory
159+
_memoryLock.withLock { _memory }
159160
}
160161

161162
/// Based on the current memory footprint, tells you if you should be able to allocate
@@ -170,16 +171,12 @@ public final class Footprint: @unchecked Sendable {
170171

171172
/// The currently tracked memory state.
172173
public var state: Memory.State {
173-
_memoryLock.lock()
174-
defer { _memoryLock.unlock() }
175-
return _memory.state
174+
_memoryLock.withLock { _memory.state }
176175
}
177176

178177
/// The currently tracked memory pressure.
179178
public var pressure: Memory.State {
180-
_memoryLock.lock()
181-
defer { _memoryLock.unlock() }
182-
return _memory.pressure
179+
_memoryLock.withLock { _memory.pressure }
183180
}
184181

185182
private init(_ provider: MemoryProvider = DefaultMemoryProvider()) {
@@ -240,12 +237,11 @@ public final class Footprint: @unchecked Sendable {
240237
return .normal
241238
}
242239

243-
internal func observe(_ action: @escaping (Memory) -> Void) {
244-
_memoryLock.lock()
245-
defer { _memoryLock.unlock() }
246-
_observers.append(action)
247-
let mem = _memory
248-
240+
public func observe(_ action: @escaping (Memory) -> Void) {
241+
let mem = _memoryLock.withLock {
242+
_observers.append(action)
243+
return _memory
244+
}
249245
DispatchQueue.global().async {
250246
action(mem)
251247
}
@@ -269,7 +265,7 @@ public final class Footprint: @unchecked Sendable {
269265
}
270266
// memory used changes only on ~1MB intevals
271267
// that's enough precision
272-
if _memory.used - memory.used > 1000 {
268+
if abs(_memory.used - memory.used) > 1000000 {
273269
changeSet.insert(.footprint)
274270
}
275271
guard !changeSet.isEmpty else {
@@ -317,9 +313,7 @@ public final class Footprint: @unchecked Sendable {
317313
if changeSet.contains(.footprint) {
318314
// copy behind the lock
319315
// deploy outside the lock
320-
_memoryLock.lock()
321-
let observers = _observers
322-
_memoryLock.unlock()
316+
let observers = _memoryLock.withLock { _observers }
323317
observers.forEach { $0(memory) }
324318
}
325319
}

Tests/FootprintTests/FootprintTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ class FootprintTests: XCTestCase {
99
XCTAssertGreaterThan(mem.limit, 0)
1010
}
1111

12+
func testName() {
13+
XCTAssertEqual("\(Footprint.Memory.State.normal)", "normal")
14+
}
1215
}

0 commit comments

Comments
 (0)