-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
packager: index files by dir for fast matching
Summary: This removes the call to `HasteFS#matchFiles()`, that has linear complexity. Instead, we index all the files by directory from `HasteFS` once loaded, a linear operation. Then, we can filter files from a particular directory much quicker. Reviewed By: davidaurelio Differential Revision: D4826721 fbshipit-source-id: c31a0ed9a354dbc7f2dcd56179b859e491faa16c
- Loading branch information
1 parent
5f37483
commit b210183
Showing
4 changed files
with
96 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const path = require('path'); | ||
|
||
/** | ||
* This is a way to find files quickly given a RegExp, in a specific directory. | ||
* This is must faster than iterating over all the files and matching both | ||
* directory and RegExp at the same time. | ||
* | ||
* This was first implemented to support finding assets fast, for which we know | ||
* the directory, but we want to identify all variants (ex. @2x, @1x, for | ||
* a picture's different definition levels). | ||
*/ | ||
class FilesByDirNameIndex { | ||
_filesByDirName: Map<string, Array<string>>; | ||
|
||
constructor(allFilePaths: Array<string>) { | ||
this._filesByDirName = new Map(); | ||
for (let i = 0; i < allFilePaths.length; ++i) { | ||
const filePath = allFilePaths[i]; | ||
const dirName = path.dirname(filePath); | ||
let dir = this._filesByDirName.get(dirName); | ||
if (dir === undefined) { | ||
dir = []; | ||
this._filesByDirName.set(dirName, dir); | ||
} | ||
dir.push(path.basename(filePath)); | ||
} | ||
} | ||
|
||
match(dirName: string, pattern: RegExp): Array<string> { | ||
const results = []; | ||
const dir = this._filesByDirName.get(dirName); | ||
if (dir === undefined) { | ||
return []; | ||
} | ||
for (let i = 0; i < dir.length; ++i) { | ||
if (pattern.test(dir[i])) { | ||
results.push(path.join(dirName, dir[i])); | ||
} | ||
} | ||
return results; | ||
} | ||
} | ||
|
||
module.exports = FilesByDirNameIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters