From b36a0061e18ef468bb409d0374c9aa3b87c4fe60 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 7 Mar 2019 17:05:54 -0800 Subject: [PATCH] contact picker perf for contact with many phone numbers Only consider first n phone numbers for a contact. Some users have a "Spam" contact created by external apps which have thousands of known telemarkers/scammers phone numbers, this pathological case causes a slowdown in the presentation of the compose picker. --- SignalServiceKit/src/Contacts/Contact.m | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/SignalServiceKit/src/Contacts/Contact.m b/SignalServiceKit/src/Contacts/Contact.m index 046f96137..74d708798 100644 --- a/SignalServiceKit/src/Contacts/Contact.m +++ b/SignalServiceKit/src/Contacts/Contact.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2018 Open Whisper Systems. All rights reserved. +// Copyright (c) 2019 Open Whisper Systems. All rights reserved. // #import "Contact.h" @@ -45,7 +45,16 @@ NS_ASSUME_NONNULL_BEGIN NSMutableArray *phoneNumbers = [NSMutableArray new]; NSMutableDictionary *phoneNumberNameMap = [NSMutableDictionary new]; - for (CNLabeledValue *phoneNumberField in cnContact.phoneNumbers) { + const NSUInteger kMaxPhoneNumbersConsidered = 50; + + NSArray *consideredPhoneNumbers; + if (cnContact.phoneNumbers.count <= kMaxPhoneNumbersConsidered) { + consideredPhoneNumbers = cnContact.phoneNumbers; + } else { + OWSLogInfo(@"For perf, only considering the first %lu phone numbers for contact with many numbers.", (unsigned long)kMaxPhoneNumbersConsidered); + consideredPhoneNumbers = [cnContact.phoneNumbers subarrayWithRange:NSMakeRange(0, kMaxPhoneNumbersConsidered)]; + } + for (CNLabeledValue *phoneNumberField in consideredPhoneNumbers) { if ([phoneNumberField.value isKindOfClass:[CNPhoneNumber class]]) { CNPhoneNumber *phoneNumber = (CNPhoneNumber *)phoneNumberField.value; [phoneNumbers addObject:phoneNumber.stringValue];