Skip to content

Commit 7f4b9bc

Browse files
RoiSoleillaeubi
authored andcommitted
Avoid useless computation of getMatchingRegexpAssociated that uses
Pattern (time consumming) Goal : Improve performance See : https://pasteboard.co/7EJ61d3sSu5Y.png and https://pasteboard.co/KvGgdDU7Macb.png Why : In ContentTypeMatcher for findContentTypeFor(String fileName) we take the first result of currentCatalog.findContentTypesFor(this, fileName); so if there is a result on selectMatchingByName(context, allByFileName, Collections.emptySet(), fileName, IContentType.FILE_NAME_SPEC); there is no need to compute selectMatchingByName(context, allByFileExtension, selectedByName, fileExtension, IContentType.FILE_EXTENSION_SPEC); because it won't be used if there is no policy to reorder the results.
1 parent 2df11ca commit 7f4b9bc

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/ContentTypeCatalog.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*******************************************************************************/
1515
package org.eclipse.core.internal.content;
1616

17+
import static java.util.Optional.empty;
18+
import static java.util.Optional.of;
19+
1720
import java.io.IOException;
1821
import java.io.InputStream;
1922
import java.io.Reader;
@@ -28,6 +31,7 @@
2831
import java.util.List;
2932
import java.util.Map;
3033
import java.util.Map.Entry;
34+
import java.util.Optional;
3135
import java.util.Set;
3236
import java.util.regex.Pattern;
3337
import org.eclipse.core.runtime.CoreException;
@@ -409,7 +413,7 @@ IContentType[] findContentTypesFor(ContentTypeMatcher matcher, InputStream conte
409413
}
410414

411415
IContentType[] findContentTypesFor(ContentTypeMatcher matcher, final String fileName) {
412-
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter));
416+
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter ,false));
413417
// give the policy a chance to change the results
414418
ISelectionPolicy policy = matcher.getPolicy();
415419
if (policy != null) {
@@ -418,6 +422,15 @@ IContentType[] findContentTypesFor(ContentTypeMatcher matcher, final String file
418422
return selected;
419423
}
420424

425+
Optional<IContentType> findFirstContentTypeFor(ContentTypeMatcher matcher, final String fileName) {
426+
ISelectionPolicy policy = matcher.getPolicy();
427+
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter, policy == null));
428+
// give the policy a chance to change the results
429+
if (policy != null)
430+
selected = applyPolicy(policy, selected, true, false);
431+
return selected.length != 0 ? of(selected[0]) : empty();
432+
}
433+
421434
synchronized public IContentType[] getAllContentTypes() {
422435
List<ContentType> result = new ArrayList<>(contentTypes.size());
423436
for (IContentType iContentType : contentTypes.values()) {
@@ -555,7 +568,7 @@ private IContentType[] internalFindContentTypesFor(ContentTypeMatcher matcher, I
555568
indeterminatePolicy = policyConstantGeneralIsBetter;
556569
validPolicy = policyConstantSpecificIsBetter;
557570
} else {
558-
subset = internalFindContentTypesSorted(matcher, fileName, policyLexicographical);
571+
subset = internalFindContentTypesSorted(matcher, fileName, policyLexicographical, false);
559572
indeterminatePolicy = policyGeneralIsBetter;
560573
validPolicy = policySpecificIsBetter;
561574
}
@@ -590,7 +603,7 @@ private IContentType[] internalFindContentTypesFor(ContentTypeMatcher matcher, I
590603
* @return all matching content types in the preferred order
591604
* @see IContentTypeManager#findContentTypesFor(String)
592605
*/
593-
synchronized private IContentType[][] internalFindContentTypesSorted(ContentTypeMatcher matcher, final String fileName, Comparator<IContentType> sortingPolicy) {
606+
synchronized private IContentType[][] internalFindContentTypesSorted(ContentTypeMatcher matcher, final String fileName, Comparator<IContentType> sortingPolicy, boolean quickFinish) {
594607
IScopeContext context = matcher.getContext();
595608
IContentType[][] result = { NO_CONTENT_TYPES, NO_CONTENT_TYPES, NO_CONTENT_TYPES };
596609

@@ -609,6 +622,9 @@ synchronized private IContentType[][] internalFindContentTypesSorted(ContentType
609622
result[0] = selectedByName.toArray(new IContentType[selectedByName.size()]);
610623
if (result[0].length > 1) {
611624
Arrays.sort(result[0], sortingPolicy);
625+
if (quickFinish) {
626+
return result;
627+
}
612628
}
613629

614630
final String fileExtension = ContentTypeManager.getFileExtension(fileName);
@@ -628,6 +644,9 @@ synchronized private IContentType[][] internalFindContentTypesSorted(ContentType
628644
}
629645
if (result[1].length > 1) {
630646
Arrays.sort(result[1], sortingPolicy);
647+
if (quickFinish) {
648+
return result;
649+
}
631650
}
632651

633652
final Set<ContentType> allByFilePattern;

runtime/bundles/org.eclipse.core.contenttype/src/org/eclipse/core/internal/content/ContentTypeMatcher.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
*******************************************************************************/
1515
package org.eclipse.core.internal.content;
1616

17-
import java.io.*;
18-
import java.util.*;
17+
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.Reader;
20+
import java.util.Collection;
21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
1924
import java.util.regex.Pattern;
2025
import org.eclipse.core.runtime.QualifiedName;
21-
import org.eclipse.core.runtime.content.*;
26+
import org.eclipse.core.runtime.content.IContentDescription;
27+
import org.eclipse.core.runtime.content.IContentType;
28+
import org.eclipse.core.runtime.content.IContentTypeManager;
29+
import org.eclipse.core.runtime.content.IContentTypeMatcher;
2230
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
2331
import org.eclipse.core.runtime.preferences.IScopeContext;
2432
import org.osgi.service.prefs.BackingStoreException;
@@ -47,8 +55,9 @@ public IContentType findContentTypeFor(InputStream contents, String fileName) th
4755
public IContentType findContentTypeFor(String fileName) {
4856
// basic implementation just gets all content types
4957
ContentTypeCatalog currentCatalog = getCatalog();
50-
IContentType[] associated = currentCatalog.findContentTypesFor(this, fileName);
51-
return associated.length == 0 ? null : new ContentTypeHandler((ContentType) associated[0], currentCatalog.getGeneration());
58+
return currentCatalog.findFirstContentTypeFor(this, fileName)
59+
.map(contentType -> new ContentTypeHandler((ContentType) contentType, currentCatalog.getGeneration()))
60+
.orElse(null);
5261
}
5362

5463
@Override

0 commit comments

Comments
 (0)