Skip to main content


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 18:44 Go to next message
Christophe Bismuth is currently offline Christophe BismuthFriend
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 08:15]

Report message to a moderator

Re: Cache resolved ASTNode binding in comparator? [message #650161 is a reply to message #650109] Sat, 22 January 2011 02:25 Go to previous messageGo to next message
Deepak Azad is currently offline Deepak AzadFriend
Messages: 543
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 15:06 Go to previous messageGo to next message
Christophe Bismuth is currently offline Christophe BismuthFriend
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 16:26 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
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 16:34 Go to previous messageGo to next message
Christophe Bismuth is currently offline Christophe BismuthFriend
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] Thu, 27 January 2011 04:16 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
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 08:13 Go to previous message
Christophe Bismuth is currently offline Christophe BismuthFriend
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: Thu Apr 25 14:28:01 GMT 2024

Powered by FUDForum. Page generated in 0.02997 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top