Changeset 7064 in josm for trunk/src/org


Ignore:
Timestamp:
2014-05-05T13:26:03+02:00 (10 years ago)
Author:
bastiK
Message:

see #9691 - add back reverted optimization [7056] (modified so it is thread safe)

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r7057 r7064  
    123123            final TagCheck check = new TagCheck(rule);
    124124            boolean containsSetClassExpression = false;
    125             for (Instruction i : rule.declaration) {
     125            for (Instruction i : rule.declaration.instructions) {
    126126                if (i instanceof Instruction.AssignmentInstruction) {
    127127                    final Instruction.AssignmentInstruction ai = (Instruction.AssignmentInstruction) i;
     
    161161            }
    162162            if (check.errors.isEmpty() && !containsSetClassExpression) {
    163                 throw new RuntimeException("No throwError/throwWarning/throwOther given! You should specify a validation error message for " + rule.selectors);
     163                throw new RuntimeException("No throwError/throwWarning/throwOther given! You should specify a validation error message for " + rule.selector);
    164164            } else if (check.errors.size() > 1) {
    165                 throw new RuntimeException("More than one throwError/throwWarning/throwOther given! You should specify a single validation error message for " + rule.selectors);
     165                throw new RuntimeException("More than one throwError/throwWarning/throwOther given! You should specify a single validation error message for " + rule.selector);
    166166            }
    167167            return check;
     
    191191            for (Iterator<MapCSSRule> it = source.rules.iterator(); it.hasNext(); ) {
    192192                MapCSSRule x = it.next();
    193                 if (x.selectors.size() == 1) {
    194                     Selector sel = x.selectors.get(0);
    195                     if (sel instanceof GeneralSelector) {
    196                         GeneralSelector gs = (GeneralSelector) sel;
    197                         if ("meta".equals(gs.base) && gs.getConditions().isEmpty()) {
    198                             it.remove();
    199                         }
     193                if (x.selector instanceof GeneralSelector) {
     194                    GeneralSelector gs = (GeneralSelector) x.selector;
     195                    if ("meta".equals(gs.base) && gs.getConditions().isEmpty()) {
     196                        it.remove();
    200197                    }
    201198                }
     
    214211
    215212        Selector whichSelectorMatchesEnvironment(Environment env) {
    216             for (Selector i : rule.selectors) {
    217                 env.clearSelectorMatchingInformation();
    218                 if (i.matches(env)) {
    219                     return i;
    220                 }
     213            env.clearSelectorMatchingInformation();
     214            if (rule.selector.matches(env)) {
     215                return rule.selector;
    221216            }
    222217            return null;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParser.jj

    r7057 r7064  
    1818import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction;
    1919import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule;
     20import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration;
    2021import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    2122import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
     
    4849    MapCSSStyleSource sheet;
    4950    StringBuilder sb;
     51    int declarationCounter;
    5052
    5153    /**
     
    7173    public MapCSSParser(InputStream in, String encoding, LexicalState initState) {
    7274        this(createTokenManager(in, encoding, initState));
     75        declarationCounter = 0;
    7376    }
    7477
     
    469472void sheet(MapCSSStyleSource sheet):
    470473{
    471     MapCSSRule r;
    472474}
    473475{
     
    476478    (
    477479        try {
    478             r=rule() { if (r != null) { sheet.rules.add(r); } } w()
     480            rule() w()
    479481        } catch (MapCSSException mex) {
    480482            error_skipto(RBRACE, mex);
     
    488490}
    489491
    490 MapCSSRule rule():
     492void rule():
    491493{
    492494    List<Selector> selectors = new ArrayList<Selector>();
    493495    Selector sel;
    494     List<Instruction> decl;
     496    Declaration decl;
    495497}
    496498{
     
    501503    )*
    502504    decl=declaration()
    503     { return new MapCSSRule(selectors, decl); }
     505    {
     506        for (Selector s : selectors) {
     507            sheet.rules.add(new MapCSSRule(s, decl));
     508        }
     509    }
    504510}
    505511
     
    707713}
    708714
    709 List<Instruction> declaration() :
     715Declaration declaration() :
    710716{
    711717    List<Instruction> ins = new ArrayList<Instruction>();
     
    723729            ( <EQUAL> val=expression() )?
    724730            { ins.add(new Instruction.AssignmentInstruction(key.image, val == null ? true : val, true)); }
    725             ( <RBRACE> { return ins; } | <SEMICOLON> w() )
     731            ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
    726732        )
    727733    |
     
    732738                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
    733739                w()
    734                 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
     740                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
    735741            |
    736742            LOOKAHEAD( expression() ( <SEMICOLON> | <RBRACE> ) )
    737743                val=expression()
    738744                { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
    739                 ( <RBRACE> { return ins; } | <SEMICOLON> w() )
     745                ( <RBRACE> { return new Declaration(ins, declarationCounter++); } | <SEMICOLON> w() )
    740746            |
    741747                val=readRaw() w() { ins.add(new Instruction.AssignmentInstruction(key.image, val, false)); }
     
    743749    )*
    744750    <RBRACE>
    745     { return ins; }
     751    { return new Declaration(ins, declarationCounter++); }
    746752}
    747753
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSRule.java

    r7057 r7064  
    99public class MapCSSRule {
    1010
    11     public List<Selector> selectors;
    12     public List<Instruction> declaration;
     11    public final Selector selector;
     12    public final Declaration declaration;
    1313
    14     public MapCSSRule(List<Selector> selectors, List<Instruction> declaration) {
    15         this.selectors = selectors;
     14    public static class Declaration {
     15        public final List<Instruction> instructions;
     16        // declarations in the StyleSource are numbered consecutively
     17        public final int idx;
     18
     19        public Declaration(List<Instruction> instructions, int idx) {
     20            this.instructions = instructions;
     21            this.idx = idx;
     22        }
     23    }
     24   
     25    public MapCSSRule(Selector selector, Declaration declaration) {
     26        this.selector = selector;
    1627        this.declaration = declaration;
    1728    }
     
    2334     */
    2435    public void execute(Environment env) {
    25         for (Instruction i : declaration) {
     36        for (Instruction i : declaration.instructions) {
    2637            i.execute(env);
    2738        }
     
    3041    @Override
    3142    public String toString() {
    32         return Utils.join(",", selectors) + " {\n  " + Utils.join("\n  ", declaration) + "\n}";
     43        return selector + " {\n  " + Utils.join("\n  ", declaration.instructions) + "\n}";
    3344    }
    3445}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r7057 r7064  
    2828import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.ChildOrParentSelector;
    2929import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.GeneralSelector;
    30 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.OptimizedGeneralSelector;
    3130import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.MapCSSParser;
    3231import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
     
    118117        // optimization: filter rules for different primitive types
    119118        for (MapCSSRule r: rules) {
    120             List<Selector> nodeSel = new ArrayList<>();
    121             List<Selector> waySel = new ArrayList<>();
    122             List<Selector> relationSel = new ArrayList<>();
    123             List<Selector> multipolygonSel = new ArrayList<>();
    124             for (Selector sel : r.selectors) {
    125                 // find the rightmost selector, this must be a GeneralSelector
    126                 Selector selRightmost = sel;
    127                 while (selRightmost instanceof ChildOrParentSelector) {
    128                     selRightmost = ((ChildOrParentSelector) selRightmost).right;
    129                 }
    130                 Selector optimizedSel = sel.optimizedBaseCheck();
    131                 switch (((GeneralSelector) selRightmost).getBase()) {
    132                     case "node":
    133                         nodeSel.add(optimizedSel);
    134                         break;
    135                     case "way":
    136                         waySel.add(optimizedSel);
    137                         break;
    138                     case "area":
    139                         waySel.add(optimizedSel);
    140                         multipolygonSel.add(optimizedSel);
    141                         break;
    142                     case "relation":
    143                         relationSel.add(optimizedSel);
    144                         multipolygonSel.add(optimizedSel);
    145                         break;
    146                     case "*":
    147                         nodeSel.add(optimizedSel);
    148                         waySel.add(optimizedSel);
    149                         relationSel.add(optimizedSel);
    150                         multipolygonSel.add(optimizedSel);
    151                         break;
    152                 }
    153             }
    154             nodeRules.add(new MapCSSRule(nodeSel, r.declaration));
    155             wayRules.add(new MapCSSRule(waySel, r.declaration));
    156             relationRules.add(new MapCSSRule(relationSel, r.declaration));
    157             multipolygonRules.add(new MapCSSRule(multipolygonSel, r.declaration));
    158         }
    159         rules.clear();
     119            // find the rightmost selector, this must be a GeneralSelector
     120            Selector selRightmost = r.selector;
     121            while (selRightmost instanceof ChildOrParentSelector) {
     122                selRightmost = ((ChildOrParentSelector) selRightmost).right;
     123            }
     124            MapCSSRule optRule = new MapCSSRule(r.selector.optimizedBaseCheck(), r.declaration);
     125            switch (((GeneralSelector) selRightmost).getBase()) {
     126                case "node":
     127                    nodeRules.add(optRule);
     128                    break;
     129                case "way":
     130                    wayRules.add(optRule);
     131                    break;
     132                case "area":
     133                    wayRules.add(optRule);
     134                    multipolygonRules.add(optRule);
     135                    break;
     136                case "relation":
     137                    relationRules.add(optRule);
     138                    multipolygonRules.add(optRule);
     139                    break;
     140                case "*":
     141                    nodeRules.add(optRule);
     142                    wayRules.add(optRule);
     143                    relationRules.add(optRule);
     144                    multipolygonRules.add(optRule);
     145                    break;
     146            }
     147        }
    160148    }
    161149   
     
    217205        Environment env = new Environment(n, mc, "default", this);
    218206
    219         NEXT_RULE:
    220207        for (MapCSSRule r : rules) {
    221             for (Selector s : r.selectors) {
    222                 if ((s instanceof GeneralSelector)) {
    223                     GeneralSelector gs = (GeneralSelector) s;
    224                     if (gs.getBase().equals(type)) {
    225                         if (!gs.matchesConditions(env)) {
    226                             continue NEXT_RULE;
    227                         }
    228                         r.execute(env);
     208            if ((r.selector instanceof GeneralSelector)) {
     209                GeneralSelector gs = (GeneralSelector) r.selector;
     210                if (gs.getBase().equals(type)) {
     211                    if (!gs.matchesConditions(env)) {
     212                        continue;
    229213                    }
     214                    r.execute(env);
    230215                }
    231216            }
     
    254239            }
    255240        }
    256         RULE: for (MapCSSRule r : matchingRules) {
    257             for (Selector s : r.selectors) {
    258                 env.clearSelectorMatchingInformation();
    259                 if (s.matches(env)) { // as side effect env.parent will be set (if s is a child selector)
    260                     if (s.getRange().contains(scale)) {
    261                         mc.range = Range.cut(mc.range, s.getRange());
    262                     } else {
    263                         mc.range = mc.range.reduceAround(scale, s.getRange());
    264                         continue;
     241       
     242        // the declaration indices are sorted, so it suffices to save the
     243        // last used index
     244        int lastDeclUsed = -1;
     245       
     246        for (MapCSSRule r : matchingRules) {
     247            env.clearSelectorMatchingInformation();
     248            if (r.selector.matches(env)) { // as side effect env.parent will be set (if s is a child selector)
     249                Selector s = r.selector;
     250                if (s.getRange().contains(scale)) {
     251                    mc.range = Range.cut(mc.range, s.getRange());
     252                } else {
     253                    mc.range = mc.range.reduceAround(scale, s.getRange());
     254                    continue;
     255                }
     256
     257                if (r.declaration.idx == lastDeclUsed) continue; // don't apply one declaration more than once
     258                lastDeclUsed = r.declaration.idx;
     259                String sub = s.getSubpart();
     260                if (sub == null) {
     261                    sub = "default";
     262                }
     263                else if ("*".equals(sub)) {
     264                    for (Entry<String, Cascade> entry : mc.getLayers()) {
     265                        env.layer = entry.getKey();
     266                        if (Utils.equal(env.layer, "*")) {
     267                            continue;
     268                        }
     269                        r.execute(env);
    265270                    }
    266 
    267                     String sub = s.getSubpart();
    268                     if (sub == null) {
    269                         sub = "default";
    270                     }
    271                     else if ("*".equals(sub)) {
    272                         for (Entry<String, Cascade> entry : mc.getLayers()) {
    273                             env.layer = entry.getKey();
    274                             if (Utils.equal(env.layer, "*")) {
    275                                 continue;
    276                             }
    277                             r.execute(env);
    278                         }
    279                     }
    280                     env.layer = sub;
    281                     r.execute(env);
    282                     continue RULE;
    283                 }
     271                }
     272                env.layer = sub;
     273                r.execute(env);
    284274            }
    285275        }
Note: See TracChangeset for help on using the changeset viewer.