Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: performance tweaks under DatafileReader #68

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a small improvement (if any) but you can also consider returning here sequence, so you don't need to create new Array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's replace arrays with dictionaries under this PR. I believe that is not the last PR like this.

}

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]
}
}
Loading