Source:
<code>public JsonRpcResult getMatchedStylesForNode(JsonRpcPeer peer, JSONObject params) { final GetMatchedStylesForNodeRequest request = mObjectMapper.convertValue( params, GetMatchedStylesForNodeRequest.class); <START> final Map<String, List<CSSProperty>> selectors = new HashMap<>(); <END> mDocument.postAndWait(new Runnable() { @Override public void run() { Object elementForNodeId = mDocument.getElementForNodeId(request.nodeId); if (elementForNodeId == null) { LogUtil.w("Failed to get style of an element that does not exist, nodeid=" + request.nodeId); return; } mDocument.getElementStyles( elementForNodeId, new StyleAccumulator() { @Override public void store(String selector, String name, String value, boolean isDefault) { if (!isDefault) { List<CSSProperty> properties = selectors.get(selector); if (properties == null) { properties = new ArrayList<>(); selectors.put(selector, properties); } final CSSProperty property = new CSSProperty(); property.name = name; property.value = value; properties.add(property); } } }); } }); final GetMatchedStylesForNodeResult result = new GetMatchedStylesForNodeResult(); result.matchedCSSRules = new ArrayList<>(); for (String selectorName : selectors.keySet()) { final RuleMatch match = new RuleMatch(); match.matchingSelectors = ListUtil.newImmutableList(0); final Selector selector = new Selector(); selector.value = selectorName; final CSSRule rule = new CSSRule(); rule.origin = Origin.REGULAR; rule.selectorList = new SelectorList(); rule.selectorList.selectors = ListUtil.newImmutableList(selector); rule.style = new CSSStyle(); rule.style.cssProperties = new ArrayList<>(); match.rule = rule; result.matchedCSSRules.add(match); } return result; }</code><technical_language>a HashMap for temporary storage this is pretty heavy weight ... is alternative? keys expect here? reuse/recycle, especially strings put in repeated over? for instance: create cache 1 HashMap in mSelectorsCache field. this method, a track of ArrayList for selector. then, put(selector, null) end instead of throwing HashMap clear()ing it. constantly churn HashMap.Entry allocations I reasonable do this in a follow-up PR well, block right</technical_language>

Target:
public JsonRpcResult getMatchedStylesForNode(JsonRpcPeer peer, JSONObject params) { final GetMatchedStylesForNodeRequest request = mObjectMapper.convertValue( params, GetMatchedStylesForNodeRequest.class); mDocument.postAndWait(new Runnable() { @Override public void run() { Object elementForNodeId = mDocument.getElementForNodeId(request.nodeId); if (elementForNodeId == null) { LogUtil.w("Failed to get style of an element that does not exist, nodeid=" + request.nodeId); return; } mDocument.getElementStyles( elementForNodeId, new StyleAccumulator() { @Override public void store(String selector, String name, String value, boolean isDefault) { if (!isDefault) { ArrayList<CSSProperty> properties = mSelectorCollector.get(selector); if (properties == null) { properties = new ArrayList<>(); mSelectorCollector.put(selector, properties); } final CSSProperty property = new CSSProperty(); property.name = name; property.value = value; properties.add(property); } } }); } }); final GetMatchedStylesForNodeResult result = new GetMatchedStylesForNodeResult(); result.matchedCSSRules = new ArrayList<>(); result.inherited = Collections.emptyList(); result.pseudoElements = Collections.emptyList(); for (Map.Entry<String, ArrayList<CSSProperty>> entry : mSelectorCollector.entrySet()) { final ArrayList<CSSProperty> properties = entry.getValue(); if (properties.isEmpty()) { continue; } final RuleMatch match = new RuleMatch(); match.matchingSelectors = ListUtil.newImmutableList(0); final Selector selector = new Selector(); selector.value = entry.getKey(); final CSSRule rule = new CSSRule(); rule.origin = Origin.REGULAR; rule.selectorList = new SelectorList(); rule.selectorList.selectors = ListUtil.newImmutableList(selector); rule.style = new CSSStyle(); rule.style.cssProperties = ListUtil.copyToImmutableList(properties); rule.style.shorthandEntries = Collections.emptyList(); match.rule = rule; result.matchedCSSRules.add(match); properties.clear(); } return result; }