Skip to content

Commit

Permalink
Fix indexing performance issue
Browse files Browse the repository at this point in the history
If you open two source roots quickly, there's a "thrashing" effect where
each file added from the second root invalidates programCache and forces
the first root's error checking task to rebuild its program(s). When
this happens, error checking takes a long time: O(num files^2).

This can also happen if you delete many files from one root while
another root is error checking.

Fix this by adding/removing all indexables at once with the lock held.
  • Loading branch information
jeffrey-easyesi committed Mar 3, 2018
1 parent fc54734 commit e8eb8cf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
16 changes: 11 additions & 5 deletions src/netbeanstypescript/TSIndexerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@
package netbeanstypescript;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.prefs.Preferences;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.modules.parsing.api.Snapshot;
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.parsing.spi.indexing.Context;
import org.netbeans.modules.parsing.spi.indexing.CustomIndexer;
import org.netbeans.modules.parsing.spi.indexing.CustomIndexerFactory;
import org.netbeans.modules.parsing.spi.indexing.Indexable;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Pair;

/**
* This "indexer" doesn't really index anything, it's just a way to read all the TS files in a
Expand All @@ -78,15 +82,19 @@ protected void index(Iterable<? extends Indexable> files, Context context) {
if (root == null) {
return;
}
List<Pair<Indexable, Snapshot>> snapshots = new ArrayList<>();
for (Indexable indxbl: files) {
FileObject fo = root.getFileObject(indxbl.getRelativePath());
if (fo == null) continue;
if ("text/typescript".equals(FileUtil.getMIMEType(fo))) {
TSService.addFile(Source.create(fo).createSnapshot(), indxbl, context);
snapshots.add(Pair.of(indxbl, Source.create(fo).createSnapshot()));
} else if (fo.getNameExt().equals("tsconfig.json")) {
TSService.addFile(Source.create(fo).createSnapshot(), indxbl, context);
snapshots.add(Pair.of(indxbl, Source.create(fo).createSnapshot()));
}
}
if (! snapshots.isEmpty()) {
TSService.addFiles(snapshots, context);
}
}
};
}
Expand All @@ -105,9 +113,7 @@ public boolean supportsEmbeddedIndexers() {

@Override
public void filesDeleted(Iterable<? extends Indexable> deleted, Context context) {
for (Indexable i: deleted) {
TSService.removeFile(i, context);
}
TSService.removeFiles(deleted, context);
}

@Override
Expand Down
33 changes: 19 additions & 14 deletions src/netbeanstypescript/TSService.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.Pair;
import org.openide.util.RequestProcessor;

/**
Expand Down Expand Up @@ -270,7 +271,7 @@ private static class FileData {
String path;
}

static void addFile(Snapshot snapshot, Indexable indxbl, Context cntxt) {
static void addFiles(List<Pair<Indexable, Snapshot>> files, Context cntxt) {
lock.lock();
try {
URL rootURL = cntxt.getRootURI();
Expand All @@ -284,16 +285,18 @@ static void addFile(Snapshot snapshot, Indexable indxbl, Context cntxt) {
}
programs.put(rootURL, program);

FileData fi = new FileData();
fi.program = program;
fi.fileObject = snapshot.getSource().getFileObject();
fi.indexable = indxbl;
fi.path = fi.fileObject.getPath();
allFiles.put(fi.path, fi);

program.addFile(fi, snapshot, cntxt.checkForEditorModifications());
if (! cntxt.isAllFilesIndexing() && ! cntxt.checkForEditorModifications()) {
program.needCompileOnSave.add(fi.fileObject);
for (Pair<Indexable, Snapshot> item: files) {
FileData fi = new FileData();
fi.program = program;
fi.fileObject = item.second().getSource().getFileObject();
fi.indexable = item.first();
fi.path = fi.fileObject.getPath();
allFiles.put(fi.path, fi);

program.addFile(fi, item.second(), cntxt.checkForEditorModifications());
if (! cntxt.isAllFilesIndexing() && ! cntxt.checkForEditorModifications()) {
program.needCompileOnSave.add(fi.fileObject);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -302,14 +305,16 @@ static void addFile(Snapshot snapshot, Indexable indxbl, Context cntxt) {
}
}

static void removeFile(Indexable indxbl, Context cntxt) {
static void removeFiles(Iterable<? extends Indexable> indxbls, Context cntxt) {
lock.lock();
try {
ProgramData program = programs.get(cntxt.getRootURI());
if (program != null) {
try {
String path = program.removeFile(indxbl);
allFiles.remove(path);
for (Indexable indxbl: indxbls) {
String path = program.removeFile(indxbl);
allFiles.remove(path);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit e8eb8cf

Please sign in to comment.