src/share/classes/sun/rmi/rmic/iiop/StaticStringsHash.java

Mon, 26 Mar 2012 14:01:40 +0100

author
coffeys
date
Mon, 26 Mar 2012 14:01:40 +0100
changeset 370
5222b7d658d4
parent 158
91006f157c46
child 748
6845b95cba6b
permissions
-rw-r--r--

7143851: Improve IIOP stub and tie generation in RMIC
7149048: Changes to corba rmic stubGenerator class are not used during jdk build process
Reviewed-by: mschoene, robm

duke@1 1 /*
ohair@158 2 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@158 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@158 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@158 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@158 22 * or visit www.oracle.com if you need additional information or have any
ohair@158 23 * questions.
duke@1 24 */
duke@1 25 /*
duke@1 26 * Licensed Materials - Property of IBM
duke@1 27 * RMI-IIOP v1.0
duke@1 28 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 29 *
duke@1 30 */
duke@1 31
duke@1 32 package sun.rmi.rmic.iiop;
duke@1 33
duke@1 34 /**
duke@1 35 * StaticStringsHash takes an array of constant strings and
duke@1 36 * uses several different hash methods to try to find the
duke@1 37 * 'best' one for that set. The set of methods is currently
duke@1 38 * fixed, but with a little work could be made extensible thru
duke@1 39 * subclassing.
duke@1 40 * <p>
duke@1 41 * The current set of methods is:
duke@1 42 * <ol>
duke@1 43 * <li> length() - works well when all strings are different length.</li>
duke@1 44 * <li> charAt(n) - works well when one offset into all strings is different.</li>
duke@1 45 * <li> hashCode() - works well with larger arrays.</li>
duke@1 46 * </ol>
duke@1 47 * After constructing an instance over the set of strings, the
duke@1 48 * <code>getKey(String)</code> method can be used to use the selected hash
duke@1 49 * method to produce a key. The <code>method</code> string will contain
duke@1 50 * "length()", "charAt(n)", or "hashCode()", and is intended for use by
duke@1 51 * code generators.
duke@1 52 * <p>
duke@1 53 * The <code>keys</code> array will contain the full set of unique keys.
duke@1 54 * <p>
duke@1 55 * The <code>buckets</code> array will contain a set of arrays, one for
duke@1 56 * each key in the <code>keys</code>, where <code>buckets[x][y]</code>
duke@1 57 * is an index into the <code>strings</code> array.
duke@1 58 * @author Bryan Atsatt
duke@1 59 */
duke@1 60 public class StaticStringsHash {
duke@1 61
duke@1 62 /** The set of strings upon which the hash info is created */
duke@1 63 public String[] strings = null;
duke@1 64
duke@1 65 /** Unique hash keys */
duke@1 66 public int[] keys = null;
duke@1 67
duke@1 68 /** Buckets for each key, where buckets[x][y] is an index
duke@1 69 * into the strings[] array. */
duke@1 70 public int[][] buckets = null;
duke@1 71
duke@1 72 /** The method to invoke on String to produce the hash key */
duke@1 73 public String method = null;
duke@1 74
duke@1 75 /** Get a key for the given string using the
duke@1 76 * selected hash method.
duke@1 77 * @param str the string to return a key for.
duke@1 78 * @return the key.
duke@1 79 */
duke@1 80 public int getKey(String str) {
duke@1 81 switch (keyKind) {
duke@1 82 case LENGTH: return str.length();
duke@1 83 case CHAR_AT: return str.charAt(charAt);
duke@1 84 case HASH_CODE: return str.hashCode();
duke@1 85 }
duke@1 86 throw new Error("Bad keyKind");
duke@1 87 }
duke@1 88
duke@1 89 /** Constructor
duke@1 90 * @param strings the set of strings upon which to
duke@1 91 * find an optimal hash method. Must not contain
duke@1 92 * duplicates.
duke@1 93 */
duke@1 94 public StaticStringsHash(String[] strings) {
duke@1 95 this.strings = strings;
duke@1 96 length = strings.length;
duke@1 97 tempKeys = new int[length];
duke@1 98 bucketSizes = new int[length];
duke@1 99 setMinStringLength();
duke@1 100
duke@1 101 // Decide on the best algorithm based on
duke@1 102 // which one has the smallest maximum
duke@1 103 // bucket depth. First, try length()...
duke@1 104
duke@1 105 int currentMaxDepth = getKeys(LENGTH);
duke@1 106 int useCharAt = -1;
duke@1 107 boolean useHashCode = false;
duke@1 108
duke@1 109 if (currentMaxDepth > 1) {
duke@1 110
duke@1 111 // At least one bucket had more than one
duke@1 112 // entry, so try charAt(i). If there
duke@1 113 // are a lot of strings in the array,
duke@1 114 // and minStringLength is large, limit
duke@1 115 // the search to a smaller number of
duke@1 116 // characters to avoid spending a lot
duke@1 117 // of time here that is most likely to
duke@1 118 // be pointless...
duke@1 119
duke@1 120 int minLength = minStringLength;
duke@1 121 if (length > CHAR_AT_MAX_LINES &&
duke@1 122 length * minLength > CHAR_AT_MAX_CHARS) {
duke@1 123 minLength = length/CHAR_AT_MAX_CHARS;
duke@1 124 }
duke@1 125
duke@1 126 charAt = 0;
duke@1 127 for (int i = 0; i < minLength; i++) {
duke@1 128 int charAtDepth = getKeys(CHAR_AT);
duke@1 129 if (charAtDepth < currentMaxDepth) {
duke@1 130 currentMaxDepth = charAtDepth;
duke@1 131 useCharAt = i;
duke@1 132 if (currentMaxDepth == 1) {
duke@1 133 break;
duke@1 134 }
duke@1 135 }
duke@1 136 charAt++;
duke@1 137 }
duke@1 138 charAt = useCharAt;
duke@1 139
duke@1 140
duke@1 141 if (currentMaxDepth > 1) {
duke@1 142
duke@1 143 // At least one bucket had more than one
duke@1 144 // entry, try hashCode().
duke@1 145 //
duke@1 146 // Since the cost of computing a full hashCode
duke@1 147 // (for the runtime target string) is much higher
duke@1 148 // than the previous methods, use it only if it is
duke@1 149 // substantially better. The definition of 'substantial'
duke@1 150 // here is not very well founded, and could be improved
duke@1 151 // with some further analysis ;^)
duke@1 152
duke@1 153 int hashCodeDepth = getKeys(HASH_CODE);
duke@1 154 if (hashCodeDepth < currentMaxDepth-3) {
duke@1 155
duke@1 156 // Using the full hashCode results in at least
duke@1 157 // 3 fewer entries in the worst bucket, so will
duke@1 158 // therefore avoid at least 3 calls to equals()
duke@1 159 // in the worst case.
duke@1 160 //
duke@1 161 // Note that using a number smaller than 3 could
duke@1 162 // result in using a hashCode when there are only
duke@1 163 // 2 strings in the array, and that would surely
duke@1 164 // be a poor performance choice.
duke@1 165
duke@1 166 useHashCode = true;
duke@1 167 }
duke@1 168 }
duke@1 169
duke@1 170 // Reset keys if needed...
duke@1 171
duke@1 172 if (!useHashCode) {
duke@1 173 if (useCharAt >= 0) {
duke@1 174
duke@1 175 // Use the charAt(i) method...
duke@1 176
duke@1 177 getKeys(CHAR_AT);
duke@1 178
duke@1 179 } else {
duke@1 180
duke@1 181 // Use length method...
duke@1 182
duke@1 183 getKeys(LENGTH);
duke@1 184 }
duke@1 185 }
duke@1 186 }
duke@1 187
duke@1 188 // Now allocate and fill our real hashKeys array...
duke@1 189
duke@1 190 keys = new int[bucketCount];
duke@1 191 System.arraycopy(tempKeys,0,keys,0,bucketCount);
duke@1 192
duke@1 193 // Sort keys and bucketSizes arrays...
duke@1 194
duke@1 195 boolean didSwap;
duke@1 196 do {
duke@1 197 didSwap = false;
duke@1 198 for (int i = 0; i < bucketCount - 1; i++) {
duke@1 199 if (keys[i] > keys[i+1]) {
duke@1 200 int temp = keys[i];
duke@1 201 keys[i] = keys[i+1];
duke@1 202 keys[i+1] = temp;
duke@1 203 temp = bucketSizes[i];
duke@1 204 bucketSizes[i] = bucketSizes[i+1];
duke@1 205 bucketSizes[i+1] = temp;
duke@1 206 didSwap = true;
duke@1 207 }
duke@1 208 }
duke@1 209 }
duke@1 210 while (didSwap == true);
duke@1 211
duke@1 212 // Allocate our buckets array. Fill the string
duke@1 213 // index slot with an unused key so we can
duke@1 214 // determine which are free...
duke@1 215
duke@1 216 int unused = findUnusedKey();
duke@1 217 buckets = new int[bucketCount][];
duke@1 218 for (int i = 0; i < bucketCount; i++) {
duke@1 219 buckets[i] = new int[bucketSizes[i]];
duke@1 220 for (int j = 0; j < bucketSizes[i]; j++) {
duke@1 221 buckets[i][j] = unused;
duke@1 222 }
duke@1 223 }
duke@1 224
duke@1 225 // And fill it in...
duke@1 226
duke@1 227 for(int i = 0; i < strings.length; i++) {
duke@1 228 int key = getKey(strings[i]);
duke@1 229 for (int j = 0; j < bucketCount; j++) {
duke@1 230 if (keys[j] == key) {
duke@1 231 int k = 0;
duke@1 232 while (buckets[j][k] != unused) {
duke@1 233 k++;
duke@1 234 }
duke@1 235 buckets[j][k] = i;
duke@1 236 break;
duke@1 237 }
duke@1 238 }
duke@1 239 }
duke@1 240 }
duke@1 241
duke@1 242 /** Print an optimized 'contains' method for the
duke@1 243 * argument strings
duke@1 244 */
duke@1 245 public static void main (String[] args) {
duke@1 246 StaticStringsHash hash = new StaticStringsHash(args);
duke@1 247 System.out.println();
duke@1 248 System.out.println(" public boolean contains(String key) {");
duke@1 249 System.out.println(" switch (key."+hash.method+") {");
duke@1 250 for (int i = 0; i < hash.buckets.length; i++) {
duke@1 251 System.out.println(" case "+hash.keys[i]+": ");
duke@1 252 for (int j = 0; j < hash.buckets[i].length; j++) {
duke@1 253 if (j > 0) {
duke@1 254 System.out.print(" } else ");
duke@1 255 } else {
duke@1 256 System.out.print(" ");
duke@1 257 }
duke@1 258 System.out.println("if (key.equals(\""+ hash.strings[hash.buckets[i][j]] +"\")) {");
duke@1 259 System.out.println(" return true;");
duke@1 260 }
duke@1 261 System.out.println(" }");
duke@1 262 }
duke@1 263 System.out.println(" }");
duke@1 264 System.out.println(" return false;");
duke@1 265 System.out.println(" }");
duke@1 266 }
duke@1 267
duke@1 268 private int length;
duke@1 269 private int[] tempKeys;
duke@1 270 private int[] bucketSizes;
duke@1 271 private int bucketCount;
duke@1 272 private int maxDepth;
duke@1 273 private int minStringLength = Integer.MAX_VALUE;
duke@1 274 private int keyKind;
duke@1 275 private int charAt;
duke@1 276
duke@1 277 private static final int LENGTH = 0;
duke@1 278 private static final int CHAR_AT = 1;
duke@1 279 private static final int HASH_CODE = 2;
duke@1 280
duke@1 281 /* Determines the maximum number of charAt(i)
duke@1 282 * tests that will be done. The search is
duke@1 283 * limited because if the number of characters
duke@1 284 * is large enough, the likelyhood of finding
duke@1 285 * a good hash key based on this method is
duke@1 286 * low. The CHAR_AT_MAX_CHARS limit only
duke@1 287 * applies f there are more strings than
duke@1 288 * CHAR_AT_MAX_LINES.
duke@1 289 */
duke@1 290 private static final int CHAR_AT_MAX_LINES = 50;
duke@1 291 private static final int CHAR_AT_MAX_CHARS = 1000;
duke@1 292
duke@1 293 private void resetKeys(int keyKind) {
duke@1 294 this.keyKind = keyKind;
duke@1 295 switch (keyKind) {
duke@1 296 case LENGTH: method = "length()"; break;
duke@1 297 case CHAR_AT: method = "charAt("+charAt+")"; break;
duke@1 298 case HASH_CODE: method = "hashCode()"; break;
duke@1 299 }
duke@1 300 maxDepth = 1;
duke@1 301 bucketCount = 0;
duke@1 302 for (int i = 0; i < length; i++) {
duke@1 303 tempKeys[i] = 0;
duke@1 304 bucketSizes[i] = 0;
duke@1 305 }
duke@1 306 }
duke@1 307
duke@1 308 private void setMinStringLength() {
duke@1 309 for (int i = 0; i < length; i++) {
duke@1 310 if (strings[i].length() < minStringLength) {
duke@1 311 minStringLength = strings[i].length();
duke@1 312 }
duke@1 313 }
duke@1 314 }
duke@1 315
duke@1 316 private int findUnusedKey() {
duke@1 317 int unused = 0;
duke@1 318 int keysLength = keys.length;
duke@1 319
duke@1 320 // Note that we just assume that resource
duke@1 321 // exhaustion will occur rather than an
duke@1 322 // infinite loop here if the set of keys
duke@1 323 // is very large.
duke@1 324
duke@1 325 while (true) {
duke@1 326 boolean match = false;
duke@1 327 for (int i = 0; i < keysLength; i++) {
duke@1 328 if (keys[i] == unused) {
duke@1 329 match = true;
duke@1 330 break;
duke@1 331 }
duke@1 332 }
duke@1 333 if (match) {
duke@1 334 unused--;
duke@1 335 } else {
duke@1 336 break;
duke@1 337 }
duke@1 338 }
duke@1 339 return unused;
duke@1 340 }
duke@1 341
duke@1 342 private int getKeys(int methodKind) {
duke@1 343 resetKeys(methodKind);
duke@1 344 for(int i = 0; i < strings.length; i++) {
duke@1 345 addKey(getKey(strings[i]));
duke@1 346 }
duke@1 347 return maxDepth;
duke@1 348 }
duke@1 349
duke@1 350 private void addKey(int key) {
duke@1 351
duke@1 352 // Have we seen this one before?
duke@1 353
duke@1 354 boolean addIt = true;
duke@1 355 for (int j = 0; j < bucketCount; j++) {
duke@1 356 if (tempKeys[j] == key) {
duke@1 357 addIt = false;
duke@1 358 bucketSizes[j]++;
duke@1 359 if (bucketSizes[j] > maxDepth) {
duke@1 360 maxDepth = bucketSizes[j];
duke@1 361 }
duke@1 362 break;
duke@1 363 }
duke@1 364 }
duke@1 365
duke@1 366 if (addIt) {
duke@1 367 tempKeys[bucketCount] = key;
duke@1 368 bucketSizes[bucketCount] = 1;
duke@1 369 bucketCount++;
duke@1 370 }
duke@1 371 }
duke@1 372 }

mercurial