Skip to content

Commit

Permalink
Don't use eclipse collection as map
Browse files Browse the repository at this point in the history
All benchmarks show that using a normal hash map is 2-3 times faster.
  • Loading branch information
pontusmelke committed May 22, 2018
1 parent 84eeeb6 commit c5e9a59
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
Expand Up @@ -19,13 +19,12 @@
*/
package org.neo4j.values.virtual;

import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.tuple.Pair;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.stream.StreamSupport;
Expand All @@ -41,26 +40,59 @@

public abstract class MapValue extends VirtualValue
{
public static MapValue EMPTY = new MapValue()
{
@Override
public Iterable<String> keySet()
{
return Collections.emptyList();
}

@Override
public <E extends Exception> void foreach( ThrowingBiConsumer<String,AnyValue,E> f )
{
//do nothing
}

@Override
public boolean containsKey( String key )
{
return false;
}

@Override
public AnyValue get( String key )
{
return NO_VALUE;
}

@Override
public int size()
{
return 0;
}
};

static final class MapWrappingMapValue extends MapValue
{
private final ImmutableMap<String,AnyValue> map;
private final Map<String,AnyValue> map;

MapWrappingMapValue( ImmutableMap<String,AnyValue> map )
MapWrappingMapValue( Map<String,AnyValue> map )
{
this.map = map;
}

public Iterable<String> keySet()
{
return map.keysView();
return map.keySet();
}

@Override
public <E extends Exception> void foreach( ThrowingBiConsumer<String,AnyValue,E> f ) throws E
{
for ( Pair<String,AnyValue> valuePair : map.keyValuesView() )
for ( Map.Entry<String,AnyValue> entry : map.entrySet() )
{
f.accept( valuePair.getOne(), valuePair.getTwo() );
f.accept( entry.getKey(), entry.getValue() );
}
}

Expand All @@ -71,7 +103,7 @@ public boolean containsKey( String key )

public AnyValue get( String key )
{
return map.getIfAbsentValue( key, NO_VALUE );
return map.getOrDefault( key, NO_VALUE );
}

public int size()
Expand Down
Expand Up @@ -19,23 +19,23 @@
*/
package org.neo4j.values.virtual;

import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.factory.Maps;
import java.util.HashMap;
import java.util.Map;

import org.neo4j.values.AnyValue;

public class MapValueBuilder
{
private final MutableMap<String, AnyValue> map;
private final Map<String, AnyValue> map;

public MapValueBuilder()
{
this.map = Maps.mutable.empty();
this.map = new HashMap<>( );
}

public MapValueBuilder( int size )
{
this.map = Maps.mutable.withInitialCapacity( size );
this.map = new HashMap<>( size );
}

public AnyValue add( String key, AnyValue value )
Expand All @@ -50,7 +50,7 @@ public void clear()

public MapValue build()
{
return new MapValue.MapWrappingMapValue( map.toImmutable() );
return new MapValue.MapWrappingMapValue( map );
}

}
Expand Up @@ -19,10 +19,7 @@
*/
package org.neo4j.values.virtual;

import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.factory.Maps;

import java.util.HashMap;
import java.util.List;

import org.neo4j.values.AnyValue;
Expand All @@ -37,7 +34,7 @@
@SuppressWarnings( "WeakerAccess" )
public final class VirtualValues
{
public static final MapValue EMPTY_MAP = new MapValue.MapWrappingMapValue( Maps.immutable.empty() );
public static final MapValue EMPTY_MAP = MapValue.EMPTY;
public static final ListValue EMPTY_LIST = new ListValue.ArrayListValue( new AnyValue[0] );

private VirtualValues()
Expand Down Expand Up @@ -143,33 +140,10 @@ public static MapValue emptyMap()
public static MapValue map( String[] keys, AnyValue[] values )
{
assert keys.length == values.length;

ImmutableMap<String,AnyValue> map;
switch ( keys.length )
HashMap<String,AnyValue> map = new HashMap<>( keys.length );
for ( int i = 0; i < keys.length; i++ )
{
case 0:
map = Maps.immutable.empty();
break;
case 1:
map = Maps.immutable.with(keys[0], values[0]);
break;
case 2:
map = Maps.immutable.with(keys[0], values[0], keys[1], values[1]);
break;
case 3:
map = Maps.immutable.with(keys[0], values[0], keys[1], values[1], keys[2], values[2]);
break;
case 4:
map = Maps.immutable.with(keys[0], values[0], keys[1], values[1], keys[2], values[2], keys[3], values[3]);
break;
default:
MutableMap<String,AnyValue> mutable = Maps.mutable.withInitialCapacity( keys.length );
for ( int i = 0; i < keys.length; i++ )
{
mutable.put(keys[i], values[i]);
}
map = mutable.toImmutable();

map.put( keys[i], values[i] );
}
return new MapValue.MapWrappingMapValue( map );
}
Expand Down

0 comments on commit c5e9a59

Please sign in to comment.