bmalloc: Allocation should be more precise
https://bugs.webkit.org/show_bug.cgi?id=136993

Reviewed by Gavin Barraclough.

13% reduction in heap size on the MallocBench *_memory_warning benchmarks.

This patch teaches the allocator to merge adjacent free lines into a
single allocatable range. This allows us to shrink the size of an
individual line without increasing fragmentation or the rate of allocator
slow paths.

We'll only take more slow paths when available memory is sparse, which
is exactly when it's worth it. When available memory is dense, we'll
take fewer slow paths.

* bmalloc.xcodeproj/project.pbxproj:
* bmalloc/Algorithm.h:
(bmalloc::divideRoundingUp):

* bmalloc/Allocator.cpp:
(bmalloc::Allocator::Allocator): Updated for interface changes.

(bmalloc::Allocator::scavenge): Scavenge by object instead of by line.
Now that we merge lines, it's not convenient to scavenge by line.

(bmalloc::Allocator::allocateSmallBumpRange):
(bmalloc::Allocator::allocateMediumBumpRange): Allocate whole ranges
instead of individual lines.

(bmalloc::Allocator::allocateSlowCase):
(bmalloc::Allocator::allocateSmallLine): Deleted.
(bmalloc::Allocator::allocateMediumLine): Deleted.
(bmalloc::Allocator::allocateMedium): Deleted.
* bmalloc/Allocator.h:
(bmalloc::Allocator::allocateFastCase): Folded medium allocations
into the standard fast path with small allocations. Since a BumpAllocator
just allocates out of an arbitrary range, it doesn't need to distinguish
between small and medium lines.

* bmalloc/BumpAllocator.h:
(bmalloc::BumpAllocator::size):
(bmalloc::BumpAllocator::BumpAllocator):
(bmalloc::BumpAllocator::init):
(bmalloc::BumpAllocator::refill):
(bmalloc::BumpAllocator::line): Deleted. No need to track line information
anymore: the heap just gives us a pointer and a pre-computed number of
objects, and we allocate them.

* bmalloc/Deallocator.cpp:
(bmalloc::Deallocator::processObjectLog): Updated for interface changes.

* bmalloc/Heap.cpp:
(bmalloc::Heap::Heap):
(bmalloc::Heap::initializeLineMetadata): Pre-compute precise metadata
detailing where all objects will lie in memory. After we merge two lines,
we might allocate an object that spans from one line to the next. This
metadata details which bits of memory overlap in that way, and how they
overlap.

(bmalloc::Heap::refillSmallBumpRangeCache):
(bmalloc::Heap::refillMediumBumpRangeCache): Scan a whole page at a time,
and merge adjacent free lines into BumpRanges.

(bmalloc::Heap::allocateSmallPage):
(bmalloc::Heap::allocateMediumPage):
(bmalloc::Heap::deallocateSmallLine):
(bmalloc::Heap::deallocateMediumLine): Track pages rather than lines,
since we scan for free memory a page at a time.

(bmalloc::Heap::allocateSmallLineSlowCase): Deleted.
(bmalloc::Heap::allocateMediumLineSlowCase): Deleted. Folded into the
fast path.

* bmalloc/Heap.h:
(bmalloc::Heap::derefSmallLine):
(bmalloc::Heap::derefMediumLine):
(bmalloc::Heap::deallocateSmallLine): Deleted.
(bmalloc::Heap::allocateSmallLine): Deleted.
(bmalloc::Heap::deallocateMediumLine): Deleted.
(bmalloc::Heap::allocateMediumLine): Deleted. Updated for interface changes.

* bmalloc/Line.h:
(bmalloc::Line<Traits>::ref):
(bmalloc::Line<Traits>::deref):
(bmalloc::Line<Traits>::concurrentRef): Deleted. We don't pass a derefCount
anymore, since we only ever deref by 1 now.

* bmalloc/MediumAllocator.h:
(bmalloc::MediumAllocator::isNull): Deleted.
(bmalloc::MediumAllocator::MediumAllocator): Deleted.
(bmalloc::MediumAllocator::line): Deleted.
(bmalloc::MediumAllocator::allocate): Deleted.
(bmalloc::MediumAllocator::derefCount): Deleted.
(bmalloc::MediumAllocator::refill): Deleted.
(bmalloc::MediumAllocator::clear): Deleted. Deleted some code that's
been dead for a while, since it doesn't build anymore with this patch.

* bmalloc/Page.h:
(bmalloc::Page::sizeClass):
(bmalloc::Page::setSizeClass):
(bmalloc::Page::smallSizeClass): Deleted.
(bmalloc::Page::setSmallSizeClass): Deleted. Renamed setSmallSizeClass
to sizeClass, since we use it for medium sizes too.

* bmalloc/Sizes.h:
(bmalloc::Sizes::sizeClass):
(bmalloc::Sizes::objectSize): Shrank line sizes to save memory.

(bmalloc::Sizes::smallSizeClassFor): Deleted.
(bmalloc::Sizes::mediumSizeClassFor): Deleted.

* bmalloc/bmalloc.h:
(bmalloc::api::realloc): Now that we have precise objects sizes, realloc
can be a bit more precise. It also has to be, since we can't guarantee
that an object ends at the end of a line anymore.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@173881 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Web Inspector: Should be able to attach a debugger to a JSContext before anything is executed
https://bugs.webkit.org/show_bug.cgi?id=136893

Patch by Joseph Pecoraro <pecoraro@apple.com> on 2014-09-23
Reviewed by Timothy Hatcher.

Source/JavaScriptCore:

Adds new remote inspector protocol handling for automatic inspection.
Debuggers can signal they have enabled automatic inspection, and
when debuggables are created the current application will pause to
see if the debugger will inspect or decline to inspect the debuggable.

* inspector/remote/RemoteInspectorConstants.h:
* inspector/remote/RemoteInspector.h:
* inspector/remote/RemoteInspector.mm:
(Inspector::globalAutomaticInspectionState):
(Inspector::RemoteInspector::RemoteInspector):
(Inspector::RemoteInspector::start):
When first starting, check the global "is there an auto-inspect" debugger state.
This is necessary so that the current application knows if it should pause or
not when a debuggable is created, even without having connected to webinspectord yet.

(Inspector::RemoteInspector::updateDebuggableAutomaticInspectCandidate):
When a debuggable has enabled remote inspection, take this path to propose
it as an automatic inspection candidate if there is an auto-inspect debugger.

(Inspector::RemoteInspector::sendAutomaticInspectionCandidateMessage):
Send the automatic inspection candidate message.

(Inspector::RemoteInspector::receivedSetupMessage):
(Inspector::RemoteInspector::setupFailed):
(Inspector::RemoteInspector::setupSucceeded):
After attempting to open an inspector, unpause if it was for the
automatic inspection candidate.

(Inspector::RemoteInspector::waitingForAutomaticInspection):
When running a nested runloop, check if we should remain paused.

(Inspector::RemoteInspector::setupXPCConnectionIfNeeded):
If by the time we connect to webinspectord we have a candidate, then
immediately send the candidate message.

(Inspector::RemoteInspector::stopInternal):
(Inspector::RemoteInspector::xpcConnectionFailed):
In error cases, clear our state.

(Inspector::RemoteInspector::xpcConnectionReceivedMessage):
(Inspector::RemoteInspector::receivedAutomaticInspectionConfigurationMessage):
(Inspector::RemoteInspector::receivedAutomaticInspectionRejectMessage):
Update state when receiving new messages.

* inspector/remote/RemoteInspectorDebuggable.h:
* inspector/remote/RemoteInspectorDebuggable.cpp:
(Inspector::RemoteInspectorDebuggable::setRemoteDebuggingAllowed):
Special case when a debuggable is newly allowed to be debuggable.

(Inspector::RemoteInspectorDebuggable::pauseWaitingForAutomaticInspection):
Run a nested run loop while this is an automatic inspection candidate.

* inspector/JSGlobalObjectInspectorController.h:
* inspector/JSGlobalObjectInspectorController.cpp:
(Inspector::JSGlobalObjectInspectorController::JSGlobalObjectInspectorController):
(Inspector::JSGlobalObjectInspectorController::connectFrontend):
When the inspector starts via automatic inspection automatically pause.
We plan on removing this condition by having the frontend signal to the
backend when it is completely initialized.

* inspector/remote/RemoteInspectorDebuggableConnection.h:
* inspector/remote/RemoteInspectorDebuggableConnection.mm:
(Inspector::RemoteInspectorDebuggableConnection::setup):
Pass on the flag of whether or not this was automatic inspection.

* runtime/JSGlobalObjectDebuggable.h:
* runtime/JSGlobalObjectDebuggable.cpp:
(JSC::JSGlobalObjectDebuggable::connect):
(JSC::JSGlobalObjectDebuggable::pauseWaitingForAutomaticInspection):
When pausing in a JSGlobalObject we need to release the API lock.

Source/WebCore:

Automatic inspection is currently disabled for web pages.
This just updates the interfaces that changed.

* WebCore.exp.in:
* inspector/InspectorController.cpp:
(WebCore::InspectorController::connectFrontend):
(WebCore::InspectorController::show):
* inspector/InspectorController.h:
* page/PageDebuggable.cpp:
(WebCore::PageDebuggable::connect):
* page/PageDebuggable.h:
* testing/Internals.cpp:
(WebCore::Internals::openDummyInspectorFrontend):

Source/WebKit:

* WebKit.vcxproj/WebKitExportGenerator/WebKitExports.def.in:

Source/WebKit2:

* WebProcess/WebPage/WebInspector.cpp:
(WebKit::WebInspector::remoteFrontendConnected):

Source/WTF:

Currently automatic inspection only happens in processes that have a
debugger attached. That condition may change in the future, but this
function can stand on its own in WTF. It may be useful in the future
to perhaps continue though ASSERTs if you have a debugger attached.

* wtf/Assertions.cpp:
* wtf/Assertions.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@173882 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Have Document::accessSVGExtensions() return a reference
https://bugs.webkit.org/show_bug.cgi?id=137030

Reviewed by Ryosuke Niwa.

Have Document::accessSVGExtensions() return a reference instead of a
pointer as it can never return null.

No new tests, no behavior change.

* dom/ContainerNode.cpp:
(WebCore::ContainerNode::removeChild):
(WebCore::ContainerNode::removeChildren):
* dom/Document.cpp:
(WebCore::Document::commonTeardown):
(WebCore::Document::implicitClose):
(WebCore::Document::accessSVGExtensions):
* dom/Document.h:
* dom/Element.cpp:
(WebCore::Element::~Element):
(WebCore::Element::removedFrom):
* editing/markup.cpp:
(WebCore::isPlainTextMarkup):
* history/CachedFrame.cpp:
(WebCore::CachedFrameBase::restore):
* rendering/FilterEffectRenderer.cpp:
(WebCore::FilterEffectRenderer::buildReferenceFilter):
* rendering/svg/RenderSVGResource.cpp:
(WebCore::removeFromCacheAndInvalidateDependencies):
* rendering/svg/RenderSVGResourceContainer.cpp:
(WebCore::svgExtensionsFromElement):
* rendering/svg/RenderSVGResourceContainer.h:
(WebCore::getRenderSVGResourceContainerById):
(WebCore::getRenderSVGResourceById):
* rendering/svg/SVGResources.cpp:
(WebCore::registerPendingResource):
(WebCore::SVGResources::buildCachedResources):
* rendering/svg/SVGResourcesCache.cpp:
(WebCore::resourcesCacheFromRenderer):
(WebCore::SVGResourcesCache::resourceDestroyed):
* svg/SVGElement.cpp:
(WebCore::SVGElement::~SVGElement):
(WebCore::SVGElement::reportAttributeParsingError):
(WebCore::SVGElement::removedFrom):
(WebCore::SVGElement::viewportElement):
(WebCore::SVGElement::attributeChanged):
(WebCore::SVGElement::buildPendingResourcesIfNeeded):
(WebCore::SVGElement::accessDocumentSVGExtensions): Deleted.
* svg/SVGElement.h:
* svg/SVGFEConvolveMatrixElement.cpp:
(WebCore::SVGFEConvolveMatrixElement::parseAttribute):
* svg/SVGFEGaussianBlurElement.cpp:
(WebCore::SVGFEGaussianBlurElement::parseAttribute):
* svg/SVGFEImageElement.cpp:
(WebCore::SVGFEImageElement::clearResourceReferences):
(WebCore::SVGFEImageElement::buildPendingResource):
* svg/SVGFitToViewBox.cpp:
(WebCore::SVGFitToViewBox::parseViewBox):
* svg/SVGFontFaceElement.cpp:
(WebCore::SVGFontFaceElement::insertedInto):
(WebCore::SVGFontFaceElement::removedFrom):
* svg/SVGMPathElement.cpp:
(WebCore::SVGMPathElement::buildPendingResource):
(WebCore::SVGMPathElement::clearResourceReferences):
* svg/SVGPathElement.cpp:
(WebCore::SVGPathElement::parseAttribute):
(WebCore::SVGPathElement::invalidateMPathDependencies):
* svg/SVGPolyElement.cpp:
(WebCore::SVGPolyElement::parseAttribute):
* svg/SVGSVGElement.cpp:
(WebCore::SVGSVGElement::~SVGSVGElement):
(WebCore::SVGSVGElement::insertedInto):
(WebCore::SVGSVGElement::removedFrom):
* svg/SVGTRefElement.cpp:
(WebCore::SVGTRefElement::detachTarget):
(WebCore::SVGTRefElement::buildPendingResource):
* svg/SVGTextPathElement.cpp:
(WebCore::SVGTextPathElement::clearResourceReferences):
(WebCore::SVGTextPathElement::buildPendingResource):
* svg/SVGUseElement.cpp:
(WebCore::SVGUseElement::clearResourceReferences):
(WebCore::SVGUseElement::buildPendingResource):
(WebCore::SVGUseElement::toClipPath):
(WebCore::SVGUseElement::buildInstanceTree):
* svg/animation/SVGSMILElement.cpp:
(WebCore::SVGSMILElement::clearResourceReferences):
(WebCore::SVGSMILElement::buildPendingResource):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@173883 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Have Document::ensureTemplateDocument() return a reference
https://bugs.webkit.org/show_bug.cgi?id=137033

Reviewed by Ryosuke Niwa.

Have Document::ensureTemplateDocument() return a reference instead of
a pointer as it can never return null.

No new tests, no behavior change.

* dom/Document.cpp:
(WebCore::Document::ensureTemplateDocument):
* dom/Document.h:
* editing/markup.cpp:
(WebCore::createFragmentForInnerOuterHTML):
* html/HTMLTemplateElement.cpp:
(WebCore::HTMLTemplateElement::content):
(WebCore::HTMLTemplateElement::didMoveToNewDocument):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@173884 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Have DOMImplementation::document() and Element::attributes() return references
https://bugs.webkit.org/show_bug.cgi?id=137035

Reviewed by Andreas Kling.

Have DOMImplementation::document() and Element::attributes() return
references instead of pointers as they can never return null.

No new tests, no behavior change.

* bindings/scripts/CodeGeneratorJS.pm:
(GenerateImplementation):
Update the JS Bindings code generator to use WTF::getPtr() in a couple
more places so that the implementation can return references instead
of pointers even when [GenerateIsReachable=xxx] IDL extended attribute
is used.

* dom/DOMImplementation.h:
(WebCore::DOMImplementation::document):
* dom/Element.cpp:
(WebCore::Element::attributes):
* dom/Element.h:
(WebCore::Node::attributes):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@173885 268f45cc-cd09-0410-ab3c-d52691b4dbfc