Skip to content

Commit

Permalink
Bug 1404666 - patch 1 - Accelerate OSPreferences::GetDateTimePattern …
Browse files Browse the repository at this point in the history
…by caching patterns found for particular style/locale combinations. r=gandalf
  • Loading branch information
jfkthame committed Dec 18, 2018
1 parent 0e5b436 commit 572203b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 25 additions & 2 deletions intl/locale/OSPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,34 @@ OSPreferences::GetDateTimePattern(int32_t aDateFormatStyle,
return NS_OK;
}

if (!ReadDateTimePattern(dateStyle, timeStyle, aLocale, aRetVal)) {
if (!GetDateTimePatternForStyle(dateStyle, timeStyle, aLocale, aRetVal)) {
// Create a cache key from the locale + style options
nsAutoCString key(aLocale);
key.Append(':');
key.AppendInt(aDateFormatStyle);
key.Append(':');
key.AppendInt(aTimeFormatStyle);

nsString pattern;
if (mPatternCache.Get(key, &pattern)) {
aRetVal = pattern;
return NS_OK;
}

if (!ReadDateTimePattern(dateStyle, timeStyle, aLocale, pattern)) {
if (!GetDateTimePatternForStyle(dateStyle, timeStyle, aLocale, pattern)) {
return NS_ERROR_FAILURE;
}
}

if (mPatternCache.Count() == kMaxCachedPatterns) {
// Don't allow unlimited cache growth; just throw it away in the case of
// pathological behavior where a page keeps requesting different formats
// and locales.
NS_WARNING("flushing DateTimePattern cache");
mPatternCache.Clear();
}
mPatternCache.Put(key, pattern);

aRetVal = pattern;
return NS_OK;
}
4 changes: 4 additions & 0 deletions intl/locale/OSPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define mozilla_intl_IntlOSPreferences_h__

#include "mozilla/StaticPtr.h"
#include "nsDataHashtable.h"
#include "nsString.h"
#include "nsTArray.h"
#include "unicode/uloc.h"
Expand Down Expand Up @@ -97,6 +98,9 @@ class OSPreferences : public mozIOSPreferences {
nsTArray<nsCString> mSystemLocales;
nsTArray<nsCString> mRegionalPrefsLocales;

const size_t kMaxCachedPatterns = 15;
nsDataHashtable<nsCStringHashKey, nsString> mPatternCache;

private:
virtual ~OSPreferences();

Expand Down

0 comments on commit 572203b

Please sign in to comment.