gecko-dev/modules/libpref/StaticPrefs.h
Kris Maglione 8e4b8a954f Bug 1472523: Part 1 - Avoid string copies in preference callbacks. r=njn
Most preference callbacks use literal strings for their domain filters, which
means that there's no need to make copies of them at all. Currently, however,
every preference observer node makes a separate heap-allocated copy of its
domain string.

This patch switches the domain string storage to nsCString instances, which
dramatically reduces the amount of unnecessary copies, at the expense of
making the callback nodes slightly larger.

MozReview-Commit-ID: 8NA3t2JS2UI

--HG--
extra : rebase_source : 628ad9af65cec16fb8be0c8dddc608b5ee5602e2
2018-06-30 23:06:17 -07:00

97 lines
3.0 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_StaticPrefs_h
#define mozilla_StaticPrefs_h
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/TypeTraits.h"
#include "MainThreadUtils.h"
namespace mozilla {
// These typedefs are for use within StaticPrefList.h.
typedef const char* String;
typedef Atomic<bool, Relaxed> RelaxedAtomicBool;
typedef Atomic<bool, ReleaseAcquire> ReleaseAcquireAtomicBool;
typedef Atomic<bool, SequentiallyConsistent> SequentiallyConsistentAtomicBool;
typedef Atomic<int32_t, Relaxed> RelaxedAtomicInt32;
typedef Atomic<int32_t, ReleaseAcquire> ReleaseAcquireAtomicInt32;
typedef Atomic<int32_t, SequentiallyConsistent>
SequentiallyConsistentAtomicInt32;
typedef Atomic<uint32_t, Relaxed> RelaxedAtomicUint32;
typedef Atomic<uint32_t, ReleaseAcquire> ReleaseAcquireAtomicUint32;
typedef Atomic<uint32_t, SequentiallyConsistent>
SequentiallyConsistentAtomicUint32;
template<typename T>
struct StripAtomicImpl
{
typedef T Type;
};
template<typename T, MemoryOrdering Order>
struct StripAtomicImpl<Atomic<T, Order>>
{
typedef T Type;
};
template<typename T>
using StripAtomic = typename StripAtomicImpl<T>::Type;
template<typename T>
struct IsAtomic : FalseType
{
};
template<typename T, MemoryOrdering Order>
struct IsAtomic<Atomic<T, Order>> : TrueType
{
};
class StaticPrefs
{
// For a VarCache pref like this:
//
// VARCACHE_PREF("my.varcache", my_varcache, int32_t, 99)
//
// we generate a static variable declaration and a getter definition:
//
// private:
// static int32_t sVarCache_my_varcache;
// public:
// static int32_t my_varcache() { return sVarCache_my_varcache; }
//
#define PREF(str, cpp_type, default_value)
#define VARCACHE_PREF(str, id, cpp_type, default_value) \
private: \
static cpp_type sVarCache_##id; \
\
public: \
static StripAtomic<cpp_type> id() \
{ \
MOZ_ASSERT(IsAtomic<cpp_type>::value || NS_IsMainThread(), \
"Non-atomic static pref '" str \
"' being accessed on background thread"); \
return sVarCache_##id; \
}
#include "mozilla/StaticPrefList.h"
#undef PREF
#undef VARCACHE_PREF
public:
static void InitAll(bool aIsStartup);
};
} // namespace mozilla
#endif // mozilla_StaticPrefs_h