From 9bf0aa863f19e78af843e6bbc21f363851e337d4 Mon Sep 17 00:00:00 2001 From: Marcin Polak Date: Fri, 29 Mar 2024 19:39:16 +0100 Subject: [PATCH] refactor: performance tweaks under `DatafileReader` --- Sources/FeaturevisorSDK/DatafileReader.swift | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Sources/FeaturevisorSDK/DatafileReader.swift b/Sources/FeaturevisorSDK/DatafileReader.swift index 6e2ac95..d4690b4 100644 --- a/Sources/FeaturevisorSDK/DatafileReader.swift +++ b/Sources/FeaturevisorSDK/DatafileReader.swift @@ -3,16 +3,22 @@ import FeaturevisorTypes public class DatafileReader { let schemaVersion: String let revision: String - let attributes: [Attribute] - let segments: [Segment] - let features: [Feature] + let attributes: [AttributeKey: Attribute] + let segments: [SegmentKey: Segment] + let features: [FeatureKey: Feature] init(datafileContent: DatafileContent) { self.schemaVersion = datafileContent.schemaVersion self.revision = datafileContent.revision - self.segments = datafileContent.segments - self.attributes = datafileContent.attributes - self.features = datafileContent.features + self.segments = Dictionary( + uniqueKeysWithValues: datafileContent.segments.map { ($0.key, $0) } + ) + self.attributes = Dictionary( + uniqueKeysWithValues: datafileContent.attributes.map { ($0.key, $0) } + ) + self.features = Dictionary( + uniqueKeysWithValues: datafileContent.features.map { ($0.key, $0) } + ) } public func getRevision() -> String { @@ -24,18 +30,18 @@ public class DatafileReader { } public func getAllAttributes() -> [Attribute] { - return self.attributes + return Array(attributes.values) } public func getAttribute(_ attributeKey: AttributeKey) -> Attribute? { - return self.attributes.first(where: { $0.key == attributeKey }) + return self.attributes[attributeKey] } public func getSegment(_ segmentKey: SegmentKey) -> Segment? { - return segments.first(where: { $0.key == segmentKey }) + return segments[segmentKey] } public func getFeature(_ featureKey: FeatureKey) -> Feature? { - return features.first(where: { $0.key == featureKey }) + return features[featureKey] } }