Changeset 7080 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2014-05-08T23:02:32+02:00 (10 years ago)
Author:
bastiK
Message:

see #9691 - on single core machine do nothing fancy and stay in EDT - this saves some array copying; provide initial capacities for ArrayLists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

    r7059 r7080  
    2525import java.awt.geom.Point2D;
    2626import java.awt.geom.Rectangle2D;
     27import java.util.AbstractList;
    2728import java.util.ArrayList;
    2829import java.util.Collection;
     
    3536import java.util.concurrent.Executors;
    3637import java.util.concurrent.Future;
    37 
    3838import javax.swing.AbstractButton;
    3939import javax.swing.FocusManager;
    40 
    4140import org.openstreetmap.josm.Main;
    4241import org.openstreetmap.josm.data.Bounds;
     
    8382   
    8483    static {
    85         noThreads = Runtime.getRuntime().availableProcessors();
    86         styleCreatorPool = Executors.newFixedThreadPool(noThreads);
     84        noThreads = Main.pref.getInteger(
     85                "mappaint.StyledMapRenderer.style_creation.numberOfThreads",
     86                Runtime.getRuntime().availableProcessors());
     87        styleCreatorPool = noThreads <= 1 ? null : Executors.newFixedThreadPool(noThreads);
    8788    }
    8889
     
    234235
    235236            return Float.compare(this.style.object_z_index, other.style.object_z_index);
     237        }
     238    }
     239
     240    /**
     241     * Joined List build from two Lists (read-only).
     242     *
     243     * Extremely simple single-purpose implementation.
     244     * @param <T>
     245     */
     246    public static class CompositeList<T> extends AbstractList<T> {
     247        List<T> a,b;
     248
     249        public CompositeList(List<T> a, List<T> b) {
     250            this.a = a;
     251            this.b = b;
     252        }
     253
     254        @Override
     255        public T get(int index) {
     256            return index < a.size() ? a.get(index) : b.get(index - a.size());
     257        }
     258
     259        @Override
     260        public int size() {
     261            return a.size() + b.size();
    236262        }
    237263    }
     
    13151341        styles = MapPaintStyles.getStyles();
    13161342        styles.setDrawMultipolygon(drawMultipolygon);
    1317 
     1343       
    13181344        highlightWaySegments = data.getHighlightedWaySegments();
    13191345       
     
    13261352
    13271353        class ComputeStyleListWorker implements Callable<List<StyleRecord>>, Visitor {
    1328             private final List<StyleRecord> styleList;
    13291354            private final List<? extends OsmPrimitive> input;
    13301355            private final int from;
    13311356            private final int to;
     1357            private final List<StyleRecord> output;
    13321358
    13331359            /**
     
    13371363             * @param to last index + 1
    13381364             */
    1339             public ComputeStyleListWorker(List<? extends OsmPrimitive> input, int from, int to) {
    1340                 this.styleList = new ArrayList<>(to - from);
     1365            public ComputeStyleListWorker(final List<? extends OsmPrimitive> input, int from, int to, List<StyleRecord> output) {
    13411366                this.input = input;
    13421367                this.from = from;
    13431368                this.to = to;
     1369                this.output = output;
    13441370            }
    13451371           
     
    13521378                    }
    13531379                }
    1354                 return styleList;
     1380                return output;
    13551381            }
    13561382           
     
    14001426                StyleList sl = styles.get(osm, circum, nc);
    14011427                for (ElemStyle s : sl) {
    1402                     styleList.add(new StyleRecord(s, osm, flags));
     1428                    output.add(new StyleRecord(s, osm, flags));
    14031429                }
    14041430            }
     
    14081434                for (ElemStyle s : sl) {
    14091435                    if (drawMultipolygon && drawArea && s instanceof AreaElemStyle && (flags & FLAG_DISABLED) == 0) {
    1410                         styleList.add(new StyleRecord(s, osm, flags));
     1436                        output.add(new StyleRecord(s, osm, flags));
    14111437                    } else if (drawRestriction && s instanceof NodeElemStyle) {
    1412                         styleList.add(new StyleRecord(s, osm, flags));
     1438                        output.add(new StyleRecord(s, osm, flags));
    14131439                    }
    14141440                }
     
    14211447                        continue;
    14221448                    }
    1423                     styleList.add(new StyleRecord(s, osm, flags));
    1424                 }
    1425             }
    1426         }
    1427 
    1428         final List<ComputeStyleListWorker> tasks = new ArrayList<>();
    1429         final List<StyleRecord> allStyleElems = new ArrayList<>();
     1449                    output.add(new StyleRecord(s, osm, flags));
     1450                }
     1451            }
     1452        }
     1453        List<Node> nodes = data.searchNodes(bbox);
     1454        List<Way> ways = data.searchWays(bbox);
     1455        List<Relation> relations = data.searchRelations(bbox);
     1456       
     1457        final List<StyleRecord> allStyleElems = new ArrayList<>(nodes.size()+ways.size()+relations.size());
    14301458       
    14311459        class ConcurrentTasksHelper {
    1432             void createTasks(List<? extends OsmPrimitive> prims) {
    1433                 int bucketsize = Math.max(100, prims.size()/noThreads/3);
    1434                 for (int i=0; i*bucketsize < prims.size(); i++) {
    1435                     tasks.add(new ComputeStyleListWorker(prims, i*bucketsize, Math.min((i+1)*bucketsize, prims.size())));
    1436                 }
    1437             }
    14381460           
    1439             void runIt() {
    1440                 if (tasks.size() == 1) {
     1461            void process(List<? extends OsmPrimitive> prims) {
     1462                final List<ComputeStyleListWorker> tasks = new ArrayList<>();
     1463                final int bucketsize = Math.max(100, prims.size()/noThreads/3);
     1464                final int noBuckets = (prims.size() + bucketsize - 1) / bucketsize;
     1465                final boolean singleThread = noThreads == 1 || noBuckets == 1;
     1466                for (int i=0; i<noBuckets; i++) {
     1467                    int from = i*bucketsize;
     1468                    int to = Math.min((i+1)*bucketsize, prims.size());
     1469                    List<StyleRecord> target = singleThread ? allStyleElems : new ArrayList<StyleRecord>(to - from);
     1470                    tasks.add(new ComputeStyleListWorker(prims, from, to, target));
     1471                }
     1472                if (singleThread) {
    14411473                    try {
    1442                         allStyleElems.addAll(tasks.get(0).call());
     1474                        for (ComputeStyleListWorker task : tasks) {
     1475                            task.call();
     1476                        }
    14431477                    } catch (Exception ex) {
    14441478                        throw new RuntimeException(ex);
     
    14621496        // (Could be synchronized, but try to avoid this for
    14631497        // performance reasons.)
    1464         helper.createTasks(data.searchRelations(bbox));
    1465         helper.runIt();
    1466        
    1467         tasks.clear();
    1468         helper.createTasks(data.searchNodes(bbox));
    1469         helper.createTasks(data.searchWays(bbox));
    1470         helper.runIt();
    1471        
     1498        helper.process(relations);
     1499        @SuppressWarnings("unchecked")
     1500        List<OsmPrimitive> nodesAndWays = (List) new CompositeList(nodes, ways);
     1501        helper.process(nodesAndWays);
    14721502       
    14731503        if (Main.isTraceEnabled()) {
Note: See TracChangeset for help on using the changeset viewer.