Skip to content

Commit

Permalink
Added cache for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEasy committed May 17, 2022
1 parent 1f34da9 commit ad259e6
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -777,20 +777,9 @@ public NodePointer getVariablePointer(QName name) {
* @return Function
*/
public Function getFunction(QName functionName, Object[] parameters) {
Function func = null;
int hashParameters;
if (parameters == null) {
hashParameters = 0;
}
else
{
List<Object> paramList = Arrays.asList(parameters);
List<Class> classList = paramList.stream().map(p -> p.getClass()).collect(Collectors.toList());
hashParameters = Arrays.hashCode(classList.toArray());
}
Integer functionHash = Integer.valueOf(Objects.hash(functionName, hashParameters));
Integer functionHash = getFunctionHash(functionName, parameters);
if (CACHE_ENABLED) {
func = functionCache.get(functionHash);
Function func = functionCache.get(functionHash);
if (func != null) {
return func;
}
Expand All @@ -803,7 +792,7 @@ public Function getFunction(QName functionName, Object[] parameters) {
while (funcCtx != null) {
funcs = funcCtx.getFunctions();
if (funcs != null) {
func = funcs.getFunction(namespace, name, parameters);
Function func = funcs.getFunction(namespace, name, parameters);
if (func != null) {
if (CACHE_ENABLED) {
functionCache.put(functionHash, func);
Expand All @@ -817,6 +806,27 @@ public Function getFunction(QName functionName, Object[] parameters) {
"Undefined function: " + functionName.toString());
}


private static Integer getFunctionHash(QName functionName, Object[] parameters)
{
if (!CACHE_ENABLED) {
return Integer.valueOf(-1);
}

int hashParameters;
if (parameters == null) {
hashParameters = 0;
}
else
{
List<Object> paramList = Arrays.asList(parameters);
List<Class> classList = paramList.stream().map(p -> p.getClass()).collect(Collectors.toList());
hashParameters = Arrays.hashCode(classList.toArray());
}
return Integer.valueOf(Objects.hash(functionName, hashParameters));
}


public void registerNamespace(String prefix, String namespaceURI) {
if (namespaceResolver.isSealed()) {
namespaceResolver = (NamespaceResolver) namespaceResolver.clone();
Expand Down

0 comments on commit ad259e6

Please sign in to comment.