commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pino Silvaggio (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-500) ClassUtils.getAllInterfaces(...) could be more efficient
Date Wed, 29 Apr 2009 17:32:30 GMT

    [ https://issues.apache.org/jira/browse/LANG-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12704217#action_12704217
] 

Pino Silvaggio commented on LANG-500:
-------------------------------------

You are absolutely right.

My first tought was to only use a LinkedHashSet and return a list by copying the elements.

After some profiling I noticed a small (need I say) negligeable performance gain
by not copying the elements and directly returning a list.

However, it's less memory efficient obviously.

Anyhow, there is no doubt that your way or my way is more efficient
than the actual code. Firstly because it doesn't go through
the list of found interfaces on every iteration and secondly because
it doesn't try to rediscover the already found interfaces.

I will try to attach some profiling if I find the time.

> ClassUtils.getAllInterfaces(...) could be more efficient
> --------------------------------------------------------
>
>                 Key: LANG-500
>                 URL: https://issues.apache.org/jira/browse/LANG-500
>             Project: Commons Lang
>          Issue Type: Improvement
>    Affects Versions: 3.x
>            Reporter: Pino Silvaggio
>            Priority: Trivial
>
> This could seem like a very minor thing but why not improve
> the code once in a while...
> Something like this could replace the current inefficient code:
> {code}
>     public static List<Class<?>> getAllInterfaces(Class<?> clazz)
>     {
>         if (clazz == null)
>         {
>             return null;
>         }
>         HashSet<Class<?>> interfacesSet = new HashSet<Class<?>>();
>         LinkedList<Class<?>> interfacesList = new LinkedList<Class<?>>();
>         getAllInterfaces(clazz, interfacesSet, interfacesList);
>         return interfacesList;
>     }
>     private static void getAllInterfaces(
>             Class<?> clazz,
>             HashSet<Class<?>> interfacesSet, List<Class<?>> interfacesList)
>     {
>         while (clazz != null)
>         {
>             Class<?>[] interfaces = clazz.getInterfaces();
>             for (Class<?> i : interfaces)
>             {
>                 if (!interfacesSet.add(i))
>                 {
>                     interfacesList.add(i);
>                     getAllInterfaces(i, interfacesSet, interfacesList);
>                 }
>             }
>             clazz = clazz.getSuperclass();
>         }
>     }
> {code}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message