Ignore:
Timestamp:
2015-08-08T12:32:18+02:00 (9 years ago)
Author:
wiktorn
Message:

Performance improvements.

  • getTileUrl now uses generic, staticly compiled pattern, instead of replaceAll (gives 2x improvement)
  • AbstractTileSourceLayer uses MemoryTileCache instead of JCS cache. This give huge improvement when there is a lot of tiles to show (eg. when min zoom is limited by user to some high value)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java

    r8637 r8647  
    4242    private Bounds worldBounds;
    4343
    44     private static final String PATTERN_HEADER  = "\\{header\\(([^,]+),([^}]+)\\)\\}";
    45     private static final String PATTERN_PROJ    = "\\{proj(\\([^})]+\\))?\\}";
    46     private static final String PATTERN_BBOX    = "\\{bbox\\}";
    47     private static final String PATTERN_W       = "\\{w\\}";
    48     private static final String PATTERN_S       = "\\{s\\}";
    49     private static final String PATTERN_E       = "\\{e\\}";
    50     private static final String PATTERN_N       = "\\{n\\}";
    51     private static final String PATTERN_WIDTH   = "\\{width\\}";
    52     private static final String PATTERN_HEIGHT  = "\\{height\\}";
     44    private static final Pattern PATTERN_HEADER  = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}");
     45    private static final Pattern PATTERN_PROJ    = Pattern.compile("\\{proj\\}");
     46    private static final Pattern PATTERN_BBOX    = Pattern.compile("\\{bbox\\}");
     47    private static final Pattern PATTERN_W       = Pattern.compile("\\{w\\}");
     48    private static final Pattern PATTERN_S       = Pattern.compile("\\{s\\}");
     49    private static final Pattern PATTERN_E       = Pattern.compile("\\{e\\}");
     50    private static final Pattern PATTERN_N       = Pattern.compile("\\{n\\}");
     51    private static final Pattern PATTERN_WIDTH   = Pattern.compile("\\{width\\}");
     52    private static final Pattern PATTERN_HEIGHT  = Pattern.compile("\\{height\\}");
     53    private static final Pattern PATTERN_PARAM   = Pattern.compile("\\{([^}]+)\\}");
    5354
    5455    private static final NumberFormat latLonFormat = new DecimalFormat("###0.0000000", new DecimalFormatSymbols(Locale.US));
    5556
    56     private static final String[] ALL_PATTERNS = {
     57    private static final Pattern[] ALL_PATTERNS = {
    5758        PATTERN_HEADER, PATTERN_PROJ, PATTERN_BBOX, PATTERN_W, PATTERN_S, PATTERN_E, PATTERN_N, PATTERN_WIDTH, PATTERN_HEIGHT
    5859    };
     
    6768        handleTemplate();
    6869        initProjection();
     70        // FIXME: remove in September 2015, when ImageryPreferenceEntry.tileSize will be initialized to -1 instead to 256
     71        // need to leave it as it is to keep compatiblity between tested and latest JOSM versions
     72        tileSize = WMSLayer.PROP_IMAGE_SIZE.get();
    6973    }
    7074
     
    8993    @Override
    9094    public int getDefaultTileSize() {
    91         return WMSLayer.PROP_IMAGE_SIZE.get();
    92     }
    93 
    94     // FIXME: remove in September 2015, when ImageryPreferenceEntry.tileSize will be initialized to -1 instead to 256
    95     // need to leave it as it is to keep compatiblity between tested and latest JOSM versions
    96     @Override
    97     public int getTileSize() {
    9895        return WMSLayer.PROP_IMAGE_SIZE.get();
    9996    }
     
    156153            bbox = String.format("%s,%s,%s,%s", latLonFormat.format(w), latLonFormat.format(s), latLonFormat.format(e), latLonFormat.format(n));
    157154        }
    158         return baseUrl.
    159                 replaceAll(PATTERN_PROJ,    myProjCode)
    160                 .replaceAll(PATTERN_BBOX,   bbox)
    161                 .replaceAll(PATTERN_W,      latLonFormat.format(w))
    162                 .replaceAll(PATTERN_S,      latLonFormat.format(s))
    163                 .replaceAll(PATTERN_E,      latLonFormat.format(e))
    164                 .replaceAll(PATTERN_N,      latLonFormat.format(n))
    165                 .replaceAll(PATTERN_WIDTH,  String.valueOf(getTileSize()))
    166                 .replaceAll(PATTERN_HEIGHT, String.valueOf(getTileSize()))
    167                 .replace(" ", "%20");
     155
     156        // Using StringBuffer and generic PATTERN_PARAM matcher gives 2x performance improvement over replaceAll
     157        StringBuffer url = new StringBuffer(baseUrl.length());
     158        Matcher matcher = PATTERN_PARAM.matcher(baseUrl);
     159        while (matcher.find()) {
     160            String replacement;
     161            switch (matcher.group(1)) {
     162            case "proj":
     163                replacement = myProjCode;
     164                break;
     165            case "bbox":
     166                replacement = bbox;
     167                break;
     168            case "w":
     169                replacement = latLonFormat.format(w);
     170                break;
     171            case "s":
     172                replacement = latLonFormat.format(s);
     173                break;
     174            case "e":
     175                replacement = latLonFormat.format(e);
     176                break;
     177            case "n":
     178                replacement = latLonFormat.format(n);
     179                break;
     180            case "width":
     181            case "height":
     182                replacement = String.valueOf(getTileSize());
     183                break;
     184            default:
     185                replacement = "{" + matcher.group(1) + "}";
     186            }
     187            matcher.appendReplacement(url, replacement);
     188        }
     189        matcher.appendTail(url);
     190        return url.toString().replace(" ", "%20");
    168191    }
    169192
     
    313336    public static void checkUrl(String url) {
    314337        CheckParameterUtil.ensureParameterNotNull(url, "url");
    315         Matcher m = Pattern.compile("\\{[^}]*\\}").matcher(url);
     338        Matcher m = PATTERN_PARAM.matcher(url);
    316339        while (m.find()) {
    317340            boolean isSupportedPattern = false;
    318             for (String pattern : ALL_PATTERNS) {
    319                 if (m.group().matches(pattern)) {
     341            for (Pattern pattern : ALL_PATTERNS) {
     342                if (pattern.matcher(m.group()).matches()) {
    320343                    isSupportedPattern = true;
    321344                    break;
     
    331354    private void handleTemplate() {
    332355        // Capturing group pattern on switch values
    333         Pattern pattern = Pattern.compile(PATTERN_HEADER);
    334356        StringBuffer output = new StringBuffer();
    335         Matcher matcher = pattern.matcher(this.baseUrl);
     357        Matcher matcher = PATTERN_HEADER.matcher(this.baseUrl);
    336358        while (matcher.find()) {
    337359            headers.put(matcher.group(1), matcher.group(2));
Note: See TracChangeset for help on using the changeset viewer.