Skip to content

[Runtime] Improve performance and memory footprint of compatibility o… #78818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions stdlib/public/CompatibilityOverride/CompatibilityOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
#define COMPATIBILITY_OVERRIDE_H

#include "../runtime/Private.h"
#include "swift/Runtime/CMakeConfig.h"
#include "swift/Runtime/Concurrency.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/Once.h"
#include "swift/Runtime/CMakeConfig.h"
#include <atomic>
#include <type_traits>

namespace swift {
Expand Down Expand Up @@ -192,15 +192,38 @@ namespace swift {
/// functionality must be available as swift_funcNameHereImpl.
#define COMPATIBILITY_OVERRIDE(name, ret, attrs, ccAttrs, namespace, \
typedArgs, namedArgs) \
/* We are creating this separate function for the override case, */ \
/* to prevent a stack frame from being created for the default case. */ \
SWIFT_NOINLINE \
static ret swift_##name##Slow(COMPATIBILITY_UNPAREN_WITH_COMMA(typedArgs) \
std::atomic<uintptr_t> &Override, \
uintptr_t fn, Original_##name defaultImpl) { \
constexpr uintptr_t DEFAULT_IMPL_SENTINEL = 0x1; \
if (SWIFT_UNLIKELY(fn == 0x0)) { \
fn = (uintptr_t)getOverride_##name(); \
if (fn == 0x0) { \
Override.store(DEFAULT_IMPL_SENTINEL, \
std::memory_order::memory_order_relaxed); \
return defaultImpl COMPATIBILITY_PAREN(namedArgs); \
} \
Override.store(fn, std::memory_order::memory_order_relaxed); \
} \
return ((Override_##name)fn)(COMPATIBILITY_UNPAREN_WITH_COMMA(namedArgs) \
defaultImpl); \
} \
attrs ccAttrs ret namespace swift_##name COMPATIBILITY_PAREN(typedArgs) { \
static Override_##name Override; \
static swift_once_t Predicate; \
swift_once( \
&Predicate, [](void *) { Override = getOverride_##name(); }, nullptr); \
if (Override != nullptr) \
return Override(COMPATIBILITY_UNPAREN_WITH_COMMA(namedArgs) \
swift_##name##Impl); \
return swift_##name##Impl COMPATIBILITY_PAREN(namedArgs); \
constexpr uintptr_t DEFAULT_IMPL_SENTINEL = 0x1; \
static std::atomic<uintptr_t> Override; \
uintptr_t fn = Override.load(std::memory_order::memory_order_relaxed); \
if (SWIFT_LIKELY(fn == DEFAULT_IMPL_SENTINEL)) { \
return swift_##name##Impl COMPATIBILITY_PAREN(namedArgs); \
} else if (SWIFT_UNLIKELY(fn == 0x0)) { \
return swift_##name##Slow(COMPATIBILITY_UNPAREN_WITH_COMMA(namedArgs) \
Override, \
fn, &swift_##name##Impl); \
} \
return ((Override_##name)fn)(COMPATIBILITY_UNPAREN_WITH_COMMA(namedArgs) & \
swift_##name##Impl); \
}

#endif // #else SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT
Expand Down