File: layout_shift_normalization.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 5,697 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/page_load_metrics/browser/layout_shift_normalization.h"

namespace page_load_metrics {
constexpr auto NEW_SHIFT_BUFFER_WINDOW_DURATION = base::Seconds(5);
constexpr auto MAX_SHIFT_BUFFER_SIZE = 300;

LayoutShiftNormalization::LayoutShiftNormalization() = default;
LayoutShiftNormalization::~LayoutShiftNormalization() = default;

void LayoutShiftNormalization::AddNewLayoutShifts(
    const std::vector<page_load_metrics::mojom::LayoutShiftPtr>& new_shifts,
    base::TimeTicks current_time,
    float cumulative_layout_shift_score) {
  if (new_shifts.empty() || normalized_cls_data_.data_tainted)
    return;

  if ((current_time - new_shifts[0]->layout_shift_time >
       NEW_SHIFT_BUFFER_WINDOW_DURATION) ||
      (recent_layout_shifts_.size() + new_shifts.size() >
       MAX_SHIFT_BUFFER_SIZE)) {
    // We can't keep all layout shifts all the time especially for long lived
    // pages. So we allow a layout shift to be delivered to the browser
    // process within 5 seconds and set a limit for the number of shifts we can
    // store in the browser process. If the delivery latency of a layout shift
    // is bigger than 5s, we can't process this layout shift and get the correct
    // normalization results, which means we should not report the results in
    // UKM.
    normalized_cls_data_.data_tainted = true;
    return;
  }

  // Append all shift data.
  for (const auto& layout_shift : new_shifts) {
    recent_layout_shifts_.emplace_back(layout_shift->layout_shift_time,
                                       layout_shift->layout_shift_score);
  }

  // At this point, the vector contains two sorted subvectors, and we can do an
  // in-place merge to create a sorted vector.
  std::inplace_merge(recent_layout_shifts_.begin(),
                     recent_layout_shifts_.end() - new_shifts.size(),
                     recent_layout_shifts_.end());

  // Now that we have a single sorted list of shifts, we can find the partition
  // point for old shifts which we are ready to delete
  auto first_non_stale = std::upper_bound(
      recent_layout_shifts_.begin(), recent_layout_shifts_.end(),
      current_time - NEW_SHIFT_BUFFER_WINDOW_DURATION,
      [](auto time, auto const& shift) { return time < shift.first; });

  // Update sliding and session window CLS.
  UpdateWindowCLS(recent_layout_shifts_.begin(), first_non_stale,
                  recent_layout_shifts_.end(), cumulative_layout_shift_score);

  // Finally, remove the stale shifts at this point.
  recent_layout_shifts_.erase(recent_layout_shifts_.begin(), first_non_stale);
}

void LayoutShiftNormalization::ClearAllLayoutShifts() {
  normalized_cls_data_ = NormalizedCLSData();
  recent_layout_shifts_.clear();
  session_gap1000ms_max5000ms_ = SessionWindow();
}

void LayoutShiftNormalization::UpdateSessionWindow(
    SessionWindow* session_window,
    base::TimeDelta gap,
    base::TimeDelta max_duration,
    std::vector<std::pair<base::TimeTicks, double>>::const_iterator begin,
    std::vector<std::pair<base::TimeTicks, double>>::const_iterator end,
    float& max_score) {
  for (auto it = begin; it != end; ++it) {
    if ((it->first - session_window->last_time > gap) ||
        (it->first - session_window->start_time > max_duration)) {
      session_window->start_time = it->first;
      session_window->layout_shift_score = 0;
    }
    session_window->last_time = it->first;
    session_window->layout_shift_score += it->second;
    max_score = std::max(max_score, session_window->layout_shift_score);
  }
}

// This function will update layout shift normalization results for sliding
// windows and session windows twice. The first update is processing stale
// layout shifts (current_time - layout_shift_time >= 5s) in
// recent_layout_shifts_. The second update is continuing the first update by
// processing remaining layout shifts in recent_layout_shifts_. Processing
// layout shifts in order is very important to our algorithm or we may
// miscalculate the number of windows or layout shift score for a window.
// We assume the browser process can receive any layout shift within 5 seconds,
// so there might be some non-stale layout shifts on the way and a newer layout
// shift can be received earlier than an old one. This is one reason why we
// update layout shift normalization twice. Another reason is we can't updating
// normalization on demand when we record UKM because of constant class objects.
// The first update is for updating the normalization for all stale layout shift
// we received. The second update is for all layout shifts we received so far in
// case we need to record UKM right away.
void LayoutShiftNormalization::UpdateWindowCLS(
    std::vector<std::pair<base::TimeTicks, double>>::const_iterator first,
    std::vector<std::pair<base::TimeTicks, double>>::const_iterator
        first_non_stale,
    std::vector<std::pair<base::TimeTicks, double>>::const_iterator last,
    float cumulative_layout_shift_score) {
  // Update Session Windows.
  UpdateSessionWindow(
      &session_gap1000ms_max5000ms_, base::Milliseconds(1000),
      base::Milliseconds(5000), first, first_non_stale,
      normalized_cls_data_.session_windows_gap1000ms_max5000ms_max_cls);
  auto tmp_session_gap1000ms_max5000ms = session_gap1000ms_max5000ms_;

  UpdateSessionWindow(
      &tmp_session_gap1000ms_max5000ms, base::Milliseconds(1000),
      base::Milliseconds(5000), first_non_stale, last,
      normalized_cls_data_.session_windows_gap1000ms_max5000ms_max_cls);
}
}  // namespace page_load_metrics