Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 17CF6200C7F for ; Wed, 24 May 2017 21:28:42 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 166A6160BD0; Wed, 24 May 2017 19:28:42 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5CCFD160BA5 for ; Wed, 24 May 2017 21:28:41 +0200 (CEST) Received: (qmail 48818 invoked by uid 500); 24 May 2017 19:28:40 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 48801 invoked by uid 99); 24 May 2017 19:28:39 -0000 Received: from Unknown (HELO svn01-us-west.apache.org) (209.188.14.144) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 May 2017 19:28:39 +0000 Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id C77533A039E for ; Wed, 24 May 2017 19:28:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1796089 - in /geronimo/specs/trunk/geronimo-jcdi_2.0_spec: ./ src/main/java/javax/enterprise/util/AnnotationLiteral.java Date: Wed, 24 May 2017 19:28:38 -0000 To: scm@geronimo.apache.org From: struberg@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20170524192838.C77533A039E@svn01-us-west.apache.org> archived-at: Wed, 24 May 2017 19:28:42 -0000 Author: struberg Date: Wed May 24 19:28:38 2017 New Revision: 1796089 URL: http://svn.apache.org/viewvc?rev=1796089&view=rev Log: GERONIMO-6556 improve AnnotationLiteral performance by caching toString, hashCode etc merged over from jcdi_1.1 Modified: geronimo/specs/trunk/geronimo-jcdi_2.0_spec/ (props changed) geronimo/specs/trunk/geronimo-jcdi_2.0_spec/src/main/java/javax/enterprise/util/AnnotationLiteral.java Propchange: geronimo/specs/trunk/geronimo-jcdi_2.0_spec/ ------------------------------------------------------------------------------ svn:mergeinfo = /geronimo/specs/trunk/geronimo-jcdi_1.1_spec:1765446 Modified: geronimo/specs/trunk/geronimo-jcdi_2.0_spec/src/main/java/javax/enterprise/util/AnnotationLiteral.java URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-jcdi_2.0_spec/src/main/java/javax/enterprise/util/AnnotationLiteral.java?rev=1796089&r1=1796088&r2=1796089&view=diff ============================================================================== --- geronimo/specs/trunk/geronimo-jcdi_2.0_spec/src/main/java/javax/enterprise/util/AnnotationLiteral.java (original) +++ geronimo/specs/trunk/geronimo-jcdi_2.0_spec/src/main/java/javax/enterprise/util/AnnotationLiteral.java Wed May 24 19:28:38 2017 @@ -40,6 +40,11 @@ public abstract class AnnotationLiteral< private static final long serialVersionUID = -1885320698638161810L; private Class annotationType; + + // cached values + private transient Method[] _meths = null; + private transient String _toString = null; + private transient Integer _hashCode = null; private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; @@ -100,13 +105,9 @@ public abstract class AnnotationLiteral< @Override public boolean equals(Object other) { - Method[] methods = (Method[])AccessController.doPrivileged(new PrivilegedAction() { - public Object run() - { - return annotationType.getDeclaredMethods(); - } - }); - + // use a private reference to prevent instance swapping as we don't use synchronization nor volatile on meths. + Method[] methods = getMethods(); + if(other == this) { return true; @@ -239,12 +240,10 @@ public abstract class AnnotationLiteral< @Override public int hashCode() { - Method[] methods = (Method[])AccessController.doPrivileged(new PrivilegedAction() { - public Object run() - { - return annotationType.getDeclaredMethods(); - } - }); + if (_hashCode != null) { + return _hashCode.intValue(); + } + Method[] methods = getMethods(); int hashCode = 0; for (Method method : methods) @@ -305,6 +304,7 @@ public abstract class AnnotationLiteral< hashCode += name ^ value; } + _hashCode = Integer.valueOf(hashCode); return hashCode; } @@ -312,12 +312,11 @@ public abstract class AnnotationLiteral< @Override public String toString() { - Method[] methods = (Method[])AccessController.doPrivileged(new PrivilegedAction() { - public Object run() - { - return annotationType.getDeclaredMethods(); - } - }); + if (_toString != null) { + return _toString; + } + + Method[] methods = getMethods(); StringBuilder sb = new StringBuilder("@" + annotationType().getName() + "("); int lenght = methods.length; @@ -337,7 +336,21 @@ public abstract class AnnotationLiteral< sb.append(")"); - return sb.toString(); + _toString = sb.toString(); + return _toString; + } + + private Method[] getMethods() { + if (_meths == null) { + // no need to have meths volatile nor synchronized. + // if invoked in parallel we only might call getMethods() a bit too often. + _meths = (Method[]) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return annotationType.getDeclaredMethods(); + } + }); + } + return _meths; } protected static class PrivilegedActionForAccessibleObject implements PrivilegedAction