Skip to content
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

Entries filtering performance optimization #238

Merged
merged 4 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}