001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.util;
007:
008: import java.lang.ref.SoftReference;
009:
010: import org.h2.constant.SysProperties;
011:
012: /**
013: * The string cache helps re-use string objects and therefore save memory.
014: * It uses a soft reference cache to keep frequently used strings in memory.
015: * The effect is similar to calling String.intern(), but faster.
016: */
017: public class StringCache {
018: private static final boolean ENABLED = true;
019: private static SoftReference softCache = new SoftReference(null);
020:
021: // testing: cacheHit / miss are public!
022: // public static int cacheHit = 0, cacheMiss = 0;
023:
024: // 4703
025: // public static String get(String s) {
026: // if (s == null) {
027: // return s;
028: // } else if (s.length() == 0) {
029: // return "";
030: // }
031: // if (!Constants.USE_OBJECT_CACHE
032: // || !ENABLED || s.length() > MAX_CACHE_SIZE / 10) {
033: // return s;
034: // }
035: // int hash = s.hashCode();
036: // int index = hash & (Constants.OBJECT_CACHE_SIZE - 1);
037: // String cached = cache[index];
038: // if (cached != null) {
039: // if (s.equals(cached)) {
040: // // cacheHit++;
041: // return cached;
042: // }
043: // }
044: // // cacheMiss++;
045: // replace(index, s);
046: // return s;
047: // }
048:
049: // 3500
050: // public static String get(String s) {
051: // return s;
052: // }
053:
054: // 3906
055: public static String get(String s) {
056: if (!SysProperties.OBJECT_CACHE || !ENABLED) {
057: return s;
058: }
059: if (s == null) {
060: return s;
061: } else if (s.length() == 0) {
062: return "";
063: }
064: String[] cache = (String[]) softCache.get();
065: int hash = s.hashCode();
066: if (cache == null) {
067: cache = new String[SysProperties.OBJECT_CACHE_SIZE];
068: softCache = new SoftReference(cache);
069: }
070: int index = hash & (SysProperties.OBJECT_CACHE_SIZE - 1);
071: String cached = cache[index];
072: if (cached != null) {
073: if (s.equals(cached)) {
074: // cacheHit++;
075: return cached;
076: }
077: }
078: cache[index] = s;
079: return s;
080: }
081:
082: public static String getNew(String s) {
083: if (!SysProperties.OBJECT_CACHE || !ENABLED) {
084: return s;
085: }
086: if (s == null) {
087: return s;
088: } else if (s.length() == 0) {
089: return "";
090: }
091: String[] cache = (String[]) softCache.get();
092: int hash = s.hashCode();
093: if (cache == null) {
094: cache = new String[SysProperties.OBJECT_CACHE_SIZE];
095: softCache = new SoftReference(cache);
096: }
097: int index = hash & (SysProperties.OBJECT_CACHE_SIZE - 1);
098: String cached = cache[index];
099: if (cached != null) {
100: if (s.equals(cached)) {
101: // cacheHit++;
102: return cached;
103: }
104: }
105: // create a new object that is not shared
106: // (to avoid out of memory if it is a substring of a big String)
107: s = new String(s); // NOPMD
108: cache[index] = s;
109: return s;
110: }
111:
112: public static void clearCache() {
113: softCache = new SoftReference(null);
114: }
115:
116: }
|