Commit ad6a3def authored by Alex Turner's avatar Alex Turner Committed by Commit Bot

Use TaskRunnerTimer in FontMatchingMetrics

This should improve performance when fonts are quickly cycled as the
repeated calls to Time::Now() were (somewhat) slow.

Bug: 1110204
Change-Id: I861288f9470157cc349e41a0e124c95cba6a381a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2333378
Commit-Queue: Alex Turner <alexmt@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarAsanka Herath <asanka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796986}
parent ce5e39ca
......@@ -7118,7 +7118,8 @@ FontMatchingMetrics* Document::GetFontMatchingMetrics() {
if (font_matching_metrics_)
return font_matching_metrics_.get();
font_matching_metrics_ = std::make_unique<FontMatchingMetrics>(
IsInMainFrame(), UkmRecorder(), UkmSourceID());
IsInMainFrame(), UkmRecorder(), UkmSourceID(),
GetTaskRunner(TaskType::kInternalDefault));
return font_matching_metrics_.get();
}
......
......@@ -21,6 +21,7 @@ include_rules = [
"+third_party/blink/renderer/platform/wtf/shared_buffer.h",
"+third_party/blink/renderer/platform/testing",
"+third_party/blink/renderer/platform/text",
"+third_party/blink/renderer/platform/timer.h",
"+third_party/blink/renderer/platform/web_test_support.h",
"+third_party/blink/renderer/platform/wtf",
"+ui/base/mojom/attributed_string.mojom-blink.h",
......
......@@ -28,12 +28,18 @@ HashSet<T> SetIntersection(const HashSet<T>& a, const HashSet<T>& b) {
namespace blink {
FontMatchingMetrics::FontMatchingMetrics(bool top_level,
ukm::UkmRecorder* ukm_recorder,
ukm::SourceId source_id)
FontMatchingMetrics::FontMatchingMetrics(
bool top_level,
ukm::UkmRecorder* ukm_recorder,
ukm::SourceId source_id,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: top_level_(top_level),
ukm_recorder_(ukm_recorder),
source_id_(source_id) {
source_id_(source_id),
identifiability_metrics_timer_(
task_runner,
this,
&FontMatchingMetrics::IdentifiabilityMetricsTimerFired) {
// Estimate of average page font use from anecdotal browsing session.
constexpr unsigned kEstimatedFontCount = 7;
local_fonts_succeeded_.ReserveCapacityForSize(kEstimatedFontCount);
......@@ -182,16 +188,14 @@ void FontMatchingMetrics::PublishUkmMetrics() {
}
void FontMatchingMetrics::OnFontLookup() {
if (!time_of_earliest_unpublished_font_lookup_) {
time_of_earliest_unpublished_font_lookup_ = base::Time::Now();
return;
if (!identifiability_metrics_timer_.IsActive()) {
identifiability_metrics_timer_.StartOneShot(base::TimeDelta::FromMinutes(1),
FROM_HERE);
}
}
if (base::Time::Now() - *time_of_earliest_unpublished_font_lookup_ >=
base::TimeDelta::FromMinutes(1)) {
PublishIdentifiabilityMetrics();
time_of_earliest_unpublished_font_lookup_ = base::Time::Now();
}
void FontMatchingMetrics::IdentifiabilityMetricsTimerFired(TimerBase*) {
PublishIdentifiabilityMetrics();
}
void FontMatchingMetrics::PublishAllMetrics() {
......
......@@ -12,6 +12,7 @@
#include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string_hash.h"
......@@ -161,7 +162,8 @@ class PLATFORM_EXPORT FontMatchingMetrics {
public:
FontMatchingMetrics(bool top_level,
ukm::UkmRecorder* ukm_recorder,
ukm::SourceId source_id);
ukm::SourceId source_id,
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
// Called when a page attempts to match a font family, and the font family is
// available.
......@@ -232,6 +234,8 @@ class PLATFORM_EXPORT FontMatchingMetrics {
void PublishUkmMetrics();
private:
void IdentifiabilityMetricsTimerFired(TimerBase*);
// Get a hash that uniquely represents the font data. Returns 0 if |font_data|
// is nullptr.
uint64_t GetHashForFontData(SimpleFontData* font_data);
......@@ -272,9 +276,7 @@ class PLATFORM_EXPORT FontMatchingMetrics {
ukm::UkmRecorder* const ukm_recorder_;
const ukm::SourceId source_id_;
// Records when the first font lookup occurred since the last call to
// PublishIdentifiablityMetrics(), if any.
base::Optional<base::Time> time_of_earliest_unpublished_font_lookup_;
TaskRunnerTimer<FontMatchingMetrics> identifiability_metrics_timer_;
};
} // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment