Changeset e4ebb9a


Ignore:
Timestamp:
Nov 7, 2015 5:08:27 PM (5 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
83b9231
Parents:
07627152
Message:

Utils: Add caching string split()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    r07627152 re4ebb9a  
    3838import java.util.Map;
    3939import java.util.Properties;
     40import java.util.concurrent.ConcurrentHashMap;
     41import java.util.regex.Pattern;
    4042import java.util.zip.Deflater;
    4143
     
    18901892        return rv;
    18911893    }
     1894
     1895    /**
     1896     *  Same as s.split(regex) but caches the compiled pattern for speed.
     1897     *  This saves about 10 microseconds (Bulldozer) on subsequent invocations.
     1898     *
     1899     *  @param s non-null
     1900     *  @param regex non-null
     1901     *  @throws java.util.regex.PatternSyntaxException unchecked
     1902     *  @since 0.9.24
     1903     */
     1904    public static String[] split(String s, String regex) {
     1905        return split(s, regex, 0);
     1906    }
     1907
     1908    private static final ConcurrentHashMap<String, Pattern> patterns = new ConcurrentHashMap<String, Pattern>();
     1909
     1910    /**
     1911     *  Same as s.split(regex, limit) but caches the compiled pattern for speed.
     1912     *  This saves about 10 microseconds (Bulldozer) on subsequent invocations.
     1913     *
     1914     *  @param s non-null
     1915     *  @param regex non-null
     1916     *  @param limit result threshold
     1917     *  @throws java.util.regex.PatternSyntaxException unchecked
     1918     *  @since 0.9.24
     1919     */
     1920    public static String[] split(String s, String regex, int limit) {
     1921        Pattern p = patterns.get(regex);
     1922        if (p == null) {
     1923            p = Pattern.compile(regex);
     1924            patterns.putIfAbsent(regex, p);
     1925        }
     1926        return p.split(s, limit);
     1927    }
    18921928}
Note: See TracChangeset for help on using the changeset viewer.