Repository: realm/realm-swift
File: RealmSwift/Impl/SchemaDiscovery.swift (Swift)

Code before:
    public convenience init<O: ObjectBase, V: _Persistable>(
        name: String,
        objectType _: O.Type,
        valueType _: V.Type,
        indexed: Bool = false,
        primaryKey: Bool = false,
        originProperty: String? = nil
    ) {
        self.init()
        self.name = name
        self.type = V._rlmType
        self.optional = V._rlmOptional
        self.indexed = primaryKey || indexed
        self.isPrimary = primaryKey
        self.linkOriginPropertyName = originProperty
        V._rlmPopulateProperty(self)
        V._rlmSetAccessor(self)
        self.swiftIvar = ivar_getOffset(class_getInstanceVariable(O.self, "_" + name)!)
    }
}

private func getModernProperties(_ object: ObjectBase) -> [RLMProperty] {
    let columnNames: [String: String] = type(of: object).propertiesMapping()
    return Mirror(reflecting: object).children.compactMap { prop in
        guard let label = prop.label else { return nil }
        guard let value = prop.value as? DiscoverablePersistedProperty else {
            return nil
        }
        let property = RLMProperty(name: label, value: value)
        property.swiftIvar = ivar_getOffset(class_getInstanceVariable(type(of: object), label)!)
        property.columnName = columnNames[property.name]
        return property
    }
}

// If the property is a storage property for a lazy Swift property, return
// the base property name (e.g. `foo.storage` becomes `foo`). Otherwise, nil.
private func baseName(forLazySwiftProperty name: String) -> String? {
    // A Swift lazy var shows up as two separate children on the reflection tree:
    // one named 'x', and another that is optional and is named "$__lazy_storage_$_propName"
    if let storageRange = name.range(of: "$__lazy_storage_$_", options: [.anchored]) {
        return String(name[storageRange.upperBound...])
    }
    return nil
}

private func getLegacyProperties(_ object: ObjectBase, _ cls: ObjectBase.Type) -> [RLMProperty] {
    let indexedProperties: Set<String>
    let ignoredPropNames: Set<String>
    let columnNames: [String: String] = type(of: object).propertiesMapping()
    // FIXME: ignored properties on EmbeddedObject appear to not be supported?

Review comment (performance):
Side note: This line is quite slow, particularly for the List Accessor which is generic over the list's Element. There is a runtime cast of the List Accessor to `AnyObject?` that takes a long time for the Swift runtime to lookup class conformance. I am no expert in this kind of optimisation, but I believe it's because the class type is defined in your module and the generic argument is defined in my module, and Swift has no way to reason about and inline it at compile time... so it just does it at runtime. 

Fixing this somehow is one of the one last bottlenecks for an insanely fast schema initialisation. I'm happy to make a separate GitHub issue for that if desired. 

Code after:
        valueType._rlmPopulateProperty(self)
        if valueType._rlmRequireObjc {
            self.updateAccessors()
        }
    }

    /// Exposed for Macros.
    /// Important: Keep args in same order & default value as `@Persisted` property wrapper
    @_spi(RealmSwiftPrivate)
    public convenience init<O: ObjectBase, V: _Persistable>(
        name: String,
        objectType _: O.Type,
        valueType _: V.Type,
        indexed: Bool = false,
        primaryKey: Bool = false,
        originProperty: String? = nil
    ) {
        self.init()
        self.name = name
        self.type = V._rlmType
        self.optional = V._rlmOptional
        self.indexed = primaryKey || indexed
        self.isPrimary = primaryKey
        self.linkOriginPropertyName = originProperty
        V._rlmPopulateProperty(self)
        V._rlmSetAccessor(self)
        self.swiftIvar = ivar_getOffset(class_getInstanceVariable(O.self, "_" + name)!)
    }
}

private func getModernProperties(_ object: ObjectBase) -> [RLMProperty] {
    let columnNames: [String: String] = type(of: object).propertiesMapping()
    return Mirror(reflecting: object).children.compactMap { prop in
        guard let label = prop.label else { return nil }
        guard let value = prop.value as? DiscoverablePersistedProperty else {
            return nil
        }
        let property = RLMProperty(name: label, value: value)
        property.swiftIvar = ivar_getOffset(class_getInstanceVariable(type(of: object), label)!)
        property.columnName = columnNames[property.name]
        return property
    }
}

// If the property is a storage property for a lazy Swift property, return
// the base property name (e.g. `foo.storage` becomes `foo`). Otherwise, nil.
private func baseName(forLazySwiftProperty name: String) -> String? {
    // A Swift lazy var shows up as two separate children on the reflection tree:
    // one named 'x', and another that is optional and is named "$__lazy_storage_$_propName"
    if let storageRange = name.range(of: "$__lazy_storage_$_", options: [.anchored]) {
        return String(name[storageRange.upperBound...])