From 1b0c4e5a9e2293e4639f6602bcf77cdd6f00bfb5 Mon Sep 17 00:00:00 2001 From: Bharath Booshan Date: Sat, 14 Nov 2020 19:00:46 -0800 Subject: [PATCH 1/4] Improve loadRecords performance in SQLiteNormalizedCache especially when queryFields involves arguments --- Sources/ApolloSQLite/SQLiteNormalizedCache.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index 6d5a8a70b9..2f5ac054d1 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -140,11 +140,12 @@ extension SQLiteNormalizedCache: NormalizedCache { let result: Swift.Result<[Record?], Error> do { let records = try self.selectRecords(forKeys: keys) + let recordsIndexMap = records.indices.reversed().reduce(into: [:]) { recordsIndexMap, index in + recordsIndexMap[records[index].key, default: index] = index + } + let recordsOrNil: [Record?] = keys.map { key in - if let recordIndex = records.firstIndex(where: { $0.key == key }) { - return records[recordIndex] - } - return nil + recordsIndexMap[key].flatMap { records[$0] } } result = .success(recordsOrNil) From e1198dd46063cc563752cf3e41dd2b9de30548a9 Mon Sep 17 00:00:00 2001 From: Bharath Booshan Date: Sun, 15 Nov 2020 21:40:27 -0800 Subject: [PATCH 2/4] rename recordsIndexMap variable in reduce block to resultMap --- Sources/ApolloSQLite/SQLiteNormalizedCache.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index 2f5ac054d1..8964200279 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -140,8 +140,8 @@ extension SQLiteNormalizedCache: NormalizedCache { let result: Swift.Result<[Record?], Error> do { let records = try self.selectRecords(forKeys: keys) - let recordsIndexMap = records.indices.reversed().reduce(into: [:]) { recordsIndexMap, index in - recordsIndexMap[records[index].key, default: index] = index + let recordsIndexMap = records.indices.reversed().reduce(into: [:]) { resultMap, index in + resultMap[records[index].key, default: index] = index } let recordsOrNil: [Record?] = keys.map { key in From 293dd3b3287413a057e83050a31d181131b74c7f Mon Sep 17 00:00:00 2001 From: bharath2020 Date: Mon, 16 Nov 2020 05:47:53 -0800 Subject: [PATCH 3/4] Remove unused default parameter while setting value in resultMap Co-authored-by: TizianoCoroneo --- Sources/ApolloSQLite/SQLiteNormalizedCache.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index 8964200279..eab779e108 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -141,7 +141,7 @@ extension SQLiteNormalizedCache: NormalizedCache { do { let records = try self.selectRecords(forKeys: keys) let recordsIndexMap = records.indices.reversed().reduce(into: [:]) { resultMap, index in - resultMap[records[index].key, default: index] = index + resultMap[records[index].key] = index } let recordsOrNil: [Record?] = keys.map { key in From 76d343121086513a3d9772aaa8ab299909739a36 Mon Sep 17 00:00:00 2001 From: Bharath Booshan Date: Tue, 17 Nov 2020 10:26:21 -0800 Subject: [PATCH 4/4] Remove reversal of record indicies when constructing recordsIndexMap in SQLiteNormalizedCache.loadRecords --- Sources/ApolloSQLite/SQLiteNormalizedCache.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift index eab779e108..af5c8da8c9 100644 --- a/Sources/ApolloSQLite/SQLiteNormalizedCache.swift +++ b/Sources/ApolloSQLite/SQLiteNormalizedCache.swift @@ -140,7 +140,7 @@ extension SQLiteNormalizedCache: NormalizedCache { let result: Swift.Result<[Record?], Error> do { let records = try self.selectRecords(forKeys: keys) - let recordsIndexMap = records.indices.reversed().reduce(into: [:]) { resultMap, index in + let recordsIndexMap = records.indices.reduce(into: [:]) { resultMap, index in resultMap[records[index].key] = index }