Skip to content

Commit

Permalink
HCANN-113 Reduce retained memory of AnnotationProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Oct 30, 2020
1 parent 2981611 commit 21b0359
Showing 1 changed file with 14 additions and 1 deletion.
Expand Up @@ -9,6 +9,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -72,7 +73,19 @@ else if ( m.getDefaultValue() != null ) {
if ( processedValuesFromDescriptor != descriptor.numberOfElements() ) {
throw new RuntimeException( "Trying to instanciate " + annotationType + " with unknown elements" );
}
return result;
return toSmallMap( result );
}

static <K, V> Map<K, V> toSmallMap(Map<K, V> map) {
switch ( map.size() ) {
case 0:
return Collections.emptyMap();
case 1:
Map.Entry<K, V> entry = map.entrySet().iterator().next();
return Collections.singletonMap( entry.getKey(), entry.getValue() );
default:
return map;
}
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Expand Down

0 comments on commit 21b0359

Please sign in to comment.