Skip to content

Commit

Permalink
Cache classname lookup
Browse files Browse the repository at this point in the history
In commit 8fd3f89 serializers that depend on jdk 8 are only registered
if the related class is available. The CI builds showed that this
classname lookup takes significant time (detected by some naive micro
benchmarks in the project): https://jenkins.inoio.de/job/kryo/buildTimeTrend

Caching classname lookup shall speed up Kryo instantiation and CI builds
as well.

Fixes EsotericSoftware#416
  • Loading branch information
magro committed Mar 29, 2016
1 parent c10fd76 commit 45e8cd8
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/com/esotericsoftware/kryo/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@

package com.esotericsoftware.kryo.util;

import java.util.concurrent.ConcurrentHashMap;

import static com.esotericsoftware.minlog.Log.*;

/** A few utility methods, mostly for private use.
* @author Nathan Sweet <[email protected]> */
public class Util {
static public boolean isAndroid = "Dalvik".equals(System.getProperty("java.vm.name"));

private static final ConcurrentHashMap<String, Boolean> classAvailabilities = new ConcurrentHashMap<String, Boolean>();

public static boolean isClassAvailable(String className) {
try {
Class.forName(className);
return true;
} catch (Exception e) {
debug("kryo", "Class not available: " + className);
return false;
Boolean result = classAvailabilities.get(className);
if(result == null) {
try {
Class.forName(className);
result = true;
} catch (Exception e) {
debug("kryo", "Class not available: " + className);
result = false;
}
classAvailabilities.put(className, result);
}
return result;
}

/** Returns the primitive wrapper class for a primitive class.
Expand Down

0 comments on commit 45e8cd8

Please sign in to comment.