Skip to content

Commit

Permalink
Cancel reindex requests instead of internally queueing them
Browse files Browse the repository at this point in the history
With the queue, the first reindex request for a series of them was
always sent and left to be handled completely, whilst only the last of
any subsequent requests was maintained in order to skip unnecessary
intermediate ones. This way the core did not have to play catch up.

The unfortunate part was that the first reindex request always went
through, even if the core was very busy handling other requests and
could not handle that first request until a while later, after which
that first request might also have been superseded. As the request had
already been sent, however, there was no way to cancel it.

As the core now has request cancellation support, it is simpler to let
all reindex requests pass through to the core and just cancel the ones
that aren't relevant. This automatically allows us to cancel even the
first request, at least if it wasn't being processed yet - but that was
also the case before.

In short, this should improve latency on slower hardware where many
actions are being performed simultaneously.

References #347
  • Loading branch information
Gert-dev committed Jan 25, 2018
1 parent aa26716 commit 3e150d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 30 deletions.
19 changes: 16 additions & 3 deletions lib/IndexingMediator.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Popover = require './Widgets/Popover'

CancellablePromise = require './CancellablePromise'

module.exports =

##*
Expand Down Expand Up @@ -37,7 +39,14 @@ class IndexingMediator
* @return {Promise}
###
reindex: (path, source, excludedPaths, fileExtensionsToIndex) ->
return new Promise (resolve, reject) =>
reindexCancellablePromise = null

cancelHandler = () =>
return if not reindexCancellablePromise?

reindexCancellablePromise.cancel()

executor = (resolve, reject) =>
@indexingEventEmitter.emit('php-integrator-base:indexing-started', {
path : path
})
Expand Down Expand Up @@ -69,13 +78,17 @@ class IndexingMediator
percentage : progress
})

return @proxy.reindex(
reindexCancellablePromise = @proxy.reindex(
path,
source,
progressStreamCallback,
excludedPaths,
fileExtensionsToIndex
).then(successHandler, failureHandler)
)

return reindexCancellablePromise.then(successHandler, failureHandler)

return new CancellablePromise(executor, cancelHandler)

###*
* Initializes the project.
Expand Down
33 changes: 7 additions & 26 deletions lib/ProjectManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class ProjectManager
* @param {String} fileName The file to index.
* @param {String|null} source The source code of the file to index.
*
* @return {Promise}
* @return {CancellablePromise}
###
performFileIndex: (project, fileName, source = null) ->
return @indexingMediator.reindex(
Expand All @@ -328,34 +328,15 @@ class ProjectManager
attemptFileIndex: (project, fileName, source = null) ->
return null if @isProjectIndexing()

if fileName not of @indexMap
@indexMap[fileName] = {
isBeingIndexed : true
nextIndexSource : null
}

else if @indexMap[fileName].isBeingIndexed
# This file is already being indexed, so keep track of the most recent changes so we can index any changes
# after the current indexing process finishes.
@indexMap[fileName].nextIndexSource = source
return null

@indexMap[fileName].isBeingIndexed = true

handler = () =>
@indexMap[fileName].isBeingIndexed = false
if fileName of @indexMap
@indexMap[fileName].cancel()

if @indexMap[fileName].nextIndexSource?
nextIndexSource = @indexMap[fileName].nextIndexSource
@indexMap[fileName] = @performFileIndex(project, fileName, source)

@indexMap[fileName].nextIndexSource = null

@attemptFileIndex(project, fileName, nextIndexSource)

successHandler = handler
failureHandler = handler
onIndexFinish = () =>
delete @indexMap[fileName]

return @performFileIndex(project, fileName, source).then(successHandler, failureHandler)
return @indexMap[fileName].then(onIndexFinish, onIndexFinish)

###*
* Indexes the current project asynchronously.
Expand Down
2 changes: 1 addition & 1 deletion lib/Proxy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ class Proxy
* @param {Array} excludedPaths A list of paths to exclude from indexing.
* @param {Array} fileExtensionsToIndex A list of file extensions (without leading dot) to index.
*
* @return {Promise}
* @return {CancellablePromise}
###
reindex: (path, source, progressStreamCallback, excludedPaths, fileExtensionsToIndex) ->
if typeof path == "string"
Expand Down

0 comments on commit 3e150d9

Please sign in to comment.