From 1d80ace6dbb9fd47401d2b996f4803bbb9a945ff Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Wed, 9 Feb 2011 18:38:25 +0300 Subject: [PATCH] performance --- .../editor/markup/AttributesFlyweight.java | 57 ++++++++----------- .../openapi/editor/markup/TextAttributes.java | 56 +++++------------- 2 files changed, 38 insertions(+), 75 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java b/platform/platform-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java index 899c312a9c26..d12a26e1fa10 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/markup/AttributesFlyweight.java @@ -19,24 +19,16 @@ */ package com.intellij.openapi.editor.markup; -import gnu.trove.THashSet; +import com.intellij.util.ConcurrencyUtil; +import com.intellij.util.containers.StripedLockConcurrentHashMap; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.awt.*; public class AttributesFlyweight { - private static final class MyTHashSet extends THashSet { - public int index(final AttributesFlyweight obj) { - return super.index(obj); - } - - public AttributesFlyweight get(int index) { - return (AttributesFlyweight)_set[index]; - } - } - - private static final MyTHashSet entries = new MyTHashSet(); + private final int myHashCode; + private static final StripedLockConcurrentHashMap entries = new StripedLockConcurrentHashMap(); @NotNull public static AttributesFlyweight create(Color foreground, @@ -46,15 +38,7 @@ public class AttributesFlyweight { EffectType effectType, Color errorStripeColor) { AttributesFlyweight key = new AttributesFlyweight(foreground, background, fontType, effectColor, effectType, errorStripeColor); - synchronized (entries) { - int idx = entries.index(key); - if (idx >= 0) { - return entries.get(idx); - } - - entries.add(key); - return key; - } + return ConcurrencyUtil.cacheOrGet(entries, key, key); } private final Color myForeground; @@ -64,18 +48,29 @@ public class AttributesFlyweight { private final EffectType myEffectType; private final Color myErrorStripeColor; - AttributesFlyweight(Color foreground, - Color background, - int fontType, - Color effectColor, - EffectType effectType, - Color errorStripeColor) { + private AttributesFlyweight(Color foreground, + Color background, + int fontType, + Color effectColor, + EffectType effectType, + Color errorStripeColor) { myForeground = foreground; myBackground = background; myFontType = fontType; myEffectColor = effectColor; myEffectType = effectType; myErrorStripeColor = errorStripeColor; + myHashCode = calcHashCode(); + } + + private int calcHashCode() { + int result = myForeground != null ? myForeground.hashCode() : 0; + result = 31 * result + (myBackground != null ? myBackground.hashCode() : 0); + result = 31 * result + myFontType; + result = 31 * result + (myEffectColor != null ? myEffectColor.hashCode() : 0); + result = 31 * result + (myEffectType != null ? myEffectType.hashCode() : 0); + result = 31 * result + (myErrorStripeColor != null ? myErrorStripeColor.hashCode() : 0); + return result; } public Color getForeground() { @@ -145,13 +140,7 @@ public class AttributesFlyweight { @Override public int hashCode() { - int result = myForeground != null ? myForeground.hashCode() : 0; - result = 31 * result + (myBackground != null ? myBackground.hashCode() : 0); - result = 31 * result + myFontType; - result = 31 * result + (myEffectColor != null ? myEffectColor.hashCode() : 0); - result = 31 * result + (myEffectType != null ? myEffectType.hashCode() : 0); - result = 31 * result + (myErrorStripeColor != null ? myErrorStripeColor.hashCode() : 0); - return result; + return myHashCode; } @NonNls diff --git a/platform/platform-api/src/com/intellij/openapi/editor/markup/TextAttributes.java b/platform/platform-api/src/com/intellij/openapi/editor/markup/TextAttributes.java index 433498aeaf9a..acd46961f624 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/markup/TextAttributes.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/markup/TextAttributes.java @@ -111,17 +111,23 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { private void setEffectType(EffectType effectType) { if (effectType == EffectType.BOXED) { EFFECT_TYPE = EFFECT_BORDER; - } else if (effectType == EffectType.LINE_UNDERSCORE) { + } + else if (effectType == EffectType.LINE_UNDERSCORE) { EFFECT_TYPE = EFFECT_LINE; - } else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) { + } + else if (effectType == EffectType.BOLD_LINE_UNDERSCORE) { EFFECT_TYPE = EFFECT_BOLD_LINE; - } else if (effectType == EffectType.STRIKEOUT) { + } + else if (effectType == EffectType.STRIKEOUT) { EFFECT_TYPE = EFFECT_STRIKEOUT; - } else if (effectType == EffectType.WAVE_UNDERSCORE) { + } + else if (effectType == EffectType.WAVE_UNDERSCORE) { EFFECT_TYPE = EFFECT_WAVE; - } else if (effectType == EffectType.BOLD_DOTTED_LINE) { + } + else if (effectType == EffectType.BOLD_DOTTED_LINE) { EFFECT_TYPE = EFFECT_BOLD_DOTTED_LINE; - } else { + } + else { EFFECT_TYPE = -1; } } @@ -213,44 +219,12 @@ public class TextAttributes implements JDOMExternalizable, Cloneable { if(!(obj instanceof TextAttributes)) { return false; } - TextAttributes textAttributes = (TextAttributes)obj; - if(!Comparing.equal(textAttributes.getForegroundColor(), getForegroundColor())) { - return false; - } - if(!Comparing.equal(textAttributes.getBackgroundColor(), getBackgroundColor())) { - return false; - } - if(!Comparing.equal(textAttributes.getErrorStripeColor(), getErrorStripeColor())) { - return false; - } - if(!Comparing.equal(textAttributes.getEffectColor(), getEffectColor())) { - return false; - } - if (textAttributes.getEffectType() != getEffectType()) { - return false; - } - if(textAttributes.getFontType() != getFontType()) { - return false; - } - return true; + // myAttrs are interned, see com.intellij.openapi.editor.markup.AttributesFlyweight.create() + return myAttrs == ((TextAttributes)obj).myAttrs; } public int hashCode() { - int hashCode = 0; - if(getForegroundColor() != null) { - hashCode += getForegroundColor().hashCode(); - } - if(getBackgroundColor() != null) { - hashCode += getBackgroundColor().hashCode(); - } - if(getErrorStripeColor() != null) { - hashCode += getErrorStripeColor().hashCode(); - } - if(getEffectColor() != null) { - hashCode += getEffectColor().hashCode(); - } - hashCode += getFontType(); - return hashCode; + return myAttrs.hashCode(); } public void readExternal(Element element) throws InvalidDataException {