From 00a2ca77ef2bd12e1330bc6eee73c00778c26074 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 30 Apr 2015 13:52:31 +0300 Subject: [PATCH] memory optimisation: do not store hashing strategy if it's the canonical one --- .../src/com/intellij/util/SingletonSet.java | 41 +++++++++++++++---- .../util/containers/ContainerUtil.java | 2 +- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/platform/util/src/com/intellij/util/SingletonSet.java b/platform/util/src/com/intellij/util/SingletonSet.java index 19aa96be2b21..d289424a4e16 100644 --- a/platform/util/src/com/intellij/util/SingletonSet.java +++ b/platform/util/src/com/intellij/util/SingletonSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ */ package com.intellij.util; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.SingletonIterator; import gnu.trove.TObjectHashingStrategy; import org.jetbrains.annotations.NotNull; @@ -30,15 +29,9 @@ import java.util.Set; */ public class SingletonSet implements Set { private final E theElement; - @NotNull private final TObjectHashingStrategy strategy; public SingletonSet(E e) { - this(e, ContainerUtil.canonicalStrategy()); - } - - public SingletonSet(E e, @NotNull final TObjectHashingStrategy strategy) { theElement = e; - this.strategy = strategy; } @Override @@ -48,7 +41,8 @@ public class SingletonSet implements Set { @Override public boolean contains(Object elem) { - return strategy.equals(theElement, (E)elem); + //noinspection unchecked + return getStrategy().equals(theElement, (E)elem); } @NotNull @@ -67,8 +61,10 @@ public class SingletonSet implements Set { @Override public T[] toArray(@NotNull T[] a) { if (a.length == 0) { + //noinspection unchecked a = (T[]) Array.newInstance(a.getClass().getComponentType(), 1); } + //noinspection unchecked a[0] = (T)theElement; if (a.length > 1) { a[1] = null; @@ -120,4 +116,31 @@ public class SingletonSet implements Set { public boolean isEmpty() { return false; } + + @NotNull + protected TObjectHashingStrategy getStrategy() { + //noinspection unchecked + return TObjectHashingStrategy.CANONICAL; + } + + @NotNull + public static Set withCustomStrategy(T o, @NotNull TObjectHashingStrategy strategy) { + return new CustomStrategySingletonSet(o, strategy); + } + + private static class CustomStrategySingletonSet extends SingletonSet { + @NotNull private final TObjectHashingStrategy strategy; + + private CustomStrategySingletonSet(E e, @NotNull final TObjectHashingStrategy strategy) { + super(e); + this.strategy = strategy; + } + + + @Override + @NotNull + protected TObjectHashingStrategy getStrategy() { + return strategy; + } + } } diff --git a/platform/util/src/com/intellij/util/containers/ContainerUtil.java b/platform/util/src/com/intellij/util/containers/ContainerUtil.java index abcbd941d6d7..634e81ad8990 100644 --- a/platform/util/src/com/intellij/util/containers/ContainerUtil.java +++ b/platform/util/src/com/intellij/util/containers/ContainerUtil.java @@ -2101,7 +2101,7 @@ public class ContainerUtil extends ContainerUtilRt { @NotNull @Contract(pure=true) public static Set singleton(final T o, @NotNull final TObjectHashingStrategy strategy) { - return new SingletonSet(o, strategy); + return strategy == TObjectHashingStrategy.CANONICAL ? new SingletonSet(o) : SingletonSet.withCustomStrategy(o, strategy); } /**