1
1
package com .fasterxml .jackson .databind .deser ;
2
2
3
3
import java .util .HashMap ;
4
- import java .util .concurrent .ConcurrentHashMap ;
5
4
6
5
import com .fasterxml .jackson .annotation .JsonFormat ;
7
6
import com .fasterxml .jackson .databind .*;
10
9
import com .fasterxml .jackson .databind .type .*;
11
10
import com .fasterxml .jackson .databind .util .ClassUtil ;
12
11
import com .fasterxml .jackson .databind .util .Converter ;
12
+ import com .fasterxml .jackson .databind .util .LRUMap ;
13
13
14
14
/**
15
15
* Class that defines caching layer between callers (like
19
19
* ({@link com.fasterxml.jackson.databind.deser.DeserializerFactory}).
20
20
*/
21
21
public final class DeserializerCache
22
- implements java .io .Serializable // since 2.1 -- needs to be careful tho
22
+ implements java .io .Serializable // since 2.1
23
23
{
24
24
private static final long serialVersionUID = 1L ;
25
25
@@ -32,15 +32,9 @@ public final class DeserializerCache
32
32
/**
33
33
* We will also cache some dynamically constructed deserializers;
34
34
* specifically, ones that are expensive to construct.
35
- * This currently means bean and Enum deserializers; starting with
36
- * 2.5, container deserializers will also be cached.
37
- *<p>
38
- * Given that we don't expect much concurrency for additions
39
- * (should very quickly converge to zero after startup), let's
40
- * define a relatively low concurrency setting.
35
+ * This currently means bean, Enum and container deserializers.
41
36
*/
42
- final protected ConcurrentHashMap <JavaType , JsonDeserializer <Object >> _cachedDeserializers
43
- = new ConcurrentHashMap <JavaType , JsonDeserializer <Object >>(64 , 0.75f , 4 );
37
+ final protected LRUMap <JavaType , JsonDeserializer <Object >> _cachedDeserializers ;
44
38
45
39
/**
46
40
* During deserializer construction process we may need to keep track of partially
@@ -56,7 +50,14 @@ public final class DeserializerCache
56
50
/**********************************************************
57
51
*/
58
52
59
- public DeserializerCache () { }
53
+ public DeserializerCache () {
54
+ this (2000 ); // see [databind#1995]
55
+ }
56
+
57
+ public DeserializerCache (int maxSize ) {
58
+ int initial = Math .min (64 , maxSize >>2 );
59
+ _cachedDeserializers = new LRUMap <>(initial , maxSize );
60
+ }
60
61
61
62
/*
62
63
/**********************************************************
@@ -67,7 +68,6 @@ public DeserializerCache() { }
67
68
Object writeReplace () {
68
69
// instead of making this transient, just clear it:
69
70
_incompleteDeserializers .clear ();
70
- // TODO: clear out "cheap" cached deserializers?
71
71
return this ;
72
72
}
73
73
0 commit comments