mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
reuse common user data maps in VFS to save memory
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -536,7 +536,7 @@ public class VirtualDirectoryImpl extends VirtualFileSystemEntry {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean changeUserMap(KeyFMap oldMap, KeyFMap newMap) {
|
protected boolean changeUserMap(KeyFMap oldMap, KeyFMap newMap) {
|
||||||
return myData.changeUserMap(oldMap, newMap);
|
return myData.changeUserMap(oldMap, UserDataInterner.internUserData(newMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -142,7 +142,7 @@ public class VirtualFileImpl extends VirtualFileSystemEntry {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean changeUserMap(KeyFMap oldMap, KeyFMap newMap) {
|
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) {
|
if (oldSize == 3) {
|
||||||
int i1 = (2-i)/2;
|
int i1 = (2-i)/2;
|
||||||
int i2 = 3 - (i+2)/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 newSize = oldSize - 1;
|
||||||
int[] newKeys = new int[newSize];
|
int[] newKeys = new int[newSize];
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class EmptyFMap implements KeyFMap {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
|
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
|
@NotNull
|
||||||
|
|||||||
@@ -18,45 +18,69 @@ package com.intellij.util.keyFMap;
|
|||||||
import com.intellij.openapi.util.Key;
|
import com.intellij.openapi.util.Key;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
class OneElementFMap<V> implements KeyFMap {
|
public class OneElementFMap<V> implements KeyFMap {
|
||||||
private final int myKeyCode;
|
private final Key myKey;
|
||||||
private final V myValue;
|
private final V myValue;
|
||||||
|
|
||||||
OneElementFMap(int keyCode, @NotNull V value) {
|
public OneElementFMap(Key key, @NotNull V value) {
|
||||||
myKeyCode = keyCode;
|
myKey = key;
|
||||||
myValue = value;
|
myValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
|
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
|
||||||
int keyCode = key.hashCode();
|
if (myKey == key) return new OneElementFMap<V>(key, value);
|
||||||
if (myKeyCode == keyCode) return new OneElementFMap<V>(keyCode, value);
|
return new PairElementsFMap(myKey, myValue, key, value);
|
||||||
return new PairElementsFMap(myKeyCode, myValue, keyCode, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public KeyFMap minus(@NotNull Key<?> key) {
|
public KeyFMap minus(@NotNull Key<?> key) {
|
||||||
if (key.hashCode() == myKeyCode) {
|
return key == myKey ? KeyFMap.EMPTY_MAP : this;
|
||||||
return KeyFMap.EMPTY_MAP;
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> V get(@NotNull Key<V> key) {
|
public <V> V get(@NotNull Key<V> key) {
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return myKeyCode == key.hashCode() ? (V)myValue : null;
|
return myKey == key ? (V)myValue : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "<"+Key.getKeyByIndex(myKeyCode) + " -> " + myValue+">";
|
return "<" + myKey + " -> " + myValue+">";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
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;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
class PairElementsFMap implements KeyFMap {
|
class PairElementsFMap implements KeyFMap {
|
||||||
private final int key1;
|
private final Key key1;
|
||||||
private final int key2;
|
private final Key key2;
|
||||||
private final Object value1;
|
private final Object value1;
|
||||||
private final Object value2;
|
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.key1 = key1;
|
||||||
this.value1 = value1;
|
this.value1 = value1;
|
||||||
this.key2 = key2;
|
this.key2 = key2;
|
||||||
@@ -35,31 +35,28 @@ class PairElementsFMap implements KeyFMap {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
|
public <V> KeyFMap plus(@NotNull Key<V> key, @NotNull V value) {
|
||||||
int keyCode = key.hashCode();
|
if (key == key1) return new PairElementsFMap(key, value, key2, value2);
|
||||||
if (keyCode == key1) return new PairElementsFMap(keyCode, value, key2, value2);
|
if (key == key2) return new PairElementsFMap(key, value, key1, value1);
|
||||||
if (keyCode == key2) return new PairElementsFMap(keyCode, value, key1, value1);
|
return new ArrayBackedFMap(new int[]{key1.hashCode(), key2.hashCode(), key.hashCode()}, new Object[]{value1, value2, value});
|
||||||
return new ArrayBackedFMap(new int[]{key1, key2, keyCode}, new Object[]{value1, value2, value});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public KeyFMap minus(@NotNull Key<?> key) {
|
public KeyFMap minus(@NotNull Key<?> key) {
|
||||||
int keyCode = key.hashCode();
|
if (key == key1) return new OneElementFMap<Object>(key2, value2);
|
||||||
if (keyCode == key1) return new OneElementFMap<Object>(key2, value2);
|
if (key == key2) return new OneElementFMap<Object>(key1, value1);
|
||||||
if (keyCode == key2) return new OneElementFMap<Object>(key1, value1);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> V get(@NotNull Key<V> key) {
|
public <V> V get(@NotNull Key<V> key) {
|
||||||
int keyCode = key.hashCode();
|
|
||||||
//noinspection unchecked
|
//noinspection unchecked
|
||||||
return keyCode == key1 ? (V)value1 : keyCode == key2 ? (V)value2 : null;
|
return key == key1 ? (V)value1 : key == key2 ? (V)value2 : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Pair: ("+ Key.getKeyByIndex(key1) + " -> " + value1+"; "+Key.getKeyByIndex(key2) + " -> " + value2 + ")";
|
return "Pair: (" + key1 + " -> " + value1 + "; " + key2 + " -> " + value2 + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user