Skip to content

Commit

Permalink
feat: Default-calculate getSizeOf(...) so the user doesn't have to (#…
Browse files Browse the repository at this point in the history
…429)

* feat: Default-calculate `getSizeOf(...)` so the user doesn't have to

* Update MemoryHelper.swift

* fix: Add `getSizeOf` back but deprecate it
  • Loading branch information
mrousavy authored Dec 17, 2024
1 parent 70116b5 commit 5b510b8
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/docs/hybrid-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class HybridImage : HybridImageSpec {
private var cgImage: CGImage
public var memorySize: Int {
let imageSize = cgImage.width * cgImage.height * cgImage.bytesPerPixel
return getSizeOf(self) + imageSize
return imageSize
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/performance-tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class HybridImage : HybridImageSpec {
private var cgImage: CGImage
public var memorySize: Int {
let imageSize = cgImage.width * cgImage.height * cgImage.bytesPerPixel
return getSizeOf(self) + imageSize
return imageSize
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/nitrogen/src/syntax/swift/SwiftHybridObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ${hasBaseClass ? 'override func' : 'func'} getCxxWrapper() -> ${name.Hybr
if (!hasBaseClass) {
// It doesn't have a base class - implement the `HybridObjectSpec` base protocol
classBaseClasses.push('HybridObjectSpec')
baseMembers.push(`public var memorySize: Int { return getSizeOf(self) }`)
baseMembers.push(`public var memorySize: Int { return 0 }`)
}

const protocolCode = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ ${hasBase ? `public class ${name.HybridTSpecCxx} : ${baseClasses.join(', ')}` :
*/
@inline(__always)
public ${hasBase ? 'override var' : 'var'} memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}
// Properties
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-image/ios/HybridImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HybridImage : HybridImageSpec {
* can efficiently garbage collect it when needed.
*/
public override var memorySize: Int {
return getSizeOf(self) + uiImage.memorySize
return uiImage.memorySize
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class HybridBaseSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return getSizeOf(self) }
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class HybridBaseSpec_cxx {
*/
@inline(__always)
public var memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}

// Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class HybridChildSpec_cxx : HybridBaseSpec_cxx {
*/
@inline(__always)
public override var memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}

// Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HybridImageFactorySpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return getSizeOf(self) }
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class HybridImageFactorySpec_cxx {
*/
@inline(__always)
public var memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}

// Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HybridImageSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return getSizeOf(self) }
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class HybridImageSpec_cxx {
*/
@inline(__always)
public var memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}

// Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class HybridTestObjectSwiftKotlinSpec_base: HybridObjectSpec {
return cxxWrapper
}
}
public var memorySize: Int { return getSizeOf(self) }
public var memorySize: Int { return 0 }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class HybridTestObjectSwiftKotlinSpec_cxx {
*/
@inline(__always)
public var memorySize: Int {
return self.__implementation.memorySize
return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
}

// Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class HybridObject: ExtendableHybridClass {
* val memorySize: ULong
* get() {
* val imageSize = this.bitmap.bytesPerRow * this.bitmap.height
* return getSizeOf(this) + imageSize
* return imageSize
* }
* ```
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ public protocol HybridObjectSpec: AnyObject {
* ```swift
* var memorySize: Int {
* let imageSize = self.uiImage.bytesPerRow * self.uiImage.height
* return getSizeOf(self) + imageSize
* return imageSize
* }
* ```
*/
var memorySize: Int { get }
}

public extension HybridObjectSpec {
/**
* Get the memory size of the given instance.
* This only accounts for stack allocated member variables,
* not for externally allocated heap allocations like images or buffers.
*/
@available(*, deprecated, message: "getSizeOf(...) will now be default-computed. Please remove getSizeOf() from your code.")
func getSizeOf<T: AnyObject>(_ instance: T) -> Int {
return malloc_size(Unmanaged.passUnretained(instance).toOpaque())
return 0
}
}
21 changes: 21 additions & 0 deletions packages/react-native-nitro-modules/ios/utils/MemoryHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// MemoryHelper.swift
// NitroModules
//
// Created by Marc Rousavy on 17.12.2024.
//

import Foundation

public final class MemoryHelper {
/**
* Get the amount of memory that was allocated using a `malloc`-like allocator
* for the given instance, in bytes.
* When allocating resources differently (e.g. GPU buffers, or `UIImage`) you
* should add their byte sizes to the result of this function to get an object's
* total memory footprint.
*/
public static func getSizeOf(_ instance: AnyObject) -> Int {
return malloc_size(Unmanaged.passUnretained(instance).toOpaque())
}
}

0 comments on commit 5b510b8

Please sign in to comment.