From e4fda7d5bb8964b8e6cdd867c66f731b78b087ac Mon Sep 17 00:00:00 2001 From: Will Date: Thu, 1 Nov 2018 13:07:17 +1100 Subject: [PATCH] GetInstance() - avoid locking --- .gitignore | 1 + csharp/PhoneNumbers/PhoneNumberUtil.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/.gitignore b/.gitignore index 31a08b7..2cbb0e8 100644 --- a/.gitignore +++ b/.gitignore @@ -42,6 +42,7 @@ build/ *.pidb *.log *.scc +*/.vs/* # Visual C++ cache files ipch/ diff --git a/csharp/PhoneNumbers/PhoneNumberUtil.cs b/csharp/PhoneNumbers/PhoneNumberUtil.cs index ca81e69..98356b5 100644 --- a/csharp/PhoneNumbers/PhoneNumberUtil.cs +++ b/csharp/PhoneNumbers/PhoneNumberUtil.cs @@ -857,6 +857,14 @@ public IReadOnlyDictionary> CallingCodeToRegion public static PhoneNumberUtil GetInstance(String baseFileLocation, Dictionary> countryCallingCodeToRegionCodeMap) { + //double-check on purpose to avoid locking + //Storing value in a temp variable first, because it can get reset to null. + var i = instance_; + if (i != null) + { + return i; + } + lock (thisLock) { if (instance_ == null) @@ -909,6 +917,14 @@ public Dictionary.KeyCollection GetSupportedGlobalNetworkCal */ public static PhoneNumberUtil GetInstance() { + //double-check on purpose to avoid locking + //Storing value in a temp variable first, because it can get reset to null. + var i = instance_; + if (i != null) + { + return i; + } + lock (thisLock) { if (instance_ == null)