Skip to content

Commit

Permalink
fix: take the const decorations out of loop
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 2, 2021
1 parent 1396a49 commit c3c1e5e
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/mixins/canvas-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ export default class CanvasDrawer extends Mixin {
const maxTokensInOneLine = this.maxTokensInOneLine

this.updateTokensLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, context, editor, editorElement, displayCodeHighlights, ignoreWhitespacesInTokens, maxTokensInOneLine)
this.updateBackDecorationsLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.updateFrontDecorationsLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)

const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)

this.updateBackDecorationsLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)
this.updateFrontDecorationsLayer(firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)

this.pendingChanges = []
this.pendingBackDecorationChanges = []
Expand Down Expand Up @@ -226,9 +229,10 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {number} canvasHeight this.tokensLayer.getSize().height
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {Array<Decoration>} decorations
* @access private
*/
updateBackDecorationsLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement) {
updateBackDecorationsLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations) {
const intactRanges = computeIntactRanges(firstRow, lastRow, this.pendingBackDecorationChanges, this.offscreenFirstRow, this.offscreenLastRow)

// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
Expand All @@ -238,7 +242,7 @@ export default class CanvasDrawer extends Mixin {
this.backLayer.clearCanvas()

if (intactRanges.length === 0) {
this.drawBackDecorationsForLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawBackDecorationsForLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
Expand All @@ -254,12 +258,12 @@ export default class CanvasDrawer extends Mixin {
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]

this.drawBackDecorationsForLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawBackDecorationsForLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)

currentRow = range.end
}
if (currentRow <= lastRow) {
this.drawBackDecorationsForLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawBackDecorationsForLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)
}
}

Expand All @@ -280,9 +284,10 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {number} canvasHeight this.tokensLayer.getSize().height
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {Array<Decoration>} decorations
* @access private
*/
updateFrontDecorationsLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement) {
updateFrontDecorationsLayer (firstRow, lastRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations) {
const intactRanges = computeIntactRanges(firstRow, lastRow, this.pendingFrontDecorationChanges, this.offscreenFirstRow, this.offscreenLastRow)

// NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately.
Expand All @@ -292,7 +297,7 @@ export default class CanvasDrawer extends Mixin {
this.frontLayer.clearCanvas()

if (intactRanges.length === 0) {
this.drawFrontDecorationsForLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawFrontDecorationsForLines(firstRow, lastRow, 0, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)
} else {
for (let j = 0, len = intactRanges.length; j < len; j++) {
const intact = intactRanges[j]
Expand All @@ -308,12 +313,12 @@ export default class CanvasDrawer extends Mixin {
for (let i = 0, len = intactRanges.length; i < len; i++) {
const range = intactRanges[i]

this.drawFrontDecorationsForLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawFrontDecorationsForLines(currentRow, range.start, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)

currentRow = range.end
}
if (currentRow <= lastRow) {
this.drawFrontDecorationsForLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement)
this.drawFrontDecorationsForLines(currentRow, lastRow, currentRow - firstRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations)
}
}

Expand Down Expand Up @@ -474,12 +479,12 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {number} canvasHeight this.tokensLayer.getSize().height
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {Array<Decoration>} decorations
* @access private
*/
drawBackDecorationsForLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement) {
drawBackDecorationsForLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations) {
if (firstRow > lastRow) { return }

const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)
const renderData = {
context: this.backLayer.context,
canvasWidth,
Expand Down Expand Up @@ -521,12 +526,12 @@ export default class CanvasDrawer extends Mixin {
* @param {number} canvasWidth this.tokensLayer.getSize().width
* @param {number} canvasHeight this.tokensLayer.getSize().height
* @param {TextEditorElement} editorElement this.minimap.getTextEditorElement()
* @param {Array<Decoration>} decorations
* @access private
*/
drawFrontDecorationsForLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement) {
drawFrontDecorationsForLines (firstRow, lastRow, offsetRow, lineHeight, charHeight, charWidth, canvasWidth, canvasHeight, editorElement, decorations) {
if (firstRow > lastRow) { return }

const decorations = this.minimap.decorationsByTypeThenRows(firstRow, lastRow)
const renderData = {
context: this.frontLayer.context,
canvasWidth,
Expand Down

0 comments on commit c3c1e5e

Please sign in to comment.