Skip to content

Commit

Permalink
🐎 Implement a basic cache of editor dimension during update
Browse files Browse the repository at this point in the history
It reduces the cost of computing the height of the text editor several
time during the update.
  • Loading branch information
abe33 committed Nov 17, 2015
1 parent 71b59e4 commit 5aeb5ce
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
19 changes: 15 additions & 4 deletions lib/adapters/beta-adapter.coffee
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
# Internal:
# Internal:
module.exports =
class BetaAdater
constructor: (@textEditor) ->
@textEditorElement = atom.views.getView(@textEditor)

enableCache: -> @useCache = true

clearCache: ->
@useCache = false
delete @heightCache
delete @scrollTopCache
delete @scrollLeftCache
delete @maxScrollTopCache

onDidChangeScrollTop: (callback) ->
@textEditorElement.onDidChangeScrollTop(callback)

onDidChangeScrollLeft: (callback) ->
@textEditorElement.onDidChangeScrollLeft(callback)

getHeight: ->
return @heightCache ?= @textEditorElement.getHeight() if @useCache
@textEditorElement.getHeight()

getScrollTop: ->
return @scrollTopCache ?= @textEditorElement.getScrollTop() if @useCache
@textEditorElement.getScrollTop()

setScrollTop: (scrollTop) ->
@textEditorElement.setScrollTop(scrollTop)

getScrollLeft: ->
return @scrollLeftCache ?= @textEditorElement.getScrollLeft() if @useCache
@textEditorElement.getScrollLeft()

getHeightWithoutScrollPastEnd: ->
@textEditor.displayBuffer.getLineHeightInPixels()

getMaxScrollTop: ->
return @maxScrollTopCache if @maxScrollTopCache? and @useCache
maxScrollTop = @textEditorElement.getScrollHeight() - @getHeight()
lineHeight = @textEditor.getLineHeightInPixels()

maxScrollTop -= @getHeight() - 3 * lineHeight if @scrollPastEnd
return @maxScrollTopCache = maxScrollTop if @useCache
maxScrollTop
19 changes: 15 additions & 4 deletions lib/adapters/legacy-adapter.coffee
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
# Internal:
# Internal:
module.exports =
class LegacyAdater
constructor: (@textEditor) ->

enableCache: -> @useCache = true

clearCache: ->
@useCache = false
delete @heightCache
delete @scrollTopCache
delete @scrollLeftCache
delete @maxScrollTopCache

onDidChangeScrollTop: (callback) ->
@textEditor.onDidChangeScrollTop(callback)

onDidChangeScrollLeft: (callback) ->
@textEditor.onDidChangeScrollLeft(callback)

getHeight: ->
return @heightCache ?= @textEditor.getHeight() if @useCache
@textEditor.getHeight()

getScrollTop: ->
return @scrollTopCache ?= @textEditor.getScrollTop() if @useCache
@textEditor.getScrollTop()

setScrollTop: (scrollTop) ->
@textEditor.setScrollTop(scrollTop)

getScrollLeft: ->
return @scrollLeftCache ?= @textEditor.getScrollLeft() if @useCache
@textEditor.getScrollLeft()

getHeightWithoutScrollPastEnd: ->
@textEditor.displayBuffer.getLineHeightInPixels()

getMaxScrollTop: ->
return @maxScrollTopCache if @maxScrollTopCache? and @useCache
maxScrollTop = @textEditor.displayBuffer.getMaxScrollTop()
lineHeight = @textEditor.getLineHeightInPixels()

maxScrollTop -= @getHeight() - 3 * lineHeight if @scrollPastEnd
return @maxScrollTopCache = maxScrollTop if @useCache
maxScrollTop
10 changes: 7 additions & 3 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CanvasDrawer = require './mixins/canvas-drawer'

MinimapQuickSettingsElement = null

SPEC_MODE = atom.inSpecMode()

# Public: The {MinimapElement} is the view meant to render a {Minimap} instance
# in the DOM.
#
Expand Down Expand Up @@ -376,6 +378,7 @@ class MinimapElement
# Internal: Performs the actual {MinimapElement} update.
update: ->
return unless @attached and @isVisible() and @minimap?
@minimap.enableCache()

visibleAreaLeft = @minimap.getTextEditorScaledScrollLeft()
visibleAreaTop = @minimap.getTextEditorScaledScrollTop() - @minimap.getScrollTop()
Expand All @@ -386,7 +389,7 @@ class MinimapElement
else
@style.flexBasis = null

if atom.inSpecMode()
if SPEC_MODE
@applyStyles @visibleArea,
width: visibleWidth + 'px'
height: @minimap.getTextEditorScaledHeight() + 'px'
Expand All @@ -406,7 +409,7 @@ class MinimapElement
canvasTransform = @makeTranslate(0, canvasTop)
canvasTransform += " " + @makeScale(1 / devicePixelRatio) if devicePixelRatio isnt 1

if atom.inSpecMode()
if SPEC_MODE
@applyStyles @canvas, top: canvasTop + 'px'
else
@applyStyles @canvas, transform: canvasTransform
Expand All @@ -419,7 +422,7 @@ class MinimapElement
indicatorHeight = minimapScreenHeight * (minimapScreenHeight / @minimap.getHeight())
indicatorScroll = (minimapScreenHeight - indicatorHeight) * @minimap.getCapedTextEditorScrollRatio()

if atom.inSpecMode()
if SPEC_MODE
@applyStyles @scrollIndicator,
height: indicatorHeight + 'px'
top: indicatorScroll + 'px'
Expand All @@ -431,6 +434,7 @@ class MinimapElement
@disposeScrollIndicator() if not @minimap.canScroll()

@updateCanvas()
@minimap.clearCache()

# Defines whether to render the code highlights or not.
#
Expand Down
6 changes: 6 additions & 0 deletions lib/minimap.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,9 @@ class Minimap

# Internal: Emits a change events with the passed-in changes as data.
emitChanges: (changes) -> @emitter.emit('did-change', changes)

# Internal:
enableCache: -> @adapter.enableCache()

# Internal:
clearCache: -> @adapter.clearCache()
4 changes: 3 additions & 1 deletion lib/mixins/decoration-management.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class DecorationManagement extends Mixin

Decoration ?= require '../decoration'

getDecorations: -> decoration for id,decoration of @decorationsById

# Registers an event listener to the `did-add-decoration` event.
#
# callback - The {Function} to call when the event is triggered.
Expand Down Expand Up @@ -231,7 +233,7 @@ class DecorationManagement extends Mixin
#
# oldStart - The row index of the first range start.
# oldEnd - The row index of the first range end.
# newStart - The row index of the second range start.
# newStart - The row index of the second range start.
# newEnd - The row index of the second range end.
#
# Returns an {Array}.
Expand Down

0 comments on commit 5aeb5ce

Please sign in to comment.