reuse common user data maps in VFS to save memory

This commit is contained in:
peter
2014-06-16 20:32:13 +02:00
parent efc969a289
commit f291e8bfef
7 changed files with 94 additions and 30 deletions
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.vfs.newvfs.impl;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.containers.ConcurrentWeakHashMap;
import com.intellij.util.keyFMap.KeyFMap;
import com.intellij.util.keyFMap.OneElementFMap;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.Charset;
/**
* @author peter
*/
class UserDataInterner {
private static final ConcurrentWeakHashMap<OneElementFMap, OneElementFMap> ourCache = new ConcurrentWeakHashMap<OneElementFMap, OneElementFMap>();
static KeyFMap internUserData(@NotNull KeyFMap map) {
if (map instanceof OneElementFMap && shouldIntern((OneElementFMap)map)) {
return ConcurrencyUtil.cacheOrGet(ourCache, (OneElementFMap)map, (OneElementFMap)map);
}
return map;
}
private static boolean shouldIntern(OneElementFMap map) {
Object value = map.getValue();
return value instanceof Enum || value instanceof Boolean || value instanceof Charset;
}
}
@@ -536,7 +536,7 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry {
@Override
protected boolean changeUserMap(KeyFMap oldMap, KeyFMap newMap) {
return myData.changeUserMap(oldMap, newMap);
return myData.changeUserMap(oldMap, UserDataInterner.internUserData(newMap));
}
}
@@ -142,7 +142,7 @@ public class VirtualFileImpl extends VirtualFileSystemEntry {
@Override
protected boolean changeUserMap(KeyFMap oldMap, KeyFMap newMap) {
return mySegment.changeUserMap(Math.abs(getId()), oldMap, newMap);
return mySegment.changeUserMap(Math.abs(getId()), oldMap, UserDataInterner.internUserData(newMap));
}
}
@@ -78,7 +78,7 @@ class ArrayBackedFMap implements KeyFMap {
if (oldSize == 3) {
int i1 = (2-i)/2;
int i2 = 3 - (i+2)/2;
return new PairElementsFMap(keys[i1], values[i1], keys[i2], values[i2]);
return new PairElementsFMap(Key.getKeyByIndex(keys[i1]), values[i1], Key.getKeyByIndex(keys[i2]), values[i2]);
}
int newSize = oldSize - 1;
int[] newKeys = new int[newSize];
@@ -25,7 +25,7 @@ class EmptyFMap implements KeyFMap {
@NotNull
@Override
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
return new OneElementFMap<V>(key.hashCode(), value);
return new OneElementFMap<V>(key, value);
}
@NotNull
@@ -18,45 +18,69 @@ package com.intellij.util.keyFMap;
import com.intellij.openapi.util.Key;
import org.jetbrains.annotations.NotNull;
class OneElementFMap<V> implements KeyFMap {
private final int myKeyCode;
public class OneElementFMap<V> implements KeyFMap {
private final Key myKey;
private final V myValue;
OneElementFMap(int keyCode, @NotNull V value) {
myKeyCode = keyCode;
public OneElementFMap(Key key, @NotNull V value) {
myKey = key;
myValue = value;
}
@NotNull
@Override
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
int keyCode = key.hashCode();
if (myKeyCode == keyCode) return new OneElementFMap<V>(keyCode, value);
return new PairElementsFMap(myKeyCode, myValue, keyCode, value);
if (myKey == key) return new OneElementFMap<V>(key, value);
return new PairElementsFMap(myKey, myValue, key, value);
}
@NotNull
@Override
public KeyFMap minus(@NotNull Key<?> key) {
if (key.hashCode() == myKeyCode) {
return KeyFMap.EMPTY_MAP;
}
return this;
return key == myKey ? KeyFMap.EMPTY_MAP : this;
}
@Override
public <V> V get(@NotNull Key<V> key) {
//noinspection unchecked
return myKeyCode == key.hashCode() ? (V)myValue : null;
return myKey == key ? (V)myValue : null;
}
@Override
public String toString() {
return "<"+Key.getKeyByIndex(myKeyCode) + " -> " + myValue+">";
return "<" + myKey + " -> " + myValue+">";
}
@Override
public boolean isEmpty() {
return false;
}
public Key getKey() {
return myKey;
}
public V getValue() {
return myValue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OneElementFMap)) return false;
OneElementFMap map = (OneElementFMap)o;
if (!myKey.equals(map.myKey)) return false;
if (!myValue.equals(map.myValue)) return false;
return true;
}
@Override
public int hashCode() {
int result = myKey.hashCode();
result = 31 * result + myValue.hashCode();
return result;
}
}
@@ -19,12 +19,12 @@ import com.intellij.openapi.util.Key;
import org.jetbrains.annotations.NotNull;
class PairElementsFMap implements KeyFMap {
private final int key1;
private final int key2;
private final Key key1;
private final Key key2;
private final Object value1;
private final Object value2;
PairElementsFMap(int key1, @NotNull Object value1, int key2, @NotNull Object value2) {
PairElementsFMap(Key key1, @NotNull Object value1, Key key2, @NotNull Object value2) {
this.key1 = key1;
this.value1 = value1;
this.key2 = key2;
@@ -35,31 +35,28 @@ class PairElementsFMap implements KeyFMap {
@NotNull
@Override
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
int keyCode = key.hashCode();
if (keyCode == key1) return new PairElementsFMap(keyCode, value, key2, value2);
if (keyCode == key2) return new PairElementsFMap(keyCode, value, key1, value1);
return new ArrayBackedFMap(new int[]{key1, key2, keyCode}, new Object[]{value1, value2, value});
if (key == key1) return new PairElementsFMap(key, value, key2, value2);
if (key == key2) return new PairElementsFMap(key, value, key1, value1);
return new ArrayBackedFMap(new int[]{key1.hashCode(), key2.hashCode(), key.hashCode()}, new Object[]{value1, value2, value});
}
@NotNull
@Override
public KeyFMap minus(@NotNull Key<?> key) {
int keyCode = key.hashCode();
if (keyCode == key1) return new OneElementFMap<Object>(key2, value2);
if (keyCode == key2) return new OneElementFMap<Object>(key1, value1);
if (key == key1) return new OneElementFMap<Object>(key2, value2);
if (key == key2) return new OneElementFMap<Object>(key1, value1);
return this;
}
@Override
public <V> V get(@NotNull Key<V> key) {
int keyCode = key.hashCode();
//noinspection unchecked
return keyCode == key1 ? (V)value1 : keyCode == key2 ? (V)value2 : null;
return key == key1 ? (V)value1 : key == key2 ? (V)value2 : null;
}
@Override
public String toString() {
return "Pair: ("+ Key.getKeyByIndex(key1) + " -> " + value1+"; "+Key.getKeyByIndex(key2) + " -> " + value2 + ")";
return "Pair: (" + key1 + " -> " + value1 + "; " + key2 + " -> " + value2 + ")";
}
@Override