Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Performance Issues: IType - newTypeHierarchy(...)(Calling this method on special types takes over 1 sec)
Performance Issues: IType - newTypeHierarchy(...) [message #643159] Sun, 05 December 2010 14:38 Go to next message
Andreas Kaluza is currently offline Andreas Kaluza
Messages: 10
Registered: December 2010
Junior Member
Hello,

I'm currently working on a project, which supports the code recommender project proposed for eclipse (http://eclipse.org/proposals/code-recommenders/).

Within my project I have to test whether two types are in a subtype / supertype hierarchies or not. For this I use the JDT framework, which allows to create such type hierarchies (Interface IType - method: newTypeHierarchy(...) ).

My task requires to test for several type hierarchies. After implementing and benchmarking I got a bottle neck. One method call takes sometimes over one second, which makes my plug-in unusable.

I created a small test plug-in to visualize the context:

public class EntryPoint implements IJavaCompletionProposalComputer {

	 /**
     * A cache for type hierarchies to avoid unnecessary computations
     */
    private final Map<IType, ITypeHierarchy> hierarchies = new HashMap<IType, ITypeHierarchy>();

	@Override
	public List<?> computeCompletionProposals(
			ContentAssistInvocationContext context, IProgressMonitor monitor) {
		
		final JavaContentAssistInvocationContext jctx = (JavaContentAssistInvocationContext) context;
		try {
			IType lookupContext = jctx.getCompilationUnit().getJavaProject().findType("java.lang.String");
			IType typeToCheck = jctx.getCompilationUnit().getJavaProject().findType("org.eclipse.ui.IWorkbench");
			System.out.println(this.isSubtype(lookupContext, typeToCheck) + " " + this.isSupertype(lookupContext, typeToCheck));
			
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

	@Override
	public List<?> computeContextInformation(
			ContentAssistInvocationContext context, IProgressMonitor monitor) {
		return Collections.emptyList();
	}

	@Override
	public String getErrorMessage() {
		return null;
	}

	@Override
	public void sessionEnded() {
		
	}

	@Override
	public void sessionStarted() {
	}
	
	
	/**
     * Tests for subtype relation between two types in a (once created) type hierarchy
     * @param context base type
     * @param subtype type to test to be a subtype or not
     * @return true, in case of subtype relation, else false
     * @throws JavaModelException
     * @see {@link #isSupertype(IType, IType)}
     */
	public boolean isSubtype(IType context, IType subtype) throws JavaModelException {
		long i = System.currentTimeMillis();
		ITypeHierarchy hierarchy = hierarchies.get(context);
		if (hierarchy == null) {
			hierarchy = context.newTypeHierarchy(new NullProgressMonitor());
			synchronized (hierarchies) {
				hierarchies.put(context, hierarchy);
			}
		}
		IType subtypes[] = hierarchy.getAllSubtypes(context);
		boolean ret = Arrays.asList(subtypes).contains(subtype);
		System.out.println("subtype: " + (System.currentTimeMillis() - i) + " " + context.getFullyQualifiedName() + " - " + subtype.getElementName());
		return ret;
	}

    /**
     * Tests for supertype relation between two types in a (once created) type hierarchy
     * @param context concrete type
     * @param subtype type to test to be a supertype or not
     * @return true, in case of supertype relation, else false
     * @throws JavaModelException
     * @see {@link #isSubtype(IType, IType)}
     */
	public boolean isSupertype(IType context, IType subtype) throws JavaModelException {
		long i = System.currentTimeMillis();
		ITypeHierarchy hierarchy = hierarchies.get(subtype);
		if (hierarchy == null) {
			hierarchy = subtype.newTypeHierarchy(new NullProgressMonitor());
			synchronized (hierarchies) {
				hierarchies.put(subtype, hierarchy);
			}
		}
		
		IType subtypes[] = hierarchy.getAllSubtypes(subtype);
		boolean ret = Arrays.asList(subtypes).contains(context);
		System.out.println("supertype: " + (System.currentTimeMillis() - i) + " " + context.getFullyQualifiedName() + " - " + subtype.getElementName());
		return ret;
	}
}


The two methods at the end are the point of interest. They check whether one type is a subtype / supertype of the other one or not. In this demonstration this plug-in checks the types java.lang.String and org.eclipse.ui.IWorkbench. Of course the result is in both cases negative.

My question is:
- Is there another way to test for such sub- and supertype hierarchies?
- Do you know how to increase the performance?

Best Regards,

Andreas K.
Re: Performance Issues: IType - newTypeHierarchy(...) [message #643180 is a reply to message #643159] Sun, 05 December 2010 19:27 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2837
Registered: July 2009
Senior Member
Hi Andreas,

I've experienced similar results with #newTypeHierarchy and finally
decided to collect the supertypes for both types by means of
#newSupertypeHierarchy and check whether the resultsets intersect.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 05.12.10 15:38, schrieb andy.kaluza@gmx.de:
> Hello,
>
> I'm currently working on a project, which supports the code recommender
> project proposed for eclipse
> (http://eclipse.org/proposals/code-recommenders/).
> Within my project I have to test whether two types are in a subtype /
> supertype hierarchies or not. For this I use the JDT framework, which
> allows to create such type hierarchies (Interface IType - method:
> newTypeHierarchy(...) ).
>
> My task requires to test for several type hierarchies. After
> implementing and benchmarking I got a bottle neck. One method call takes
> sometimes over one second, which makes my plug-in unusable.
>
> I created a small test plug-in to visualize the context:
>
>
> public class EntryPoint implements IJavaCompletionProposalComputer {
>
> /**
> * A cache for type hierarchies to avoid unnecessary computations
> */
> private final Map<IType, ITypeHierarchy> hierarchies = new
> HashMap<IType, ITypeHierarchy>();
>
> @Override
> public List<?> computeCompletionProposals(
> ContentAssistInvocationContext context, IProgressMonitor monitor) {
>
> final JavaContentAssistInvocationContext jctx =
> (JavaContentAssistInvocationContext) context;
> try {
> IType lookupContext =
> jctx.getCompilationUnit().getJavaProject().findType("java.lang.String ");
> IType typeToCheck =
> jctx.getCompilationUnit().getJavaProject().findType("org.eclipse.ui.IWorkbench ");
>
> System.out.println(this.isSubtype(lookupContext, typeToCheck) + " " +
> this.isSupertype(lookupContext, typeToCheck));
>
> } catch (JavaModelException e) {
> e.printStackTrace();
> }
> return Collections.emptyList();
> }
>
> @Override
> public List<?> computeContextInformation(
> ContentAssistInvocationContext context, IProgressMonitor monitor) {
> return Collections.emptyList();
> }
>
> @Override
> public String getErrorMessage() {
> return null;
> }
>
> @Override
> public void sessionEnded() {
>
> }
>
> @Override
> public void sessionStarted() {
> }
>
>
> /**
> * Tests for subtype relation between two types in a (once created) type
> hierarchy
> * @param context base type
> * @param subtype type to test to be a subtype or not
> * @return true, in case of subtype relation, else false
> * @throws JavaModelException
> * @see {@link #isSupertype(IType, IType)}
> */
> public boolean isSubtype(IType context, IType subtype) throws
> JavaModelException {
> long i = System.currentTimeMillis();
> ITypeHierarchy hierarchy = hierarchies.get(context);
> if (hierarchy == null) {
> hierarchy = context.newTypeHierarchy(new NullProgressMonitor());
> synchronized (hierarchies) {
> hierarchies.put(context, hierarchy);
> }
> }
> IType subtypes[] = hierarchy.getAllSubtypes(context);
> boolean ret = Arrays.asList(subtypes).contains(subtype);
> System.out.println("subtype: " + (System.currentTimeMillis() - i) + " "
> + context.getFullyQualifiedName() + " - " + subtype.getElementName());
> return ret;
> }
>
> /**
> * Tests for supertype relation between two types in a (once created)
> type hierarchy
> * @param context concrete type
> * @param subtype type to test to be a supertype or not
> * @return true, in case of supertype relation, else false
> * @throws JavaModelException
> * @see {@link #isSubtype(IType, IType)}
> */
> public boolean isSupertype(IType context, IType subtype) throws
> JavaModelException {
> long i = System.currentTimeMillis();
> ITypeHierarchy hierarchy = hierarchies.get(subtype);
> if (hierarchy == null) {
> hierarchy = subtype.newTypeHierarchy(new NullProgressMonitor());
> synchronized (hierarchies) {
> hierarchies.put(subtype, hierarchy);
> }
> }
>
> IType subtypes[] = hierarchy.getAllSubtypes(subtype);
> boolean ret = Arrays.asList(subtypes).contains(context);
> System.out.println("supertype: " + (System.currentTimeMillis() - i) + "
> " + context.getFullyQualifiedName() + " - " + subtype.getElementName());
> return ret;
> }
> }
>
>
> The two methods at the end are the point of interest. They check whether
> one type is a subtype / supertype of the other one or not. In this
> demonstration this plug-in checks the types java.lang.String and
> org.eclipse.ui.IWorkbench. Of course the result is in both cases negative.
>
> My question is:
> - Is there another way to test for such sub- and supertype hierarchies?
> - Do you know how to increase the performance?
>
> Best Regards,
>
> Andreas K.
Re: Performance Issues: IType - newTypeHierarchy(...) [message #643276 is a reply to message #643180] Mon, 06 December 2010 11:17 Go to previous message
Andreas Kaluza is currently offline Andreas Kaluza
Messages: 10
Registered: December 2010
Junior Member
Hello Sebastian,

thank you for the fast response and solution. It is really much faster.

Regards,

Andreas
Previous Topic:Find if a method overrides a method belonging in a library or the java api
Next Topic:Re-executing plug-Ins: Performance Issues
Goto Forum:
  


Current Time: Tue Sep 23 16:42:45 GMT 2014

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

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