Skip to content

Commit

Permalink
perf: avoid unnecessary casts during CSS styling
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jun 7, 2023
1 parent 02d4f0a commit 4cc4add
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 51 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.core
Bundle-Version: 0.5.4.qualifier
Bundle-Version: 0.5.5.qualifier
Require-Bundle: org.apache.batik.css;bundle-version="1.9.1";resolution:=optional,
org.apache.batik.util;bundle-version="1.9.1";resolution:=optional,
com.google.gson;bundle-version="2.9.0",
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>org.eclipse.tm4e.core</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.5.4-SNAPSHOT</version>
<version>0.5.5-SNAPSHOT</version>

<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

public abstract class AbstractCombinatorCondition implements CombinatorCondition, ExtendedCondition {

private final Condition firstCondition;
private final Condition secondCondition;
protected final ExtendedCondition firstCondition;
protected final ExtendedCondition secondCondition;

/**
* Creates a new CombinatorCondition object.
*/
protected AbstractCombinatorCondition(final Condition c1, final Condition c2) {
protected AbstractCombinatorCondition(final ExtendedCondition c1, final ExtendedCondition c2) {
firstCondition = c1;
secondCondition = c2;
}
Expand All @@ -39,7 +39,6 @@ public Condition getSecondCondition() {

@Override
public int getSpecificity() {
return ((ExtendedCondition) getFirstCondition()).getSpecificity()
+ ((ExtendedCondition) getSecondCondition()).getSpecificity();
return firstCondition.getSpecificity() + secondCondition.getSpecificity();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
*/
package org.eclipse.tm4e.core.internal.theme.css;

import org.w3c.css.sac.Condition;

final class CSSAndCondition extends AbstractCombinatorCondition {

/**
* Creates a new CombinatorCondition object.
*/
CSSAndCondition(final Condition c1, final Condition c2) {
CSSAndCondition(final ExtendedCondition c1, final ExtendedCondition c2) {
super(c1, c2);
}

Expand All @@ -29,13 +27,11 @@ public short getConditionType() {

@Override
public int nbMatch(final String... names) {
return ((ExtendedCondition) getFirstCondition()).nbMatch(names)
+ ((ExtendedCondition) getSecondCondition()).nbMatch(names);
return firstCondition.nbMatch(names) + secondCondition.nbMatch(names);
}

@Override
public int nbClass() {
return ((ExtendedCondition) getFirstCondition()).nbClass()
+ ((ExtendedCondition) getSecondCondition()).nbClass();
return firstCondition.nbClass() + secondCondition.nbClass();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public AttributeCondition createAttributeCondition(final String localName, final

@Override
public CombinatorCondition createAndCondition(final Condition first, final Condition second) throws CSSException {
return new CSSAndCondition(first, second);
return new CSSAndCondition((ExtendedCondition) first, (ExtendedCondition) second);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,11 @@

final class CSSConditionalSelector implements ConditionalSelector, ExtendedSelector {

/**
* The simple selector.
*/
private final SimpleSelector simpleSelector;
private final ExtendedSelector selector;
private final ExtendedCondition condition;

/**
* The condition.
*/
private final Condition condition;

/**
* Creates a new ConditionalSelector object.
*/
CSSConditionalSelector(final SimpleSelector simpleSelector, final Condition condition) {
this.simpleSelector = simpleSelector;
CSSConditionalSelector(final ExtendedSelector simpleSelector, final ExtendedCondition condition) {
this.selector = simpleSelector;
this.condition = condition;
}

Expand All @@ -47,25 +37,21 @@ public Condition getCondition() {

@Override
public SimpleSelector getSimpleSelector() {
return simpleSelector;
return selector;
}

@Override
public int getSpecificity() {
return ((ExtendedSelector) getSimpleSelector()).getSpecificity()
+ ((ExtendedCondition) getCondition()).getSpecificity();
return selector.getSpecificity() + condition.getSpecificity();
}

@Override
public int nbMatch(final String... names) {
return ((ExtendedSelector) getSimpleSelector()).nbMatch(names)
+ ((ExtendedCondition) getCondition()).nbMatch(names);
return selector.nbMatch(names) + condition.nbMatch(names);
}

@Override
public int nbClass() {
return ((ExtendedSelector) getSimpleSelector()).nbClass()
+ ((ExtendedCondition) getCondition()).nbClass();
return selector.nbClass() + condition.nbClass();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CharacterDataSelector createCommentSelector(final String arg0) throws CSS
@Override
public ConditionalSelector createConditionalSelector(final SimpleSelector selector, final Condition condition)
throws CSSException {
return new CSSConditionalSelector(selector, condition);
return new CSSConditionalSelector((ExtendedSelector) selector, (ExtendedCondition) condition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
*/
package org.eclipse.tm4e.core.internal.theme.css;

interface ExtendedCondition {
import org.w3c.css.sac.Condition;

interface ExtendedCondition extends Condition {

/**
* Returns the specificity of this condition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
package org.eclipse.tm4e.core.internal.theme.css;

import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SimpleSelector;

public interface ExtendedSelector extends Selector {
public interface ExtendedSelector extends SimpleSelector {

/**
* Returns the specificity of this selector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.w3c.css.sac.CSSException;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.Parser;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SelectorList;

/**
Expand Down Expand Up @@ -69,15 +68,13 @@ public IStyle getBestStyle(final String... names) {
IStyle bestStyle = null;
for (final IStyle style : handler.getList()) {
final SelectorList list = ((CSSStyle) style).getSelectorList();
for (int i = 0; i < list.getLength(); i++) {
final Selector selector = list.item(i);
if (selector instanceof final ExtendedSelector s) {
final int nbMatch = s.nbMatch(names);
if ((nbMatch >= bestSpecificity || bestStyle == null)
&& nbMatch > 0 && nbMatch == s.nbClass()) {
bestStyle = style;
bestSpecificity = nbMatch;
}
for (int i = 0, l = list.getLength(); i < l; i++) {
final var selector = (ExtendedSelector) list.item(i);
final int nbMatch = selector.nbMatch(names);
if ((nbMatch >= bestSpecificity || bestStyle == null)
&& nbMatch > 0 && nbMatch == selector.nbClass()) {
bestStyle = style;
bestSpecificity = nbMatch;
}
}
}
Expand Down

0 comments on commit 4cc4add

Please sign in to comment.