Skip to content

Commit fe40da0

Browse files
committed
Use ArraySlice for performance optimization in recursive functions
See http://swiftdoc.org/type/ArraySlice/
1 parent 110a5b4 commit fe40da0

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

‎ObjectMapper/Core/Mapper.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class Map {
4242
// save key and value associated to it
4343
currentKey = key
4444
// break down the components of the key
45-
currentValue = valueFor(key.componentsSeparatedByString("."), JSONDictionary)
45+
currentValue = valueFor(ArraySlice(split(key) { $0 == "." }), JSONDictionary)
4646

4747
return self
4848
}
@@ -80,7 +80,7 @@ public final class Map {
8080
}
8181

8282
/// Fetch value from JSON dictionary, loop through them until we reach the desired object.
83-
private func valueFor(keyPathComponents: [String], dictionary: [String : AnyObject]) -> AnyObject? {
83+
private func valueFor(keyPathComponents: ArraySlice<String>, dictionary: [String : AnyObject]) -> AnyObject? {
8484
// Implement it as a tail recursive function.
8585

8686
if keyPathComponents.isEmpty {
@@ -93,7 +93,7 @@ private func valueFor(keyPathComponents: [String], dictionary: [String : AnyObje
9393
return nil
9494

9595
case let dict as [String : AnyObject] where keyPathComponents.count > 1:
96-
let tail = Array(keyPathComponents[1..<keyPathComponents.count])
96+
let tail = dropFirst(keyPathComponents)
9797
return valueFor(tail, dict)
9898

9999
default:

‎ObjectMapper/Core/ToJSON.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import class Foundation.NSNumber
1010

1111
private func setValue(value: AnyObject, forKey key: String, inout #dictionary: [String : AnyObject]) {
12-
return setValue(value, forKeyPathComponents: key.componentsSeparatedByString("."), dictionary: &dictionary)
12+
return setValue(value, forKeyPathComponents: ArraySlice(split(key) { $0 == "." }), dictionary: &dictionary)
1313
}
1414

15-
private func setValue(value: AnyObject, forKeyPathComponents components: [String], inout #dictionary: [String : AnyObject]) {
15+
private func setValue(value: AnyObject, forKeyPathComponents components: ArraySlice<String>, inout #dictionary: [String : AnyObject]) {
1616
if components.isEmpty {
1717
return
1818
}
@@ -27,7 +27,7 @@ private func setValue(value: AnyObject, forKeyPathComponents components: [String
2727
child = [:]
2828
}
2929

30-
let tail = Array(components[1..<components.count])
30+
let tail = dropFirst(components)
3131
setValue(value, forKeyPathComponents: tail, dictionary: &child!)
3232

3333
return dictionary[head] = child

0 commit comments

Comments
 (0)