Page 1 of 1

StylesFilter fires many, many times

Posted: Sat Jan 11, 2014 6:10 am
by odaata
I have a question about creating a custom StylesFilter. I've developed one that performs an XQuery to get the values and labels needed for a combo box. My example case is a list of people, so my XQuery simply pulls out the xml:id and then transforms various parts of people's names, birth and death dates, etc. in order to generate labels to go along with the ids. I took the example given in the documentation http://www.oxygenxml.com/doc/ug-oxygen/ ... ilter.html and also located in the AuthorSDK as my basis and tweaked it. I did get it to work, but unfortunately, the filter() method is being called dozens of times for a single node (70+ times!), which in turn is compiling and executing my XQuery dozens of times. This causes the editor to run extremely slowly.

I tried adding the results from the query to static variables, so they were essentially cached in global variables, but when my StylesFilter implementation is called it does not have access to any static variables across instances, so it's re-running the XQuery regardless. How can I avoid this being called so many times? Ultimately, I'd like to be able to use static variables and cache the results so that multiple fields of the same type could use the same results from my XQuery, or at the very least find a way to only have it compile and call the XQuery once per node. Does anyone know how to do that?

Thanks for any help you can provide!

Re: StylesFilter fires many, many times

Posted: Mon Jan 13, 2014 11:55 am
by alex_jitianu
Hi,

The StylesFilter is called many times because we don't cache it's results on our side and the styles for an element are requested a lot. So it's up to the implementation to do a caching if necessary.

I suspect that you are using the node as the key in your cache. There might be a problem if this node is a pseudo element (:before/:after) because these are not present in the model and we create instances every time we need them. If this is indeed what's happening then you can keep the parent element as the key:

Code: Select all


AuthorNode key = authorNode;
if (authorNode.getType() == AuthorNode.NODE_TYPE_PSEUDO_ELEMENT) {
key = authorNode.getParent();
}
Might also help to use a WeakHashMap to avoid memory leaks generated by the fact you are keeping nodes in the cache.

If that's not the actual issue then it would help to take a look at the StylesFilter implementation so perhaps you could send it to our support email address.

Best regards,
Alex

Re: StylesFilter fires many, many times

Posted: Mon Jan 13, 2014 7:17 pm
by odaata
Actually, after reading your answer, I realized that I had made a mistake in my code which was causing it not to cache correctly, so in the end it was just a bug in the way I was caching my objects :-( Thanks for your help nonetheless!