Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
sped up workspace project classpath entry hashing
Browse files Browse the repository at this point in the history
introduced in-memory cache of sha1 hashes of workspace project
classpath entries. this reduced time of the first source lookup
after workspace restart from ~42 second to under 2 seconds for
one of my fairly large workspaces.

Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
  • Loading branch information
ifedorenko committed May 4, 2016
1 parent e1f9fdb commit 690731b
Showing 1 changed file with 65 additions and 4 deletions.
Expand Up @@ -4,23 +4,84 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableMap;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.google.common.io.Files;

class Locations
{
public static final Object hash( File location )
private static class CacheKey
{
try
public final File file;

private final long length;

private final long lastModified;

public CacheKey( File file )
throws IOException
{
this.file = file.getCanonicalFile();
this.length = file.length();
this.lastModified = file.lastModified();
}

@Override
public int hashCode()
{
int hash = 17;
hash = hash * 31 + file.hashCode();
hash = hash * 31 + (int) length;
hash = hash * 31 + (int) lastModified;
return hash;
}

@Override
public boolean equals( Object obj )
{
return Files.hash( location, Hashing.sha1() );
if ( obj == this )
{
return true;
}
if ( !( obj instanceof CacheKey ) )
{
return false;
}
CacheKey other = (CacheKey) obj;
return file.equals( other.file ) && length == other.length && lastModified == other.lastModified;
}
catch ( IOException e )
}

private static final Cache<CacheKey, HashCode> CACHE = CacheBuilder.newBuilder().build();

public static final Object hash( final File location )
{
if ( location == null || !location.isFile() )
{
return null;
}
try
{
return CACHE.get( new CacheKey( location ), new Callable<HashCode>()
{
@Override
public HashCode call()
throws Exception
{
return Files.hash( location, Hashing.sha1() );
}
} );
}
catch ( ExecutionException | IOException e )
{
return null; // file does not exist or can't be read
}
}

public static final <T> Map<Object, T> hash( Map<File, T> map )
Expand Down

0 comments on commit 690731b

Please sign in to comment.