mjson
mjson copied to clipboard
escaper caching
Hi, Found I can get a huge increase in speed by caching in the escaper. In my case I am outputting periodic updates to the same data, so its especially useful. the escape method was using 5% CPU, now its below 1%. I know you dont want any dependencies but I used Google guava cache. Code is here if you want to use it:
escapeHash = CacheBuilder.newBuilder()
.maximumSize(1000)
.build(
new CacheLoader<CharSequence, String>() {
public String load(CharSequence plainText) throws Exception {
StringBuilder escapedString = new StringBuilder(plainText.length() + 20);
try {
escapeJsonString(plainText, escapedString);
} catch (IOException e) {
throw new RuntimeException(e);
}
return escapedString.toString();
}
});
}
public String escapeJsonString(CharSequence plainText) {
return escapeHash.getUnchecked(plainText);
}
That's interesting and I'd do something about it. You are right, I wouldn't want any dependencies on the project :)
BTW I treid a raw ConcurrentHashMap, but it kept growing and slowing, so that doesnt work without an evictor.