gecko-dev/js/ipc/JavaScriptChild.cpp
Nicholas Nethercote b9e071e2e8 Bug 1481998 - Make mozilla::Hash{Map,Set}'s entry storage allocation lazy. r=luke,sfink
Entry storage allocation now occurs on the first lookupForAdd()/put()/putNew().
This removes the need for init() and initialized(), and matches how
PLDHashTable/nsTHashtable work. It also removes the need for init() functions
in a lot of types that are built on top of mozilla::Hash{Map,Set}.

Pros:

- No need for init() calls and subsequent checks.

- No memory allocated for empty tables, which are not that uncommon.

Cons:

- An extra branch in lookup() and lookupForAdd(), but not in put()/putNew(),
  because the existing checkOverloaded() can handle it.

Specifics:

- Construction now can take a length parameter.

- init() is removed. Explicit length-setting, when necessary, now occurs in the
  constructors.

- initialized() is removed.

- capacity() now returns zero when the entry storage is absent.

- lookupForAdd() is no longer `const`, because it can instantiate the storage,
  which requires modifications.

- lookupForAdd() can now return an invalid AddPtr in two cases:

  - old: hashing failure (due to OOM in the hasher)

  - new: OOM while instantiating entry storage

  The existing failure handling paths for the old case work for the new case.

- clear(), finish(), and clearAndShrink() are replaced by clear(), compact(),
  and reserve(). The old compactIfUnderloaded() is also removed.

- Capacity computation code is now in its own functions, bestCapacity() and
  hashShift(). setTableSizeLog2() is removed.

- uint32_t is used throughout for capacities, instead of size_t, for
  consistency with other similar values.

- changeTableSize() now takes a capacity instead of a deltaLog2, and it can now
  handle !mTable.

Measurements:

- Total source code size is reduced by over 900 lines. Also, lots of existing
  lines got shorter (i.e. two checks were reduced to one).

- Executable size barely changed, down by 2 KiB on Linux64. The extra branches
  are compensated for by the lack of init() calls.

- Speed changed negligibly. The instruction count for Bench_Cpp_MozHash
  increased from 2.84 billion to 2.89 billion but any execution time change was
  well below noise.
2018-08-10 18:00:29 +10:00

96 lines
2.4 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et 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/. */
#include "JavaScriptChild.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/ipc/MessageChannel.h"
#include "nsContentUtils.h"
#include "xpcprivate.h"
#include "jsfriendapi.h"
#include "AccessCheck.h"
using namespace JS;
using namespace mozilla;
using namespace mozilla::jsipc;
using mozilla::AutoSafeJSContext;
static void
UpdateChildWeakPointersBeforeSweepingZoneGroup(JSContext* cx, void* data)
{
static_cast<JavaScriptChild*>(data)->updateWeakPointers();
}
static void
TraceChild(JSTracer* trc, void* data)
{
static_cast<JavaScriptChild*>(data)->trace(trc);
}
JavaScriptChild::~JavaScriptChild()
{
JSContext* cx = dom::danger::GetJSContext();
JS_RemoveWeakPointerZonesCallback(cx, UpdateChildWeakPointersBeforeSweepingZoneGroup);
JS_RemoveExtraGCRootsTracer(cx, TraceChild, this);
}
bool
JavaScriptChild::init()
{
JSContext* cx = dom::danger::GetJSContext();
JS_AddWeakPointerZonesCallback(cx, UpdateChildWeakPointersBeforeSweepingZoneGroup, this);
JS_AddExtraGCRootsTracer(cx, TraceChild, this);
return true;
}
void
JavaScriptChild::trace(JSTracer* trc)
{
objects_.trace(trc, strongReferenceObjIdMinimum_);
}
void
JavaScriptChild::updateWeakPointers()
{
objects_.sweep();
unwaivedObjectIds_.sweep();
waivedObjectIds_.sweep();
}
JSObject*
JavaScriptChild::scopeForTargetObjects()
{
// CPOWs from the parent need to point into the child's privileged junk
// scope so that they can benefit from XrayWrappers in the child.
return xpc::PrivilegedJunkScope();
}
mozilla::ipc::IPCResult
JavaScriptChild::RecvDropTemporaryStrongReferences(const uint64_t& upToObjId)
{
strongReferenceObjIdMinimum_ = upToObjId + 1;
return IPC_OK();
}
PJavaScriptChild*
mozilla::jsipc::NewJavaScriptChild()
{
JavaScriptChild* child = new JavaScriptChild();
if (!child->init()) {
delete child;
return nullptr;
}
return child;
}
void
mozilla::jsipc::ReleaseJavaScriptChild(PJavaScriptChild* child)
{
static_cast<JavaScriptChild*>(child)->decref();
}