Skip to content

Commit

Permalink
Merge pull request #238 from atom-community/performance
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Nov 11, 2020
2 parents 749cdaa + 33311f4 commit 2f45900
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/paths-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import gitIgnoreParser from 'git-ignore-parser';
import _ from 'underscore-plus'
import minimatch from 'minimatch'
import { Directory, File } from 'atom'
import { execPromise } from './utils'
import { execPromise, union } from './utils'

export default class PathsCache extends EventEmitter {
constructor () {
super()

const rebuildCacheBound = this.rebuildCache.bind(this)
this._projectChangeWatcher = atom.project.onDidChangePaths(rebuildCacheBound)

const _onDidChangeFilesBound = this._onDidChangeFiles.bind(this)
this._projectWatcher = atom.project.onDidChangeFiles(_onDidChangeFilesBound)

Expand Down Expand Up @@ -154,11 +154,17 @@ export default class PathsCache extends EventEmitter {
const entries = await this._getDirectoryEntries(directory)
if (this._cancelled) return []

// Filter: Files that are not ignored
const filePaths = entries
.filter(entry => entry instanceof File)
.map(entry => entry.path)
.filter(path => !this._isPathIgnored(path))
// Filter: Files and Directories that are not ignored
let filePaths = []
let directories = []
for (let i = 0, len = entries.length; i < len; i++) {
const entry = entries[i]
if (entry instanceof File && !this._isPathIgnored(entry.path)) {
filePaths.push(entry.path)
} else if (entry instanceof Directory && !this._isPathIgnored(entry.path)) {
directories.push(entry)
}
}

// Merge file paths into existing array (which contains *all* file paths)
let filePathsArray = this._filePathsByProjectDirectory.get(projectDirectory.path) || []
Expand All @@ -178,19 +184,15 @@ export default class PathsCache extends EventEmitter {
}

this._filePathsByProjectDirectory.set(projectDirectory.path,
_.union(filePathsArray, filePaths)
union(filePathsArray, filePaths)
)

// Merge file paths into existing array (which contains file paths for a specific directory)
filePathsArray = this._filePathsByDirectory.get(directory.path) || []
this._filePathsByDirectory.set(directory.path,
_.union(filePathsArray, filePaths)
union(filePathsArray, filePaths)
)

const directories = entries
.filter(entry => entry instanceof Directory)
.filter(entry => !this._isPathIgnored(entry.path))

return Promise.all(directories.map(directory =>
this._cacheDirectoryFilePaths(projectDirectory, directory)
))
Expand Down
18 changes: 18 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,21 @@ export function execPromise(cmd, options) {
});
});
}

// fast merge function
// https://uilicious.com/blog/javascript-array-push-is-945x-faster-than-array-concat/
export function merge(arr1: Array<any>, arr2: Array<any>) {
if (!arr2.length) return
Array.prototype.push.apply(arr1, arr2)
}

// get unique entries of an array
export function unique(arr: Array<any>) {
return [...new Set(arr)]
}

// fast union function (replacement for _.union)
export function union(arr1: Array<any>, arr2: Array<any>) {
merge(arr1, arr2)
return unique(arr1)
}

0 comments on commit 2f45900

Please sign in to comment.