diff options
author | Joerg Kubitz | 2021-09-09 07:02:58 +0000 |
---|---|---|
committer | Mickael Istria | 2021-09-09 13:33:39 +0000 |
commit | 90da03e2e035d5d1b6404c463de8a0a5fbaa0f43 (patch) | |
tree | d51d157983de65012fdaeb5d3f2e86fecf95f585 | |
parent | e405e0ca9ed28fa2d5c08dc7fca89e42a8cedd53 (diff) | |
download | eclipse.platform.text-90da03e2e035d5d1b6404c463de8a0a5fbaa0f43.tar.gz eclipse.platform.text-90da03e2e035d5d1b6404c463de8a0a5fbaa0f43.tar.xz eclipse.platform.text-90da03e2e035d5d1b6404c463de8a0a5fbaa0f43.zip |
Bug 575893 - [performance] improve file search: file sort
Avoid file.getLocation() during sort: only calculate location once.
Change-Id: Ic0953c430385c74d4b879347ac1699f38c9099d5
Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/185194
Tested-by: Platform Bot <platform-bot@eclipse.org>
Reviewed-by: Mickael Istria <mistria@redhat.com>
-rw-r--r-- | org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java index 492de0e9c79..b02ee9a5a84 100644 --- a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java +++ b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java @@ -22,11 +22,13 @@ import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -316,6 +318,21 @@ public class TextSearchVisitor { fIsLightweightAutoRefresh= Platform.getPreferencesService().getBoolean(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PREF_LIGHTWEIGHT_AUTO_REFRESH, false, null); } + /** + * Just a record pair to avoid multiple file.getLocation() calls during + * sort. + **/ + private static final class FileWithCachedLocation { + final IFile file; + final String location; // cached + + FileWithCachedLocation(IFile file) { + this.file = file; + IPath path = file.getLocation(); // invokes slow OS operation + this.location = path == null ? null : path.toString(); + } + } + public IStatus search(IFile[] files, IProgressMonitor monitor) { if (files.length == 0) { return fStatus; @@ -387,26 +404,12 @@ public class TextSearchVisitor { fCollector.beginReporting(); Map<IFile, IDocument> documentsInEditors= PlatformUI.isWorkbenchRunning() ? evalNonFileBufferDocuments() : Collections.emptyMap(); int filesPerJob = Math.max(1, files.length / jobCount); - IFile[] filesByLocation= new IFile[files.length]; - System.arraycopy(files, 0, filesByLocation, 0, files.length); // Sorting files to search by location allows to more easily reuse // search results from one file to the other when they have same location - Arrays.sort(filesByLocation, (o1, o2) -> { - if (o1 == o2) { - return 0; - } - if (o1.getLocation() == o2.getLocation()) { - return 0; - } - if (o1.getLocation() == null) { - return +1; - } - if (o2.getLocation() == null) { - return -1; - } - return o1.getLocation().toString().compareTo(o2.getLocation().toString()); - }); - for (int first= 0; first < filesByLocation.length; first += filesPerJob) { + IFile[] filesByLocation = Arrays.stream(files).map(FileWithCachedLocation::new) + .sorted(Comparator.nullsFirst(Comparator.comparing(fn -> fn.location))).map(fn -> fn.file) + .collect(Collectors.toList()).toArray(IFile[]::new); + for (int first = 0; first < filesByLocation.length; first += filesPerJob) { int end= Math.min(filesByLocation.length, first + filesPerJob); Job job= new TextSearchJob(filesByLocation, first, end, documentsInEditors); job.setJobGroup(jobGroup); |