Browse code

[core] BM-13725 Fix: string cache for low cardinality strings

Thomas Cataldo authored on 08/01/2019 09:10:44
Showing 1 changed files
... ...
@@ -27,7 +27,9 @@ import java.util.Date;
27 27
 import java.util.HashSet;
28 28
 import java.util.LinkedList;
29 29
 import java.util.List;
30
+import java.util.Map;
30 31
 import java.util.Set;
32
+import java.util.concurrent.ConcurrentHashMap;
31 33
 
32 34
 import javax.sql.DataSource;
33 35
 
... ...
@@ -46,13 +48,31 @@ public class ContainerStore extends JdbcAbstractStore {
46 48
 
47 49
 	private ContainerCache cache;
48 50
 
51
+	/**
52
+	 * https://shipilev.net/jvm-anatomy-park/10-string-intern/
53
+	 */
54
+	public static class CHMInterner {
55
+		private final Map<String, String> map;
56
+
57
+		public CHMInterner() {
58
+			map = new ConcurrentHashMap<>();
59
+		}
60
+
61
+		public String intern(String s) {
62
+			String exist = map.putIfAbsent(s, s);
63
+			return (exist == null) ? s : exist;
64
+		}
65
+	}
66
+
67
+	private static final CHMInterner interner = new CHMInterner();
68
+
49 69
 	private static final EntityPopulator<Container> CONTAINER_POPULATOR = new EntityPopulator<Container>() {
50 70
 
51 71
 		@Override
52 72
 		public int populate(ResultSet rs, int index, Container value) throws SQLException {
53 73
 			value.id = rs.getLong(index++);
54 74
 			value.uid = rs.getString(index++);
55
-			value.type = rs.getString(index++);
75
+			value.type = interner.intern(rs.getString(index++));
56 76
 			value.name = rs.getString(index++);
57 77
 			value.owner = rs.getString(index++);
58 78
 			value.createdBy = rs.getString(index++);
... ...
@@ -61,7 +81,7 @@ public class ContainerStore extends JdbcAbstractStore {
61 81
 			value.created = new Date(rs.getTimestamp(index++).getTime());
62 82
 			value.updated = new Date(rs.getTimestamp(index++).getTime());
63 83
 			value.defaultContainer = rs.getBoolean(index++);
64
-			value.domainUid = rs.getString(index++);
84
+			value.domainUid = interner.intern(rs.getString(index++));
65 85
 			value.readOnly = rs.getBoolean(index++);
66 86
 			return index;
67 87
 		}