From 731c5903c2894df36174edc8facd39613d37e163 Mon Sep 17 00:00:00 2001 From: Devon Carew Date: Wed, 20 Apr 2016 11:26:24 -0700 Subject: [PATCH] cache the source lines when running analyze (#3436) --- .../lib/src/commands/analyze.dart | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 153655e638..57c40184fc 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -367,6 +367,8 @@ class AnalyzeCommand extends FlutterCommand { Set changedFiles = new Set(); // files about which we've complained that they changed + _SourceCache cache = new _SourceCache(10); + int membersMissingDocumentation = 0; List errorLines = output.toString().split('\n'); for (String errorLine in errorLines) { @@ -380,7 +382,7 @@ class AnalyzeCommand extends FlutterCommand { int colNumber = int.parse(groups[5]); try { File source = new File(filename); - List sourceLines = source.readAsLinesSync(); + List sourceLines = cache.getSourceFor(source); if (lineNumber > sourceLines.length) throw new FileChanged(); String sourceLine = sourceLines[lineNumber-1]; @@ -801,3 +803,20 @@ class AnalysisError implements Comparable { return '${severity.toLowerCase().padLeft(7)} $sep $message $sep $relativePath:$startLine:$startColumn'; } } + +class _SourceCache { + _SourceCache(this.cacheSize); + + final int cacheSize; + final Map> _lines = new LinkedHashMap>(); + + List getSourceFor(File file) { + if (!_lines.containsKey(file.path)) { + if (_lines.length >= cacheSize) + _lines.remove(_lines.keys.first); + _lines[file.path] = file.readAsLinesSync(); + } + + return _lines[file.path]; + } +}