-
Notifications
You must be signed in to change notification settings - Fork 482
Draw bar charts in custom marker tracks faster. #4711
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ import { assertExhaustiveCheck } from 'firefox-profiler/utils/flow'; | |
|
|
||
| import type { SizeProps } from 'firefox-profiler/components/shared/WithSize'; | ||
| import type { ConnectedProps } from 'firefox-profiler/utils/connect'; | ||
| import { timeCode } from 'firefox-profiler/utils/time-code'; | ||
|
|
||
| import './TrackCustomMarker.css'; | ||
|
|
||
|
|
@@ -226,6 +227,22 @@ class TrackCustomMarkerCanvas extends React.PureComponent<CanvasProps> { | |
| // | ||
| ctx.strokeStyle = _getStrokeColor(color || TRACK_MARKER_DEFAULT_COLOR); | ||
|
|
||
| const getX = (marker) => | ||
| Math.round((marker.start - rangeStart) * millisecondWidth); | ||
| const getY = (i) => { | ||
| const unitValue = _calculateUnitValue( | ||
| type, | ||
| minNumber, | ||
| maxNumber, | ||
| samples[i] | ||
| ); | ||
| // Add on half the stroke's line width so that it won't be cut | ||
| // off the edge of the graph. | ||
| return Math.round( | ||
| deviceHeight - deviceHeight * unitValue - deviceLineHalfWidth | ||
| ); | ||
| }; | ||
|
|
||
| switch (type) { | ||
| case 'line': | ||
| case 'line-filled': { | ||
|
|
@@ -247,16 +264,8 @@ class TrackCustomMarkerCanvas extends React.PureComponent<CanvasProps> { | |
| const marker = getMarker(collectedSamples.markerIndexes[i]); | ||
| // Create a path for the top of the chart. This is the line that | ||
| // will have a stroke applied to it. | ||
| x = (marker.start - rangeStart) * millisecondWidth; | ||
| // Add on half the stroke's line width so that it won't be cut | ||
| // off the edge of the graph. | ||
| const unitValue = _calculateUnitValue( | ||
| type, | ||
| minNumber, | ||
| maxNumber, | ||
| samples[i] | ||
| ); | ||
| y = deviceHeight - deviceHeight * unitValue - deviceLineHalfWidth; | ||
| x = getX(marker); | ||
| y = getY(i); | ||
| if (i === sampleStart) { | ||
| // This is the first iteration, only move the line, do not draw it. | ||
| // Also remember this first X, as the bottom of the graph will need | ||
|
|
@@ -301,32 +310,32 @@ class TrackCustomMarkerCanvas extends React.PureComponent<CanvasProps> { | |
| ctx.fillStyle = ctx.strokeStyle; | ||
|
|
||
| for (let i = sampleStart; i < sampleEnd; i++) { | ||
| const marker = getMarker(collectedSamples.markerIndexes[i]); | ||
| const x = Math.round( | ||
| (marker.start - rangeStart) * millisecondWidth | ||
| ); | ||
|
|
||
| const unitValue = _calculateUnitValue( | ||
| type, | ||
| minNumber, | ||
| maxNumber, | ||
| samples[i] | ||
| ); | ||
| const y = | ||
| deviceHeight - deviceHeight * unitValue - deviceLineHalfWidth; | ||
|
|
||
| if (marker.end) { | ||
| const x2 = Math.round( | ||
| (marker.end - rangeStart) * millisecondWidth | ||
| let marker = getMarker(collectedSamples.markerIndexes[i]); | ||
| const x = getX(marker); | ||
| let y = getY(i); | ||
|
|
||
| // If we have multiple markers to draw on the same horizontal pixel, | ||
| // draw only the one with the maximum value to save time. | ||
| while (i + 1 < sampleEnd) { | ||
| const nextMarker = getMarker( | ||
| collectedSamples.markerIndexes[i + 1] | ||
| ); | ||
| if (x2 >= x + 1) { | ||
| ctx.fillRect(x, y, x2 - x, deviceHeight - y); | ||
| continue; | ||
| if (getX(nextMarker) !== x) { | ||
| break; | ||
| } | ||
|
|
||
| marker = nextMarker; | ||
| y = Math.min(y, getY(++i)); | ||
| } | ||
| ctx.moveTo(x, deviceHeight); | ||
| ctx.lineTo(x, y); | ||
| ctx.stroke(); | ||
|
|
||
| const x2 = marker.end | ||
| ? Math.max( | ||
| x + 1, | ||
| Math.round((marker.end - rangeStart) * millisecondWidth) | ||
| ) | ||
| : x + 1; | ||
|
|
||
| ctx.fillRect(x, y, x2 - x, deviceHeight - y); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice improvement, thanks for investigating this! |
||
| } | ||
| break; | ||
| default: | ||
|
|
@@ -353,7 +362,9 @@ class TrackCustomMarkerCanvas extends React.PureComponent<CanvasProps> { | |
| this._requestedAnimationFrame = false; | ||
| const canvas = this._canvas; | ||
| if (canvas) { | ||
| this.drawCanvas(canvas); | ||
| timeCode('TrackCustomMarkerCanvas render', () => { | ||
| this.drawCanvas(canvas); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to get the max value with the Math.max here instead of min?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want the max value, which (unless I'm confused) is the min y (as y = 0 would mean top of the canvas).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh welp right, I was the one who's confused not you. Sorry about the confusion!