Skip to content

Commit c55bbbe

Browse files
authored
refactor: performance tweaks under DatafileReader (#68)
1 parent 4930eff commit c55bbbe

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Sources/FeaturevisorSDK/DatafileReader.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import FeaturevisorTypes
33
public class DatafileReader {
44
let schemaVersion: String
55
let revision: String
6-
let attributes: [Attribute]
7-
let segments: [Segment]
8-
let features: [Feature]
6+
let attributes: [AttributeKey: Attribute]
7+
let segments: [SegmentKey: Segment]
8+
let features: [FeatureKey: Feature]
99

1010
init(datafileContent: DatafileContent) {
1111
self.schemaVersion = datafileContent.schemaVersion
1212
self.revision = datafileContent.revision
13-
self.segments = datafileContent.segments
14-
self.attributes = datafileContent.attributes
15-
self.features = datafileContent.features
13+
self.segments = Dictionary(
14+
uniqueKeysWithValues: datafileContent.segments.map { ($0.key, $0) }
15+
)
16+
self.attributes = Dictionary(
17+
uniqueKeysWithValues: datafileContent.attributes.map { ($0.key, $0) }
18+
)
19+
self.features = Dictionary(
20+
uniqueKeysWithValues: datafileContent.features.map { ($0.key, $0) }
21+
)
1622
}
1723

1824
public func getRevision() -> String {
@@ -24,18 +30,18 @@ public class DatafileReader {
2430
}
2531

2632
public func getAllAttributes() -> [Attribute] {
27-
return self.attributes
33+
return Array(attributes.values)
2834
}
2935

3036
public func getAttribute(_ attributeKey: AttributeKey) -> Attribute? {
31-
return self.attributes.first(where: { $0.key == attributeKey })
37+
return self.attributes[attributeKey]
3238
}
3339

3440
public func getSegment(_ segmentKey: SegmentKey) -> Segment? {
35-
return segments.first(where: { $0.key == segmentKey })
41+
return segments[segmentKey]
3642
}
3743

3844
public func getFeature(_ featureKey: FeatureKey) -> Feature? {
39-
return features.first(where: { $0.key == featureKey })
45+
return features[featureKey]
4046
}
4147
}

0 commit comments

Comments
 (0)