Skip to content

Commit

Permalink
Cleaned up locks and fixed a footprint change bug
Browse files Browse the repository at this point in the history
  • Loading branch information
naftaly committed Nov 8, 2024
1 parent 951293e commit 45d3cf3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
36 changes: 15 additions & 21 deletions Sources/Footprint/Footprint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class Footprint: @unchecked Sendable {
public struct Memory {

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

/// Everything is good, no need to worry.
case normal
Expand All @@ -57,7 +57,10 @@ public final class Footprint: @unchecked Sendable {
/// memory usage behavior.
/// Please revisit memory best practices and profile your app.
case terminal


public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
}
/// Init from String value
public init?(_ value: String) {
for c in Self.allCases {
Expand Down Expand Up @@ -153,9 +156,7 @@ public final class Footprint: @unchecked Sendable {

/// Returns a copy of the current memory structure.
public var memory: Memory {
_memoryLock.lock()
defer { _memoryLock.unlock() }
return _memory
_memoryLock.withLock { _memory }
}

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

/// The currently tracked memory state.
public var state: Memory.State {
_memoryLock.lock()
defer { _memoryLock.unlock() }
return _memory.state
_memoryLock.withLock { _memory.state }
}

/// The currently tracked memory pressure.
public var pressure: Memory.State {
_memoryLock.lock()
defer { _memoryLock.unlock() }
return _memory.pressure
_memoryLock.withLock { _memory.pressure }
}

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

internal func observe(_ action: @escaping (Memory) -> Void) {
_memoryLock.lock()
defer { _memoryLock.unlock() }
_observers.append(action)
let mem = _memory

public func observe(_ action: @escaping (Memory) -> Void) {
let mem = _memoryLock.withLock {
_observers.append(action)
return _memory
}
DispatchQueue.global().async {
action(mem)
}
Expand All @@ -269,7 +265,7 @@ public final class Footprint: @unchecked Sendable {
}
// memory used changes only on ~1MB intevals
// that's enough precision
if _memory.used - memory.used > 1000 {
if abs(_memory.used - memory.used) > 1000000 {
changeSet.insert(.footprint)
}
guard !changeSet.isEmpty else {
Expand Down Expand Up @@ -317,9 +313,7 @@ public final class Footprint: @unchecked Sendable {
if changeSet.contains(.footprint) {
// copy behind the lock
// deploy outside the lock
_memoryLock.lock()
let observers = _observers
_memoryLock.unlock()
let observers = _memoryLock.withLock { _observers }
observers.forEach { $0(memory) }
}
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/FootprintTests/FootprintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class FootprintTests: XCTestCase {
XCTAssertGreaterThan(mem.limit, 0)
}

func testName() {
XCTAssertEqual("\(Footprint.Memory.State.normal)", "normal")
}
}

0 comments on commit 45d3cf3

Please sign in to comment.