Skip to content

Commit ed1bcb9

Browse files
EcljpseB0Tjukzi
authored andcommitted
CharsetManager.internalGetCharsetFor reuse calculated key
to reduce memory allocations.
1 parent 8330656 commit ed1bcb9

File tree

1 file changed

+23
-18
lines changed
  • resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources

1 file changed

+23
-18
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/CharsetManager.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void flushPreferences(Preferences projectPrefs, boolean shouldDisableCharsetDelt
310310
*
311311
* @param resourcePath the path for the resource
312312
* @param recurse whether the parent should be queried
313-
* @return the charset setting for the given resource
313+
* @return the charset setting for the given resource or <code>null</code>.
314314
*/
315315
public String getCharsetFor(IPath resourcePath, boolean recurse) {
316316
Assert.isLegal(resourcePath.segmentCount() >= 1);
@@ -319,10 +319,11 @@ public String getCharsetFor(IPath resourcePath, boolean recurse) {
319319
Preferences prefs = getPreferences(project, false, false);
320320
Preferences derivedPrefs = getPreferences(project, false, true);
321321

322-
if (prefs == null && derivedPrefs == null)
322+
if (prefs == null && derivedPrefs == null) {
323323
// no preferences found - for performance reasons, short-circuit
324324
// lookup by falling back to workspace's default setting
325325
return recurse ? ResourcesPlugin.getEncoding() : null;
326+
}
326327

327328
return internalGetCharsetFor(prefs, derivedPrefs, resourcePath, recurse);
328329
}
@@ -366,32 +367,36 @@ Preferences getPreferences(IProject project, boolean create, boolean isDerived,
366367
}
367368

368369
private String internalGetCharsetFor(Preferences prefs, Preferences derivedPrefs, IPath resourcePath, boolean recurse) {
369-
String charset = null;
370-
371-
// try to find the encoding in regular and then derived preferences
372-
if (prefs != null)
373-
charset = prefs.get(getKeyFor(resourcePath), null);
374-
// derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
375-
// so the explicit check against #isDerivedEncodingStoredSeparately is not required
376-
if (charset == null && derivedPrefs != null)
377-
charset = derivedPrefs.get(getKeyFor(resourcePath), null);
378-
379-
if (!recurse)
370+
String charset = getCharsetFromPreferences(prefs, derivedPrefs, resourcePath);
371+
if (!recurse) {
380372
return charset;
373+
}
381374

382375
while (charset == null && resourcePath.segmentCount() > 1) {
383376
resourcePath = resourcePath.removeLastSegments(1);
384-
// try to find the encoding in regular and then derived preferences
385-
if (prefs != null)
386-
charset = prefs.get(getKeyFor(resourcePath), null);
387-
if (charset == null && derivedPrefs != null)
388-
charset = derivedPrefs.get(getKeyFor(resourcePath), null);
377+
charset = getCharsetFromPreferences(prefs, derivedPrefs, resourcePath);
389378
}
390379

391380
// ensure we default to the workspace encoding if none is found
392381
return charset == null ? ResourcesPlugin.getEncoding() : charset;
393382
}
394383

384+
private String getCharsetFromPreferences(Preferences prefs, Preferences derivedPrefs, IPath resourcePath) {
385+
String charset = null;
386+
String key = getKeyFor(resourcePath);
387+
388+
// try to find the encoding in regular and then derived preferences
389+
if (prefs != null) {
390+
charset = prefs.get(key, null);
391+
}
392+
// derivedPrefs may be not null, only if #isDerivedEncodingStoredSeparately returns true
393+
// so the explicit check against #isDerivedEncodingStoredSeparately is not required
394+
if (charset == null && derivedPrefs != null) {
395+
charset = derivedPrefs.get(key, null);
396+
}
397+
return charset;
398+
}
399+
395400
private boolean isDerivedEncodingStoredSeparately(IProject project) {
396401
// be careful looking up for our node so not to create any nodes as side effect
397402
Preferences node = Platform.getPreferencesService().getRootNode().node(ProjectScope.SCOPE);

0 commit comments

Comments
 (0)