Skip to content

Commit

Permalink
refactor: performance tweaks under DatafileReader (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
polok authored Apr 3, 2024
1 parent 4930eff commit c55bbbe
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions Sources/FeaturevisorSDK/DatafileReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
}
}

0 comments on commit c55bbbe

Please sign in to comment.