Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduling Profiler: Add network measures #22112

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ export function trimText(
text: string,
width: number,
): string | null {
for (let i = text.length - 1; i >= 0; i--) {
const trimmedText = i === text.length - 1 ? text : text.substr(0, i) + '…';
const maxIndex = text.length - 1;

let startIndex = 0;
let stopIndex = maxIndex;

let longestValidIndex = 0;
let longestValidText = null;

// Trimming long text could be really slow if we decrease only 1 character at a time.
// Trimming with more of a binary search approach is faster in the worst cases.
while (startIndex <= stopIndex) {
bvaughn marked this conversation as resolved.
Show resolved Hide resolved
const currentIndex = Math.floor((startIndex + stopIndex) / 2);
const trimmedText =
currentIndex === maxIndex ? text : text.substr(0, currentIndex) + '…';

if (getTextWidth(context, trimmedText) <= width) {
return trimmedText;
if (longestValidIndex < currentIndex) {
longestValidIndex = currentIndex;
longestValidText = trimmedText;
}

startIndex = currentIndex + 1;
} else {
stopIndex = currentIndex - 1;
}
}

return null;
return longestValidText;
}

type TextConfig = {|
Expand Down