Skip to content

Commit

Permalink
perf: Use & for getSwiftPart()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 15, 2025
1 parent ed79295 commit 38e4fe5
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/nitrogen/src/syntax/swift/SwiftCxxTypeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void* _Nonnull get_${name}(${name} cppType) {
throw std::runtime_error("Class \\"${HybridTSpec}\\" is not implemented in Swift!");
}
#endif
${swiftPartType} swiftPart = swiftWrapper->getSwiftPart();
${swiftPartType}& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}
`.trim(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ namespace ${cxxNamespace} {
public:
// Get the Swift part
inline ${iosModuleName}::${name.HybridTSpecCxx} getSwiftPart() noexcept { return _swiftPart; }
inline ${iosModuleName}::${name.HybridTSpecCxx}& getSwiftPart() noexcept {
return _swiftPart;
}
public:
// Get memory pressure
Expand Down
21 changes: 17 additions & 4 deletions packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
createViewComponentShadowNodeFiles,
getViewComponentNames,
} from '../ViewComponentShadowNode.js'
import { createFileMetadataString } from '../../syntax/helpers.js'
import {
createFileMetadataString,
escapeCppName,
} from '../../syntax/helpers.js'
import { NitroConfig } from '../../config/NitroConfig.js'
import { getUmbrellaHeaderName } from '../../autolinking/ios/createSwiftUmbrellaHeader.js'
import { getHybridObjectName } from '../../syntax/getHybridObjectName.js'
import { getHybridObjectConstructorCall } from '../../syntax/swift/SwiftHybridObjectRegistration.js'
import { indent } from '../../utils.js'

export function createSwiftHybridViewManager(
spec: HybridObjectSpec
Expand All @@ -29,6 +33,11 @@ export function createSwiftHybridViewManager(
)
}

const propAssignments = spec.properties.map(
(p) =>
`swiftPart.${p.cppSetterName}(newViewProps.${escapeCppName(p.name)});`
)

const mmFile = `
${createFileMetadataString(`${component}.mm`)}
Expand Down Expand Up @@ -78,16 +87,20 @@ using namespace ${namespace}::views;
}
- (void) updateView {
${swiftNamespace}::${HybridTSpecCxx} swiftPart = _hybridView->getSwiftPart();
${swiftNamespace}::${HybridTSpecCxx}& swiftPart = _hybridView->getSwiftPart();
void* viewUnsafe = swiftPart.getView();
UIView* view = (__bridge_transfer UIView*) viewUnsafe;
[self setContentView:view];
}
- (void) updateProps:(const react::Props::Shared&)props
oldProps:(const react::Props::Shared&)oldProps {
// TODO: const auto& newViewProps = *std::static_pointer_cast<${propsClassName} const>(props);
// 1. Downcast props
const auto& newViewProps = *std::static_pointer_cast<${propsClassName} const>(props);
${swiftNamespace}::${HybridTSpecCxx}& swiftPart = _hybridView->getSwiftPart();
// 2. Update each prop
${indent(propAssignments.join('\n'), ' ')}
// 3. Continue in base class
[super updateProps:props oldProps:oldProps];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridImageSpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridImageSpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridImageSpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand All @@ -54,7 +54,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridImageFactorySpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridImageFactorySpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridImageFactorySpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand All @@ -70,7 +70,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridTestObjectSwiftKotlinSpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridTestObjectSwiftKotlinSpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridTestObjectSwiftKotlinSpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand Down Expand Up @@ -202,7 +202,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridBaseSpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridBaseSpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridBaseSpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand All @@ -218,7 +218,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridChildSpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridChildSpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridChildSpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand All @@ -234,7 +234,7 @@ namespace margelo::nitro::image::bridge::swift {
throw std::runtime_error("Class \"HybridTestViewSpec\" is not implemented in Swift!");
}
#endif
NitroImage::HybridTestViewSpec_cxx swiftPart = swiftWrapper->getSwiftPart();
NitroImage::HybridTestViewSpec_cxx& swiftPart = swiftWrapper->getSwiftPart();
return swiftPart.toUnsafe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridBaseSpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridBaseSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridChildSpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridChildSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridImageFactorySpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridImageFactorySpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridImageSpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridImageSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridTestObjectSwiftKotlinSpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridTestObjectSwiftKotlinSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace margelo::nitro::image {

public:
// Get the Swift part
inline NitroImage::HybridTestViewSpec_cxx getSwiftPart() noexcept { return _swiftPart; }
inline NitroImage::HybridTestViewSpec_cxx& getSwiftPart() noexcept {
return _swiftPart;
}

public:
// Get memory pressure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,21 @@ - (instancetype) init {
}

- (void) updateView {
NitroImage::HybridTestViewSpec_cxx swiftPart = _hybridView->getSwiftPart();
NitroImage::HybridTestViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();
void* viewUnsafe = swiftPart.getView();
UIView* view = (__bridge_transfer UIView*) viewUnsafe;
[self setContentView:view];
}

- (void) updateProps:(const react::Props::Shared&)props
oldProps:(const react::Props::Shared&)oldProps {
// TODO: const auto& newViewProps = *std::static_pointer_cast<HybridTestViewProps const>(props);

// 1. Downcast props
const auto& newViewProps = *std::static_pointer_cast<HybridTestViewProps const>(props);
NitroImage::HybridTestViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();
// 2. Update each prop
swiftPart.setSomeProp(newViewProps.someProp);
swiftPart.setSomeCallback(newViewProps.someCallback);
// 3. Continue in base class
[super updateProps:props oldProps:oldProps];
}

Expand Down

0 comments on commit 38e4fe5

Please sign in to comment.