Skip to content

Commit 4fa7bcd

Browse files
committed
Improve efficiency of if statements
1 parent 478adc0 commit 4fa7bcd

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

Source/WebCore/html/HTMLMediaElement.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,9 @@ void HTMLMediaElement::didMoveToNewDocument(Document& oldDocument, Document& new
952952
ASSERT_WITH_SECURITY_IMPLICATION(&document() == &newDocument);
953953

954954
// Handle lazy loading observer transfer between documents
955-
if (newDocument.settings().lazyMediaLoadingEnabled()) {
956-
oldDocument.lazyLoadMediaObserver().unobserve(*this, oldDocument);
957-
if (isLazyLoadable())
958-
LazyLoadMediaObserver::observe(*this);
959-
}
955+
oldDocument.lazyLoadMediaObserver().unobserve(*this, oldDocument);
956+
if (newDocument.settings().lazyMediaLoadingEnabled() && isLazyLoadable())
957+
LazyLoadMediaObserver::observe(*this);
960958

961959
if (m_shouldDelayLoadEvent) {
962960
oldDocument.decrementLoadEventDelayCount();
@@ -1037,7 +1035,7 @@ void HTMLMediaElement::attributeChanged(const QualifiedName& name, const AtomStr
10371035
// If a src attribute of a media element is set or changed, the user
10381036
// agent must invoke the media element's media element load algorithm.
10391037
if (!newValue.isNull()) {
1040-
if (isLazyLoadable() && document().settings().lazyMediaLoadingEnabled() && m_networkState == NETWORK_EMPTY)
1038+
if (m_networkState == NETWORK_EMPTY && document().settings().lazyMediaLoadingEnabled() && isLazyLoadable())
10411039
LazyLoadMediaObserver::observe(*this);
10421040
else
10431041
prepareForLoad();
@@ -1142,11 +1140,11 @@ void HTMLMediaElement::didFinishInsertingNode()
11421140

11431141
if (m_inActiveDocument && m_networkState == NETWORK_EMPTY) {
11441142
if (!attributeWithoutSynchronization(srcAttr).isEmpty()) {
1145-
if (isLazyLoadable() && document().settings().lazyMediaLoadingEnabled())
1143+
if (document().settings().lazyMediaLoadingEnabled() && isLazyLoadable())
11461144
LazyLoadMediaObserver::observe(*this);
11471145
else
11481146
prepareForLoad();
1149-
} else if (auto firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
1147+
} else if (RefPtr firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
11501148
// If there are source children, they will trigger sourceWasAdded() which handles lazy loading
11511149
// But if source children are already present when video is inserted, we need to check here 1
11521150
sourceWasAdded(*firstSource);
@@ -1230,8 +1228,11 @@ void HTMLMediaElement::removedFromAncestor(RemovalType removalType, ContainerNod
12301228
{
12311229
HTMLMEDIAELEMENT_RELEASE_LOG(REMOVEDFROMANCESTOR);
12321230

1233-
if (document().settings().lazyMediaLoadingEnabled())
1234-
document().lazyLoadMediaObserver().unobserve(*this, document());
1231+
{
1232+
Ref document = this->document();
1233+
if (document->settings().lazyMediaLoadingEnabled())
1234+
document->lazyLoadMediaObserver().unobserve(*this, document);
1235+
}
12351236

12361237
setInActiveDocument(false);
12371238
if (removalType.disconnectedFromDocument) {
@@ -1538,18 +1539,20 @@ bool HTMLMediaElement::hasLazyLoadableAttributeValue(StringView attributeValue)
15381539

15391540
bool HTMLMediaElement::isLazyLoadable() const
15401541
{
1541-
if (!document().frame() || !document().frame()->checkedScript()->canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
1542+
Ref document = this->document();
1543+
if (!document->frame() || !protect(document->frame()->script())->canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
15421544
return false;
15431545

1544-
if (document().paginated())
1546+
if (document->paginated())
15451547
return false;
15461548
return hasLazyLoadableAttributeValue(attributeWithoutSynchronization(HTMLNames::loadingAttr));
15471549
}
15481550

15491551
void HTMLMediaElement::loadDeferredMedia()
15501552
{
1551-
if (document().settings().lazyMediaLoadingEnabled())
1552-
document().lazyLoadMediaObserver().unobserve(*this, document());
1553+
Ref document = this->document();
1554+
if (document->settings().lazyMediaLoadingEnabled())
1555+
document->lazyLoadMediaObserver().unobserve(*this, document);
15531556

15541557
// Load the media - load() will handle both src attribute and source children
15551558
if (networkState() == NETWORK_EMPTY) {
@@ -1561,8 +1564,9 @@ void HTMLMediaElement::loadDeferredMedia()
15611564
void HTMLMediaElement::resumeLazyLoadingIfNeeded()
15621565
{
15631566
// Per HTML spec: user-initiated play() triggers lazy load resumption steps
1564-
if (document().settings().lazyMediaLoadingEnabled()) {
1565-
auto& observer = document().lazyLoadMediaObserver();
1567+
Ref document = this->document();
1568+
if (document->settings().lazyMediaLoadingEnabled()) {
1569+
auto& observer = document->lazyLoadMediaObserver();
15661570
if (observer.isObserved(*this))
15671571
loadDeferredMedia();
15681572
}
@@ -5900,7 +5904,7 @@ void HTMLMediaElement::sourceWasAdded(HTMLSourceElement& source)
59005904
// the media element's resource selection algorithm.
59015905
if (m_networkState == NETWORK_EMPTY) {
59025906
m_nextChildNodeToConsider = source;
5903-
if (isLazyLoadable() && document().settings().lazyMediaLoadingEnabled()) {
5907+
if (document().settings().lazyMediaLoadingEnabled() && isLazyLoadable()) {
59045908
LazyLoadMediaObserver::observe(*this);
59055909
return;
59065910
}

Source/WebCore/html/LazyLoadMediaObserver.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ class LazyMediaLoadIntersectionObserverCallback final : public IntersectionObser
7575

7676
void LazyLoadMediaObserver::observe(Element& element)
7777
{
78-
auto& observer = element.document().lazyLoadMediaObserver();
79-
auto* intersectionObserver = observer.intersectionObserver(protect(element.document()));
78+
Ref protectedDocument = element.document();
79+
auto& observer = protectedDocument->lazyLoadMediaObserver();
80+
RefPtr intersectionObserver = observer.intersectionObserver(protectedDocument);
8081
if (!intersectionObserver)
8182
return;
8283
intersectionObserver->observe(element);
@@ -104,7 +105,8 @@ IntersectionObserver* LazyLoadMediaObserver::intersectionObserver(Document& docu
104105

105106
bool LazyLoadMediaObserver::isObserved(Element& element) const
106107
{
107-
return m_observer && m_observer->isObserving(element);
108+
RefPtr observer = m_observer;
109+
return observer && observer->isObserving(element);
108110
}
109111

110112
}

Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "ImageBitmap.h"
6767
#include "ImageBuffer.h"
6868
#include "ImageData.h"
69+
#include "InspectorInstrumentation.h"
6970
#include "OffscreenCanvas.h"
7071
#include "PaintRenderingContext2D.h"
7172
#include "Path2D.h"
@@ -657,7 +658,7 @@ void CanvasRenderingContext2DBase::setLineCap(const String& stringValue)
657658
cap = CanvasLineCap::Square;
658659
else
659660
return;
660-
661+
661662
setLineCap(cap);
662663
}
663664

@@ -1289,7 +1290,7 @@ bool CanvasRenderingContext2DBase::isPointInPathInternal(const Path& path, doubl
12891290
{
12901291
if (!std::isfinite(x) || !std::isfinite(y))
12911292
return false;
1292-
1293+
12931294
if (!effectiveDrawingContext())
12941295
return false;
12951296
if (!hasInvertibleTransform()) [[unlikely]]
@@ -2232,7 +2233,7 @@ ExceptionOr<RefPtr<CanvasPattern>> CanvasRenderingContext2DBase::createPattern(C
22322233
ExceptionOr<RefPtr<CanvasPattern>> CanvasRenderingContext2DBase::createPattern(HTMLImageElement& imageElement, bool repeatX, bool repeatY)
22332234
{
22342235
CachedResourceHandle cachedImage = imageElement.cachedImage();
2235-
2236+
22362237
// If the image loading hasn't started or the image is not complete, it is not fully decodable.
22372238
if (!cachedImage || !imageElement.complete())
22382239
return nullptr;
@@ -2288,21 +2289,21 @@ ExceptionOr<RefPtr<CanvasPattern>> CanvasRenderingContext2DBase::createPattern(C
22882289

22892290
if (!copiedImage)
22902291
return Exception { ExceptionCode::InvalidStateError };
2291-
2292+
22922293
auto nativeImage = copiedImage->nativeImage();
22932294
if (!nativeImage)
22942295
return Exception { ExceptionCode::InvalidStateError };
22952296

22962297
return RefPtr<CanvasPattern> { CanvasPattern::create({ nativeImage.releaseNonNull() }, repeatX, repeatY, canvas.originClean()) };
22972298
}
2298-
2299+
22992300
#if ENABLE(VIDEO)
23002301

23012302
ExceptionOr<RefPtr<CanvasPattern>> CanvasRenderingContext2DBase::createPattern(HTMLVideoElement& videoElement, bool repeatX, bool repeatY)
23022303
{
23032304
if (videoElement.readyState() < HTMLMediaElement::HAVE_CURRENT_DATA)
23042305
return nullptr;
2305-
2306+
23062307
checkOrigin(&videoElement);
23072308
bool originClean = canvasBase().originClean();
23082309

@@ -2317,7 +2318,7 @@ ExceptionOr<RefPtr<CanvasPattern>> CanvasRenderingContext2DBase::createPattern(H
23172318
return nullptr;
23182319

23192320
videoElement.paintCurrentFrameInContext(imageBuffer->context(), FloatRect(FloatPoint(), size(videoElement)));
2320-
2321+
23212322
return RefPtr<CanvasPattern> { CanvasPattern::create({ imageBuffer.releaseNonNull() }, repeatX, repeatY, originClean) };
23222323
}
23232324

Source/WebCore/html/shadow/DataListButtonElement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Event.h"
3030
#include "EventNames.h"
3131
#include "MouseEvent.h"
32+
#include "RenderStyle+GettersInlines.h"
3233
#include "ResolvedStyle.h"
3334
#include "StyleAppearance.h"
3435
#include <wtf/TZoneMallocInlines.h>

0 commit comments

Comments
 (0)