Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » [SOLVED] Cache resolved ASTNode binding in comparator?
icon5.gif  [SOLVED] Cache resolved ASTNode binding in comparator? [message #650109] Fri, 21 January 2011 13:44 Go to next message
Christophe Bismuth is currently offline Christophe Bismuth
Messages: 67
Registered: July 2009
Location: Paris, France
Member

Hi,

I'm not sure resolving binding in the compare implementation of a comparator is a best practice. The same binding is resolved several times.

I'm thinking of caching the binding for each compare session (Collections.sort(...)). Does anyone already do it?

What would be the best key to use from an ASTNode instance?

Thanks,
Chris

[Updated on: Thu, 27 January 2011 03:15]

Report message to a moderator

Re: Cache resolved ASTNode binding in comparator? [message #650161 is a reply to message #650109] Fri, 21 January 2011 21:25 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak Azad
Messages: 545
Registered: July 2009
Senior Member
On 1/22/2011 12:14 AM, Christophe Bismuth wrote:
> Hi,
>
> I'm not sure resolving binding in the compare implementation of a
> comparator is a best practice. The same binding is resolved several times.
>
> I'm thinking of caching the binding for each compare session
> (Collections.sort(...)). Does anyone already do it?
>
> What would be the best key to use from an ASTNode instance?
>
> Thanks,
> Chris
Can you paste a code snippet here ?
Re: Cache resolved ASTNode binding in comparator? [message #650180 is a reply to message #650161] Sat, 22 January 2011 10:06 Go to previous messageGo to next message
Christophe Bismuth is currently offline Christophe Bismuth
Messages: 67
Registered: July 2009
Location: Paris, France
Member

Hi Deepak,

Here is a code snippet of a comparator which sort catch blocks by exception hierarchy or alphabetically when exceptions are not related.

Then, I'm not so sure the API below is safe. Could you tell me more about it? When cache should be reset?

CatchClauseComparator.getBinding(Type): ITypeBinding


Thank you,
Chris

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.core.dom.CatchClause;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Type;

public final class CatchClauseComparator implements Comparator<CatchClause> {
    private final Map<Integer, ITypeBinding> _bindings = new HashMap<Integer, ITypeBinding>();

    @Override
    public int compare(final CatchClause o1, final CatchClause o2) {
        final ITypeBinding exceptionType1 = this.getBinding(o1.getException().getType());
        final ITypeBinding exceptionType2 = this.getBinding(o2.getException().getType());

        int rval = 0;

        if (CatchClauseComparator.isSubType(exceptionType1, exceptionType2)) {
            rval = Integer.MIN_VALUE;
        } else if (CatchClauseComparator.isSubType(exceptionType2, exceptionType1)) {
            rval = Integer.MAX_VALUE;
        } else {
            rval = exceptionType1.getJavaElement().getElementName().compareTo(exceptionType2.getJavaElement().getElementName());
        }

        return rval;
    }

    private ITypeBinding getBinding(final Type type) {
        ITypeBinding binding = this._bindings.get(type.hashCode());

        if (binding == null) {
            binding = type.resolveBinding();
            this._bindings.put(type.hashCode(), binding);
        }

        return binding;
    }

    private static boolean isSubType(final ITypeBinding type1, final ITypeBinding type2) {
        boolean isSubType = false;

        ITypeBinding superClass = type1.getSuperclass();
        while ((superClass != null) && !isSubType) {
            if (superClass.equals(type2)) {
                isSubType = true;
            } else {
                superClass = superClass.getSuperclass();
            }
        }

        return isSubType;
    }
}

Re: Cache resolved ASTNode binding in comparator? [message #650185 is a reply to message #650180] Sat, 22 January 2011 11:26 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier Thomann
Messages: 518
Registered: July 2009
Senior Member
It is a bad idea to cache bindings. They hold to a lot of memory. If you really need to, then you should flush it as soon as possible.

Olivier
Re: Cache resolved ASTNode binding in comparator? [message #650186 is a reply to message #650185] Sat, 22 January 2011 11:34 Go to previous messageGo to next message
Christophe Bismuth is currently offline Christophe Bismuth
Messages: 67
Registered: July 2009
Location: Paris, France
Member

Is it wise enough to hold them only while sorting a collection?
Re: Cache resolved ASTNode binding in comparator? [message #650893 is a reply to message #650186] Wed, 26 January 2011 23:16 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam Kandula
Messages: 444
Registered: July 2009
Senior Member
It should be fine to cache the bindings during the sort. You don't want to cache your ASTNode(s) and bindings across createASTs() calls.
Re: Cache resolved ASTNode binding in comparator? [message #650923 is a reply to message #650893] Thu, 27 January 2011 03:13 Go to previous message
Christophe Bismuth is currently offline Christophe Bismuth
Messages: 67
Registered: July 2009
Location: Paris, France
Member

Thank you Satyam.
Previous Topic:How to use eclipse variables in plugin ?
Next Topic:Where do I find org.eclipse.cdt.core.testplugin ?
Goto Forum:
  


Current Time: Wed Jul 30 11:22:54 EDT 2014

Powered by FUDForum. Page generated in 0.01902 seconds