Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: e20f666f105ba9f902201fb018ce20ceafe35fd8
https://github.com/WebKit/WebKit/commit/e20f666f105ba9f902201fb018ce20ceafe35fd8
Author: Nathan Solomon <[email protected]>
Date: 2026-03-10 (Tue, 10 Mar 2026)
Changed paths:
M Source/WebCore/platform/graphics/Font.h
M Source/WebCore/platform/graphics/FontCascade.h
M Source/WebCore/platform/graphics/FontCascadeFonts.h
Log Message:
-----------
Reduce Ref counting operations in TextUtil::width
rdar://170275347
https://bugs.webkit.org/show_bug.cgi?id=307738
Reviewed by Vitor Roriz.
Add NODELETE cachedPrimaryFont() and
cachedCanTakeFixedPitchFastContentMeasuring()
to reduce ref-count operations in TextUtil::width().
TextUtil::width() is a hot function with many ref()/deref(). Each call to
FontCascade::primaryFont() uses
protect(m_fonts) / protect(fontSelector()), causing
ref/deref operations per call. Similarly,
canTakeFixedPitchFastContentMeasuring() uses protect() internally.
Add cachedPrimaryFont() which returns const Font* directly
from the cached weak pointer, and cachedCanTakeFixedPitchFastContentMeasuring()
which reads the cached TriState without protect(). On cache hit,
this eliminates all ref counting. On the cache miss,
fall back to the existing ref-counted methods. Cache hit is much more common
than cache miss.
Accessing m_fonts without protect() and returning a raw pointer instead
of a Ref is safe because the FontCascade owns the FontCascadeFonts through
a RefPtr, and the FontCascadeFonts holds the Font alive through a
chain of strong references (FontRanges -> FontAccessor -> Font).
Since every caller of cachedPrimaryFont() is operating on a live
FontCascade, the returned Font pointer is valid for the duration of
the call.
The existing code already assumes m_fonts is non-null at the time of access
because
protect(m_fonts)->primaryFont() would crash on a null m_fonts,
since protect() of a null RefPtr returns a null RefPtr.
The NODELETE annotation verifies that the called method
cannot free the returned object's memory.
A/B testing shows this is a small performance improvement.
* Source/WebCore/platform/graphics/Font.h:
(WebCore::Font::spaceWidth const):
* Source/WebCore/platform/graphics/FontCascade.h:
(WebCore::FontCascade::primaryFont const):
(WebCore::FontCascade::canTakeFixedPitchFastContentMeasuring const):
* Source/WebCore/platform/graphics/FontCascadeFonts.h:
(WebCore::FontCascadeFonts::cachedCanTakeFixedPitchFastContentMeasuring const):
(WebCore::FontCascadeFonts::cachedPrimaryFont const):
Canonical link: https://commits.webkit.org/309020@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications