Return-Path: Delivered-To: apmail-db-derby-dev-archive@www.apache.org Received: (qmail 45642 invoked from network); 7 Feb 2006 02:28:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 7 Feb 2006 02:28:14 -0000 Received: (qmail 74870 invoked by uid 500); 7 Feb 2006 02:28:13 -0000 Delivered-To: apmail-db-derby-dev-archive@db.apache.org Received: (qmail 74656 invoked by uid 500); 7 Feb 2006 02:28:12 -0000 Mailing-List: contact derby-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: Delivered-To: mailing list derby-dev@db.apache.org Received: (qmail 74647 invoked by uid 99); 7 Feb 2006 02:28:11 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 Feb 2006 18:28:11 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [210.245.32.200] (HELO localhost.localdomain) (210.245.32.200) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 06 Feb 2006 18:28:10 -0800 Received: from [172.16.1.32] (helo=[172.16.1.32]) by localhost.localdomain with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.50) id 1F6Ia1-0004OZ-Qh for derby-dev@db.apache.org; Tue, 07 Feb 2006 09:27:46 +0700 Message-ID: <43E805BC.2090909@it.fts-vn.com> Date: Tue, 07 Feb 2006 09:28:12 +0700 From: Kev Jackson User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: derby-dev@db.apache.org Subject: [patch] cleanup of org.apache.derby.iapi.services.cache.ClassSize References: <43E721C5.2050305@it.fts-vn.com> <43E7764D.1080605@sun.com> In-Reply-To: <43E7764D.1080605@sun.com> Content-Type: multipart/mixed; boundary="------------000005070102090908070006" X-Spam-Score: -2.8 {--} X-Spam-Report: Spam detection software, running on the system "mail.fts-vn.com", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Following on from yesterdays post, here's a patch that tries to get this class to conform to the coding standards chosen by the Derby developers - remove unused import - change static final variables from camelCase to CONST_NAMES (except where they are public and could be used elsewhere BWC concern) - add {} for conditionals - strip lots of extra white space [...] Content analysis details: (-2.8 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.8 ALL_TRUSTED Did not pass through any untrusted hosts X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. --------------000005070102090908070006 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Following on from yesterdays post, here's a patch that tries to get this class to conform to the coding standards chosen by the Derby developers - remove unused import - change static final variables from camelCase to CONST_NAMES (except where they are public and could be used elsewhere BWC concern) - add {} for conditionals - strip lots of extra white space As for the CLA/ICLA, I've already signed one (for ant), so do I need to sign another for Derby? Thanks Kev --------------000005070102090908070006 Content-Type: text/plain; name="ClassSize_cleanup.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ClassSize_cleanup.patch" Index: D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java =================================================================== --- D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java (revision 375440) +++ D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java (working copy) @@ -2,7 +2,7 @@ Derby - Class org.apache.derby.iapi.services.cache.ClassSize - Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable. + Copyright 2002, 2006 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -25,21 +25,23 @@ import java.lang.Class; import java.lang.reflect.Field; import java.lang.Runtime; -import java.lang.InterruptedException; import java.lang.reflect.Modifier; -public class ClassSize -{ +public class ClassSize { + + //cannot change this due to BC concerns as it's declared public public static final int refSize; - private static final int objectOverhead = 2; // references, not bytes! - private static final int booleanSize = 4; - private static final int charSize = 4; // Unicode - private static final int shortSize = 4; - private static final int intSize = 4; - private static final int longSize = 8; - private static final int floatSize = 4; - private static final int doubleSize = 8; - private static final int minObjectSize; + + // these have been changed to reflect the common standard for naming static final values + private static final int OBJECT_OVERHEAD = 2; // references, not bytes! + private static final int BOOLEAN_SIZE = 4; + private static final int CHAR_SIZE = 4; // Unicode + private static final int SHORT_SIZE = 4; + private static final int INT_SIZE = 4; + private static final int LONG_SIZE = 8; + private static final int FLOAT_SIZE = 4; + private static final int DOUBLE_SIZE = 8; + private static final int MIN_OBJECT_SIZE; private static boolean dummyCatalog = false; // Used when constructing the catalog to prevent recursion @@ -49,7 +51,7 @@ static boolean unitTest = false; // unitTest is used in unit testing - private static final int[] wildGuess = {0,16}; + private static final int[] WILD_GUESS = {0,16}; /* The standard wild guess of the size of an unknown class, the size of 16 references. * Used when the security manager will not let us look at the class fields. */ @@ -59,14 +61,14 @@ * until everything else has been compiled. Bury ClassSizeCatalog in a string. */ private static java.util.Hashtable catalog; - static - { - try - { + + static { + try { catalog = (java.util.Hashtable) - Class.forName( "org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance(); + Class.forName("org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance(); + } catch(Exception e){ + //why is this caught and then not either rethrown or logged? } - catch( Exception e){}; // Figure out whether this is a 32 or 64 bit machine. Runtime runtime = Runtime.getRuntime(); @@ -74,33 +76,31 @@ Object[] junk = new Object[10000]; long memUsed = runtime.totalMemory() - runtime.freeMemory() - memBase; int sz = (int)((memUsed + junk.length/2)/junk.length); - refSize = ( 4 > sz) ? 4 : sz; - minObjectSize = 4*refSize; + refSize = (4 > sz) ? 4 : sz; + MIN_OBJECT_SIZE = 4*refSize; } /** * do not try to use the catalog. */ - public static void setDummyCatalog() - { + public static void setDummyCatalog() { dummyCatalog = true; } + /** * Get the estimate of the size of an object reference. * * @return the estimate in bytes. */ - public static int getRefSize() - { + public static int getRefSize() { return refSize; } /** * @return the estimate of the size of a primitive int */ - public static int getIntSize() - { - return intSize; + public static int getIntSize() { + return INT_SIZE; } /** @@ -117,46 +117,38 @@ * @return an array of 2 integers. The first integer is the constant part of the function, * the second is the reference size coefficient. */ - public static int[] getSizeCoefficients( Class cl) - { - int[] coeff = {0, objectOverhead}; - - + public static int[] getSizeCoefficients(Class cl) { + int[] coeff = {0, OBJECT_OVERHEAD}; - for( ; null != cl; cl = cl.getSuperclass()) - { - Field[] field = cl.getDeclaredFields(); - if( null != field) - { - for( int i = 0; i < field.length; i++) - { - if( ! Modifier.isStatic( field[i].getModifiers())) - { - Class fieldClass = field[i].getType(); - if( fieldClass.isArray() || ! fieldClass.isPrimitive()) + for(; null != cl; cl = cl.getSuperclass()) { + Field[] fields = cl.getDeclaredFields(); + if(null != fields) { + for(int i = 0; i < fields.length; i++) { + if(!Modifier.isStatic(fields[i].getModifiers())) { + Class fieldClass = fields[i].getType(); + if(fieldClass.isArray() || !fieldClass.isPrimitive()) { coeff[1]++; - else // Is simple primitive - { + } else { //Is simple primitive String name = fieldClass.getName(); - - if( name.equals( "int") || name.equals( "I")) - coeff[0] += intSize; - else if( name.equals( "long") || name.equals( "J")) - coeff[0] += longSize; - else if( name.equals( "boolean") || name.equals( "Z")) - coeff[0] += booleanSize; - else if( name.equals( "short") || name.equals( "S")) - coeff[0] += shortSize; - else if( name.equals( "byte") || name.equals( "B")) + if(name.equals("int") || name.equals("I")) { + coeff[0] += INT_SIZE; + } else if(name.equals("long") || name.equals("J")) { + coeff[0] += LONG_SIZE; + } else if(name.equals("boolean") || name.equals("Z")) { + coeff[0] += BOOLEAN_SIZE; + } else if(name.equals("short") || name.equals("S")) { + coeff[0] += SHORT_SIZE; + } else if(name.equals("byte") || name.equals("B")) { coeff[0] += 1; - else if( name.equals( "char") || name.equals( "C")) - coeff[0] += charSize; - else if( name.equals( "float") || name.equals( "F")) - coeff[0] += floatSize; - else if( name.equals( "double") || name.equals( "D")) - coeff[0] += doubleSize; - else // What is this?? + } else if(name.equals("char") || name.equals("C")) { + coeff[0] += CHAR_SIZE; + } else if(name.equals("float") || name.equals("F")) { + coeff[0] += FLOAT_SIZE; + } else if(name.equals("double") || name.equals("D")) { + coeff[0] += DOUBLE_SIZE; + } else { // What is this?? coeff[1]++; // Make a guess: one reference (?) + } } } } @@ -173,13 +165,12 @@ * * @return the size estimate, in bytes */ - public static int estimateBaseFromCoefficients( int[] coeff) - { + public static int estimateBaseFromCoefficients(int[] coeff) { int size = coeff[0] + coeff[1]*refSize; // Round up to a multiple of 8 size = (size + 7)/8; size *= 8; - return (size < minObjectSize) ? minObjectSize : size; + return (size < MIN_OBJECT_SIZE) ? MIN_OBJECT_SIZE : size; } // end of estimateBaseFromCoefficients /** @@ -193,36 +184,33 @@ * @see #getSizeCoefficients * see org.apache.derbyBuild.ClassSizeCrawler */ - public static int estimateBaseFromCatalog( Class cls) - { + public static int estimateBaseFromCatalog(Class cls) { return estimateBaseFromCatalog( cls, false); } - private static int estimateBaseFromCatalog( Class cls, boolean addToCatalog) - { - if( dummyCatalog) + private static int estimateBaseFromCatalog( Class cls, boolean addToCatalog) { + if(dummyCatalog) { return 0; - - if( SanityManager.DEBUG) - SanityManager.ASSERT( catalog != null, "The class size catalog could not be initialized."); - - int[] coeff = (int[]) catalog.get( cls.getName()); - if( coeff == null) - { - try - { - coeff = getSizeCoefficients( cls); + } + if(SanityManager.DEBUG) { + SanityManager.ASSERT(catalog != null, "The class size catalog could not be initialized."); + } + int[] coeff = (int[]) catalog.get(cls.getName()); + if(null == coeff) { + try { + coeff = getSizeCoefficients(cls); } - catch( Throwable t) - { - if( noGuess) + catch(Throwable t) { + if(noGuess) { return -2; - coeff = wildGuess; + } + coeff = WILD_GUESS; } - if( addToCatalog) - catalog.put( cls.getName(), coeff); + if(addToCatalog) { + catalog.put(cls.getName(), coeff); + } } - return estimateBaseFromCoefficients( coeff); + return estimateBaseFromCoefficients(coeff); } // end of estimateBaseFromCatalog @@ -238,9 +226,8 @@ * @see #getSizeCoefficients * see org.apache.derbyBuild.ClassSizeCrawler */ - public static int estimateAndCatalogBase( Class cls) - { - return estimateBaseFromCatalog( cls, true); + public static int estimateAndCatalogBase(Class cls) { + return estimateBaseFromCatalog(cls, true); } // end of estimateAndCatalogBase /** @@ -257,18 +244,16 @@ * see org.apache.derbyBuild.ClassSizeCrawler * @see #estimateBaseFromCatalog */ - public static int estimateBase( Class cl) - { - return estimateBaseFromCoefficients( getSizeCoefficients( cl)); + public static int estimateBase(Class cl) { + return estimateBaseFromCoefficients(getSizeCoefficients(cl)); } // End of estimateBase /** * @return the estimated overhead of an array. The estimated size of an x[n] array is * estimateArrayOverhead() + n*sizeOf(x). */ - public static int estimateArrayOverhead() - { - return minObjectSize; + public static int estimateArrayOverhead() { + return MIN_OBJECT_SIZE; } /** @@ -277,9 +262,8 @@ * * @return the estimate, in bytes */ - public static int estimateHashEntrySize() - { - return objectOverhead + 3*refSize; + public static int estimateHashEntrySize() { + return OBJECT_OVERHEAD + 3*refSize; } /** @@ -287,11 +271,11 @@ * * @return the estimated size, in bytes */ - public static int estimateMemoryUsage( String str) - { - if( null == str) + public static int estimateMemoryUsage(String str) { + if(null == str) { return 0; + } // Since Java uses Unicode assume that each character takes 2 bytes return 2*str.length(); } -} +} \ No newline at end of file --------------000005070102090908070006--