Skip to content

Commit da618a3

Browse files
committed
Avoid useless computation of getMatchingRegexpAssociated that uses
Pattern (time consumming)
1 parent cf54e17 commit da618a3

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ IContentType[] findContentTypesFor(ContentTypeMatcher matcher, InputStream conte
409409
}
410410

411411
IContentType[] findContentTypesFor(ContentTypeMatcher matcher, final String fileName) {
412-
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter));
412+
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter ,false));
413413
// give the policy a chance to change the results
414414
ISelectionPolicy policy = matcher.getPolicy();
415415
if (policy != null) {
@@ -418,6 +418,15 @@ IContentType[] findContentTypesFor(ContentTypeMatcher matcher, final String file
418418
return selected;
419419
}
420420

421+
IContentType findFirstContentTypeFor(ContentTypeMatcher matcher, final String fileName) {
422+
ISelectionPolicy policy = matcher.getPolicy();
423+
IContentType[] selected = concat(internalFindContentTypesSorted(matcher, fileName, policyConstantGeneralIsBetter, policy == null));
424+
// give the policy a chance to change the results
425+
if (policy != null)
426+
selected = applyPolicy(policy, selected, true, false);
427+
return selected.length != 0 ? selected[0] : null;
428+
}
429+
421430
synchronized public IContentType[] getAllContentTypes() {
422431
List<ContentType> result = new ArrayList<>(contentTypes.size());
423432
for (IContentType iContentType : contentTypes.values()) {
@@ -555,7 +564,7 @@ private IContentType[] internalFindContentTypesFor(ContentTypeMatcher matcher, I
555564
indeterminatePolicy = policyConstantGeneralIsBetter;
556565
validPolicy = policyConstantSpecificIsBetter;
557566
} else {
558-
subset = internalFindContentTypesSorted(matcher, fileName, policyLexicographical);
567+
subset = internalFindContentTypesSorted(matcher, fileName, policyLexicographical, false);
559568
indeterminatePolicy = policyGeneralIsBetter;
560569
validPolicy = policySpecificIsBetter;
561570
}
@@ -590,7 +599,7 @@ private IContentType[] internalFindContentTypesFor(ContentTypeMatcher matcher, I
590599
* @return all matching content types in the preferred order
591600
* @see IContentTypeManager#findContentTypesFor(String)
592601
*/
593-
synchronized private IContentType[][] internalFindContentTypesSorted(ContentTypeMatcher matcher, final String fileName, Comparator<IContentType> sortingPolicy) {
602+
synchronized private IContentType[][] internalFindContentTypesSorted(ContentTypeMatcher matcher, final String fileName, Comparator<IContentType> sortingPolicy, boolean quickFinish) {
594603
IScopeContext context = matcher.getContext();
595604
IContentType[][] result = { NO_CONTENT_TYPES, NO_CONTENT_TYPES, NO_CONTENT_TYPES };
596605

@@ -609,6 +618,9 @@ synchronized private IContentType[][] internalFindContentTypesSorted(ContentType
609618
result[0] = selectedByName.toArray(new IContentType[selectedByName.size()]);
610619
if (result[0].length > 1) {
611620
Arrays.sort(result[0], sortingPolicy);
621+
if(quickFinish) {
622+
return result;
623+
}
612624
}
613625

614626
final String fileExtension = ContentTypeManager.getFileExtension(fileName);
@@ -628,6 +640,9 @@ synchronized private IContentType[][] internalFindContentTypesSorted(ContentType
628640
}
629641
if (result[1].length > 1) {
630642
Arrays.sort(result[1], sortingPolicy);
643+
if (quickFinish) {
644+
return result;
645+
}
631646
}
632647

633648
final Set<ContentType> allByFilePattern;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public IContentType findContentTypeFor(InputStream contents, String fileName) th
4747
public IContentType findContentTypeFor(String fileName) {
4848
// basic implementation just gets all content types
4949
ContentTypeCatalog currentCatalog = getCatalog();
50-
IContentType[] associated = currentCatalog.findContentTypesFor(this, fileName);
51-
return associated.length == 0 ? null : new ContentTypeHandler((ContentType) associated[0], currentCatalog.getGeneration());
50+
IContentType associated = currentCatalog.findFirstContentTypeFor(this, fileName);
51+
return associated == null ? null
52+
: new ContentTypeHandler((ContentType) associated, currentCatalog.getGeneration());
5253
}
5354

5455
@Override

0 commit comments

Comments
 (0)