Closed Bug 1346224 Opened 7 years ago Closed 6 years ago

Add helper macro for defining static preference caches

Categories

(Core :: Preferences: Backend, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1436655

People

(Reporter: nika, Unassigned)

Details

I've seen a somewhat common pattern of writing something like the following:

static bool sCachedPref = false;
static bool sPref = false;
if (!sCachedPref) {
  sCachedPref = true;
  Preferences::AddBoolVarCache(&sCachedPref, "the.pref");
}

if (sPref) {
}

And that seems like a lot of writing to do in order to get efficient main-thread-only reads of a preference. It would be nice to have something like

#define LAZY_MAINTHREAD_BOOL_PREF(cacheName, prefStr) \
  static bool cacheName = false; \
  do { \
    static bool cacheName##__iscached = false; \
    if (!cacheName##__iscached) { \
      cacheName##__iscached = true; \
      Preferences::AddBoolVarCache(&cacheName, prefStr); \
    } \
  } while (0)

Which would mean that you could write something like

LAZY_MAINTHREAD_BOOL_PREF(sCachedPref, "the.pref");
if (sCachedPref) {
  // ...
}

There is also potential for doing this in a class, which would be defined something like:

static LazyBoolPrefCache sPref("the.pref");

I'm not sure what the best solution would be, but it seems like a pattern which we use enough to avoid the cost of "Preferences::GetBool" that it's worth simplifying some more.
Bug 1436655's introduction of StaticPrefs allows us to get rid of these static bools in a different way.
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.