But it's calculation needs converting Error to NSError using _swift_stdlib_bridgeErrorToNSError function that calls Error._domain computed-property through _getErrorDomainNSString<A>(_:) call.
Error._domain uses String(reflecting: type(of: self)) that internally performs multiple casts to protocols in OutputStream.swift: CustomDebugStringConvertible, CustomStringConvertible, TextOutputStreamable and CustomReflectable inside Mirror initializer. First conformance check for each pair of (class, protocol) is time-consuming because we need to scan all protocol conformance type descriptors.
That's why calculation Error._domain takes 15 ms on launch. Screenshot from Time Profilers traces is attached:
But internally, String(reflecting:) called on class-type eventually calls _typeName(:qualified:) function with qualified: true.
Instead of calling _typeName(type, qualified: true) indirectly using String(reflecting:) with significant performance hit because of protocol conformances checks, I suggest to call _typeName(type(of: self), qualified: true) directly in Error._domain computed-property.
Motivation
Error.localizedDescription
is widely used in many iOS apps.But it's calculation needs converting
Error
toNSError
using_swift_stdlib_bridgeErrorToNSError
function that callsError._domain
computed-property through_getErrorDomainNSString<A>(_:)
call.Error._domain
usesString(reflecting: type(of: self))
that internally performs multiple casts to protocols in OutputStream.swift:CustomDebugStringConvertible
,CustomStringConvertible
,TextOutputStreamable
andCustomReflectable
insideMirror
initializer. First conformance check for each pair of (class, protocol) is time-consuming because we need to scan all protocol conformance type descriptors.That's why calculation
Error._domain
takes 15 ms on launch. Screenshot from Time Profilers traces is attached:But internally,
String(reflecting:)
called on class-type eventually calls_typeName(:qualified:)
function withqualified: true
.Full call-stack:
Error._domain
String(reflecting:)
_debugPrint_unlocked
. Because Any.Type does not conform any of checked protocols._adHocPrint_unlocked
_adHocPrint_unlocked.printTypeName
Proposed solution
Instead of calling
_typeName(type, qualified: true)
indirectly usingString(reflecting:)
with significant performance hit because of protocol conformances checks, I suggest to call_typeName(type(of: self), qualified: true)
directly inError._domain
computed-property.Alternatives considered
Leave everything as it is now.
Additional information
No response