From 608c88a5c2c2eb9019a8cb38d391617acc5f78e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Ubald=20Brien?= Date: Tue, 17 Jan 2017 22:30:36 -0500 Subject: [PATCH 1/2] Comments for pull request --- Sources/Reflection/Properties.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Reflection/Properties.swift b/Sources/Reflection/Properties.swift index 212bfc7..4d72e62 100644 --- a/Sources/Reflection/Properties.swift +++ b/Sources/Reflection/Properties.swift @@ -49,10 +49,12 @@ private func nextProperty(description: Property.Description, storage: UnsafeRawP public func properties(_ type: Any.Type) throws -> [Property.Description] { let hashedType = HashedType(type) if let properties = cachedProperties[hashedType] { + // When cached, only nominal type properties are returned return properties } else if let nominalType = Metadata.Struct(type: type) { return try fetchAndSaveProperties(nominalType: nominalType, hashedType: hashedType) } else if let nominalType = Metadata.Class(type: type) { + // This will return all properties (nominal and super), but only cache nominal return try nominalType.properties() } else { throw ReflectionError.notStruct(type: type) From 148e1f0ad595eb6811fced3202e8617b2bdc750e Mon Sep 17 00:00:00 2001 From: "Fran]ois Ubald Brien" Date: Tue, 17 Jan 2017 22:53:49 -0500 Subject: [PATCH 2/2] Fixed property caching --- Sources/Reflection/Metadata+Class.swift | 2 +- Sources/Reflection/Properties.swift | 18 +++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Sources/Reflection/Metadata+Class.swift b/Sources/Reflection/Metadata+Class.swift index abfa45e..3c39fa8 100644 --- a/Sources/Reflection/Metadata+Class.swift +++ b/Sources/Reflection/Metadata+Class.swift @@ -14,7 +14,7 @@ extension Metadata { } func properties() throws -> [Property.Description] { - let properties = try fetchAndSaveProperties(nominalType: self, hashedType: HashedType(pointer)) + let properties = try propertiesForNominalType(self) guard let superclass = superclass, String(describing: unsafeBitCast(superclass.pointer, to: Any.Type.self)) != "SwiftObject" else { return properties } diff --git a/Sources/Reflection/Properties.swift b/Sources/Reflection/Properties.swift index 4d72e62..07cb8a5 100644 --- a/Sources/Reflection/Properties.swift +++ b/Sources/Reflection/Properties.swift @@ -49,25 +49,21 @@ private func nextProperty(description: Property.Description, storage: UnsafeRawP public func properties(_ type: Any.Type) throws -> [Property.Description] { let hashedType = HashedType(type) if let properties = cachedProperties[hashedType] { - // When cached, only nominal type properties are returned return properties } else if let nominalType = Metadata.Struct(type: type) { - return try fetchAndSaveProperties(nominalType: nominalType, hashedType: hashedType) + let properties = try propertiesForNominalType(nominalType) + cachedProperties[hashedType] = properties + return properties } else if let nominalType = Metadata.Class(type: type) { - // This will return all properties (nominal and super), but only cache nominal - return try nominalType.properties() + let properties = try nominalType.properties() + cachedProperties[hashedType] = properties + return properties } else { throw ReflectionError.notStruct(type: type) } } -func fetchAndSaveProperties(nominalType: T, hashedType: HashedType) throws -> [Property.Description] { - let properties = try propertiesForNominalType(nominalType) - cachedProperties[hashedType] = properties - return properties -} - -private func propertiesForNominalType(_ type: T) throws -> [Property.Description] { +internal func propertiesForNominalType(_ type: T) throws -> [Property.Description] { guard type.nominalTypeDescriptor.numberOfFields != 0 else { return [] } guard let fieldTypes = type.fieldTypes, let fieldOffsets = type.fieldOffsets else { throw ReflectionError.unexpected