Skip to content

Commit

Permalink
#340 cache attribute values of xml elements
Browse files Browse the repository at this point in the history
My profiler shows that `nsh.getClass((Element) e)` consumes quite a remarkable percentage of total CPU time.
So I hope caching these value could improve performance (though, my profiler doesn't show it :) ).
  • Loading branch information
asolntsev committed Jul 19, 2024
1 parent fc6312f commit 749ff44
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.xhtmlrenderer.extend.UserInterface;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.IdentityHashMap;
import java.util.Map;


/**
Expand All @@ -39,6 +41,7 @@ public class StandardAttributeResolver implements AttributeResolver {
private final NamespaceHandler nsh;
private final UserAgentCallback uac;
private final UserInterface ui;
private final Map<Node, String> classAttributeCache = new IdentityHashMap<>();

public StandardAttributeResolver(NamespaceHandler nsh, UserAgentCallback uac, UserInterface ui) {
this.nsh = nsh;
Expand All @@ -64,7 +67,7 @@ public String getAttributeValue(Node e, String namespaceURI, String attrName) {
*/
@Override
public String getClass(Node e) {
return nsh.getClass((Element) e);
return classAttributeCache.computeIfAbsent(e, (x) -> nsh.getClass((Element) e));
}

/**
Expand Down

0 comments on commit 749ff44

Please sign in to comment.