1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.invoke; 27 28 import jdk.internal.org.objectweb.asm.ClassWriter; 29 import jdk.internal.org.objectweb.asm.Label; 30 import jdk.internal.org.objectweb.asm.MethodVisitor; 31 import jdk.internal.org.objectweb.asm.Opcodes; 32 import jdk.internal.vm.annotation.ForceInline; 33 import jdk.internal.misc.Unsafe; 34 35 import java.lang.invoke.MethodHandles.Lookup; 36 import java.util.*; 37 import java.util.concurrent.ConcurrentHashMap; 38 import java.util.concurrent.ConcurrentMap; 39 import java.util.function.Function; 40 import sun.security.action.GetPropertyAction; 41 42 import static jdk.internal.org.objectweb.asm.Opcodes.*; 43 44 /** 45 * <p>Methods to facilitate the creation of String concatenation methods, that 46 * can be used to efficiently concatenate a known number of arguments of known 47 * types, possibly after type adaptation and partial evaluation of arguments. 48 * These methods are typically used as <em>bootstrap methods</em> for {@code 49 * invokedynamic} call sites, to support the <em>string concatenation</em> 50 * feature of the Java Programming Language. 51 * 52 * <p>Indirect access to the behavior specified by the provided {@code 53 * MethodHandle} proceeds in order through two phases: 54 * 55 * <ol> 56 * <li><em>Linkage</em> occurs when the methods in this class are invoked. 57 * They take as arguments a method type describing the concatenated arguments 58 * count and types, and optionally the String <em>recipe</em>, plus the 59 * constants that participate in the String concatenation. The details on 60 * accepted recipe shapes are described further below. Linkage may involve 61 * dynamically loading a new class that implements the expected concatenation 62 * behavior. The {@code CallSite} holds the {@code MethodHandle} pointing to the 63 * exact concatenation method. The concatenation methods may be shared among 64 * different {@code CallSite}s, e.g. if linkage methods produce them as pure 65 * functions.</li> 66 * 67 * <li><em>Invocation</em> occurs when a generated concatenation method is 68 * invoked with the exact dynamic arguments. This may occur many times for a 69 * single concatenation method. The method referenced by the behavior {@code 70 * MethodHandle} is invoked with the static arguments and any additional dynamic 71 * arguments provided on invocation, as if by {@link MethodHandle#invoke(Object...)}.</li> 72 * </ol> 73 * 74 * <p> This class provides two forms of linkage methods: a simple version 75 * ({@link #makeConcat(java.lang.invoke.MethodHandles.Lookup, String, 76 * MethodType)}) using only the dynamic arguments, and an advanced version 77 * ({@link #makeConcatWithConstants(java.lang.invoke.MethodHandles.Lookup, 78 * String, MethodType, String, Object...)} using the advanced forms of capturing 79 * the constant arguments. The advanced strategy can produce marginally better 80 * invocation bytecode, at the expense of exploding the number of shapes of 81 * string concatenation methods present at runtime, because those shapes would 82 * include constant static arguments as well. 83 * 84 * @author Aleksey Shipilev 85 * @author Remi Forax 86 * @author Peter Levart 87 * 88 * @apiNote 89 * <p>There is a JVM limit (classfile structural constraint): no method 90 * can call with more than 255 slots. This limits the number of static and 91 * dynamic arguments one can pass to bootstrap method. Since there are potential 92 * concatenation strategies that use {@code MethodHandle} combinators, we need 93 * to reserve a few empty slots on the parameter lists to capture the 94 * temporal results. This is why bootstrap methods in this factory do not accept 95 * more than 200 argument slots. Users requiring more than 200 argument slots in 96 * concatenation are expected to split the large concatenation in smaller 97 * expressions. 98 * 99 * @since 9 100 */ 101 public final class StringConcatFactory { 102 103 /** 104 * Tag used to demarcate an ordinary argument. 105 */ 106 private static final char TAG_ARG = '\u0001'; 107 108 /** 109 * Tag used to demarcate a constant. 110 */ 111 private static final char TAG_CONST = '\u0002'; 112 113 /** 114 * Maximum number of argument slots in String Concat call. 115 * 116 * While the maximum number of argument slots that indy call can handle is 253, 117 * we do not use all those slots, to let the strategies with MethodHandle 118 * combinators to use some arguments. 119 */ 120 private static final int MAX_INDY_CONCAT_ARG_SLOTS = 200; 121 122 /** 123 * Concatenation strategy to use. See {@link Strategy} for possible options. 124 * This option is controllable with -Djava.lang.invoke.stringConcat JDK option. 125 */ 126 private static Strategy STRATEGY; 127 128 /** 129 * Default strategy to use for concatenation. 130 */ 131 private static final Strategy DEFAULT_STRATEGY = Strategy.MH_INLINE_SIZED_EXACT; 132 133 private enum Strategy { 134 /** 135 * Bytecode generator, calling into {@link java.lang.StringBuilder}. 136 */ 137 BC_SB, 138 139 /** 140 * Bytecode generator, calling into {@link java.lang.StringBuilder}; 141 * but trying to estimate the required storage. 142 */ 143 BC_SB_SIZED, 144 145 /** 146 * Bytecode generator, calling into {@link java.lang.StringBuilder}; 147 * but computing the required storage exactly. 148 */ 149 BC_SB_SIZED_EXACT, 150 151 /** 152 * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}. 153 * This strategy also tries to estimate the required storage. 154 */ 155 MH_SB_SIZED, 156 157 /** 158 * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}. 159 * This strategy also estimate the required storage exactly. 160 */ 161 MH_SB_SIZED_EXACT, 162 163 /** 164 * MethodHandle-based generator, that constructs its own byte[] array from 165 * the arguments. It computes the required storage exactly. 166 */ 167 MH_INLINE_SIZED_EXACT 168 } 169 170 /** 171 * Enables debugging: this may print debugging messages, perform additional (non-neutral for performance) 172 * checks, etc. 173 */ 174 private static final boolean DEBUG; 175 176 /** 177 * Enables caching of strategy stubs. This may improve the linkage time by reusing the generated 178 * code, at the expense of contaminating the profiles. 179 */ 180 private static final boolean CACHE_ENABLE; 181 182 private static final ConcurrentMap<Key, MethodHandle> CACHE; 183 184 /** 185 * Dump generated classes to disk, for debugging purposes. 186 */ 187 private static final ProxyClassesDumper DUMPER; 188 189 static { 190 // In case we need to double-back onto the StringConcatFactory during this 191 // static initialization, make sure we have the reasonable defaults to complete 192 // the static initialization properly. After that, actual users would use the 193 // the proper values we have read from the the properties. 194 STRATEGY = DEFAULT_STRATEGY; 195 // CACHE_ENABLE = false; // implied 196 // CACHE = null; // implied 197 // DEBUG = false; // implied 198 // DUMPER = null; // implied 199 200 Properties props = GetPropertyAction.privilegedGetProperties(); 201 final String strategy = 202 props.getProperty("java.lang.invoke.stringConcat"); 203 CACHE_ENABLE = Boolean.parseBoolean( 204 props.getProperty("java.lang.invoke.stringConcat.cache")); 205 DEBUG = Boolean.parseBoolean( 206 props.getProperty("java.lang.invoke.stringConcat.debug")); 207 final String dumpPath = 208 props.getProperty("java.lang.invoke.stringConcat.dumpClasses"); 209 210 STRATEGY = (strategy == null) ? DEFAULT_STRATEGY : Strategy.valueOf(strategy); 211 CACHE = CACHE_ENABLE ? new ConcurrentHashMap<>() : null; 212 DUMPER = (dumpPath == null) ? null : ProxyClassesDumper.getInstance(dumpPath); 213 } 214 215 /** 216 * Cache key is a composite of: 217 * - class name, that lets to disambiguate stubs, to avoid excess sharing 218 * - method type, describing the dynamic arguments for concatenation 219 * - concat recipe, describing the constants and concat shape 220 */ 221 private static final class Key { 222 final String className; 223 final MethodType mt; 224 final Recipe recipe; 225 226 public Key(String className, MethodType mt, Recipe recipe) { 227 this.className = className; 228 this.mt = mt; 229 this.recipe = recipe; 230 } 231 232 @Override 233 public boolean equals(Object o) { 234 if (this == o) return true; 235 if (o == null || getClass() != o.getClass()) return false; 236 237 Key key = (Key) o; 238 239 if (!className.equals(key.className)) return false; 240 if (!mt.equals(key.mt)) return false; 241 if (!recipe.equals(key.recipe)) return false; 242 return true; 243 } 244 245 @Override 246 public int hashCode() { 247 int result = className.hashCode(); 248 result = 31 * result + mt.hashCode(); 249 result = 31 * result + recipe.hashCode(); 250 return result; 251 } 252 } 253 254 /** 255 * Parses the recipe string, and produces the traversable collection of 256 * {@link java.lang.invoke.StringConcatFactory.RecipeElement}-s for generator 257 * strategies. Notably, this class parses out the constants from the recipe 258 * and from other static arguments. 259 */ 260 private static final class Recipe { 261 private final List<RecipeElement> elements; 262 263 public Recipe(String src, Object[] constants) { 264 List<RecipeElement> el = new ArrayList<>(); 265 266 int constC = 0; 267 int argC = 0; 268 269 StringBuilder acc = new StringBuilder(); 270 271 for (int i = 0; i < src.length(); i++) { 272 char c = src.charAt(i); 273 274 if (c == TAG_CONST || c == TAG_ARG) { 275 // Detected a special tag, flush all accumulated characters 276 // as a constant first: 277 if (acc.length() > 0) { 278 el.add(new RecipeElement(acc.toString())); 279 acc.setLength(0); 280 } 281 if (c == TAG_CONST) { 282 Object cnst = constants[constC++]; 283 el.add(new RecipeElement(cnst)); 284 } else if (c == TAG_ARG) { 285 el.add(new RecipeElement(argC++)); 286 } 287 } else { 288 // Not a special character, this is a constant embedded into 289 // the recipe itself. 290 acc.append(c); 291 } 292 } 293 294 // Flush the remaining characters as constant: 295 if (acc.length() > 0) { 296 el.add(new RecipeElement(acc.toString())); 297 } 298 299 elements = el; 300 } 301 302 public List<RecipeElement> getElements() { 303 return elements; 304 } 305 306 @Override 307 public boolean equals(Object o) { 308 if (this == o) return true; 309 if (o == null || getClass() != o.getClass()) return false; 310 311 Recipe recipe = (Recipe) o; 312 return elements.equals(recipe.elements); 313 } 314 315 @Override 316 public int hashCode() { 317 return elements.hashCode(); 318 } 319 } 320 321 private static final class RecipeElement { 322 private final Object value; 323 private final int argPos; 324 private final char tag; 325 326 public RecipeElement(Object cnst) { 327 this.value = Objects.requireNonNull(cnst); 328 this.argPos = -1; 329 this.tag = TAG_CONST; 330 } 331 332 public RecipeElement(int arg) { 333 this.value = null; 334 this.argPos = arg; 335 this.tag = TAG_ARG; 336 } 337 338 public Object getValue() { 339 assert (tag == TAG_CONST); 340 return value; 341 } 342 343 public int getArgPos() { 344 assert (tag == TAG_ARG); 345 return argPos; 346 } 347 348 public char getTag() { 349 return tag; 350 } 351 352 @Override 353 public boolean equals(Object o) { 354 if (this == o) return true; 355 if (o == null || getClass() != o.getClass()) return false; 356 357 RecipeElement that = (RecipeElement) o; 358 359 if (this.tag != that.tag) return false; 360 if (this.tag == TAG_CONST && (!value.equals(that.value))) return false; 361 if (this.tag == TAG_ARG && (argPos != that.argPos)) return false; 362 return true; 363 } 364 365 @Override 366 public int hashCode() { 367 return (int)tag; 368 } 369 } 370 371 /** 372 * Facilitates the creation of optimized String concatenation methods, that 373 * can be used to efficiently concatenate a known number of arguments of 374 * known types, possibly after type adaptation and partial evaluation of 375 * arguments. Typically used as a <em>bootstrap method</em> for {@code 376 * invokedynamic} call sites, to support the <em>string concatenation</em> 377 * feature of the Java Programming Language. 378 * 379 * <p>When the target of the {@code CallSite} returned from this method is 380 * invoked, it returns the result of String concatenation, taking all 381 * function arguments passed to the linkage method as inputs for 382 * concatenation. The target signature is given by {@code concatType}. 383 * The arguments are concatenated as per requirements stated in JLS 15.18.1 384 * "String Concatenation Operator +". Notably, the inputs are converted as 385 * per JLS 5.1.11 "String Conversion", and combined from left to right. 386 * 387 * <p>Assume the linkage arguments are as follows: 388 * 389 * <ul> 390 * <li>{@code concatType}, describing the {@code CallSite} signature</li> 391 * </ul> 392 * 393 * <p>Then the following linkage invariants must hold: 394 * 395 * <ul> 396 * <li>The parameter count in {@code concatType} is less than or equal to 200</li> 397 * 398 * <li>The return type in {@code concatType} is assignable from {@link java.lang.String}</li> 399 * </ul> 400 * 401 * @param lookup Represents a lookup context with the accessibility 402 * privileges of the caller. When used with {@code 403 * invokedynamic}, this is stacked automatically by the VM. 404 * @param name The name of the method to implement. This name is 405 * arbitrary, and has no meaning for this linkage method. 406 * When used with {@code invokedynamic}, this is provided by 407 * the {@code NameAndType} of the {@code InvokeDynamic} 408 * structure and is stacked automatically by the VM. 409 * @param concatType The expected signature of the {@code CallSite}. The 410 * parameter types represent the types of concatenation 411 * arguments; the return type is always assignable from {@link 412 * java.lang.String}. When used with {@code invokedynamic}, 413 * this is provided by the {@code NameAndType} of the {@code 414 * InvokeDynamic} structure and is stacked automatically by 415 * the VM. 416 * @return a CallSite whose target can be used to perform String 417 * concatenation, with dynamic concatenation arguments described by the given 418 * {@code concatType}. 419 * @throws StringConcatException If any of the linkage invariants described 420 * here are violated. 421 * @throws NullPointerException If any of the incoming arguments is null. 422 * This will never happen when a bootstrap method 423 * is called with invokedynamic. 424 * 425 * @jls 5.1.11 String Conversion 426 * @jls 15.18.1 String Concatenation Operator + 427 */ 428 public static CallSite makeConcat(MethodHandles.Lookup lookup, 429 String name, 430 MethodType concatType) throws StringConcatException { 431 if (DEBUG) { 432 System.out.println("StringConcatFactory " + STRATEGY + " is here for " + concatType); 433 } 434 435 return doStringConcat(lookup, name, concatType, true, null); 436 } 437 438 /** 439 * Facilitates the creation of optimized String concatenation methods, that 440 * can be used to efficiently concatenate a known number of arguments of 441 * known types, possibly after type adaptation and partial evaluation of 442 * arguments. Typically used as a <em>bootstrap method</em> for {@code 443 * invokedynamic} call sites, to support the <em>string concatenation</em> 444 * feature of the Java Programming Language. 445 * 446 * <p>When the target of the {@code CallSite} returned from this method is 447 * invoked, it returns the result of String concatenation, taking all 448 * function arguments and constants passed to the linkage method as inputs for 449 * concatenation. The target signature is given by {@code concatType}, and 450 * does not include constants. The arguments are concatenated as per requirements 451 * stated in JLS 15.18.1 "String Concatenation Operator +". Notably, the inputs 452 * are converted as per JLS 5.1.11 "String Conversion", and combined from left 453 * to right. 454 * 455 * <p>The concatenation <em>recipe</em> is a String description for the way to 456 * construct a concatenated String from the arguments and constants. The 457 * recipe is processed from left to right, and each character represents an 458 * input to concatenation. Recipe characters mean: 459 * 460 * <ul> 461 * 462 * <li><em>\1 (Unicode point 0001)</em>: an ordinary argument. This 463 * input is passed through dynamic argument, and is provided during the 464 * concatenation method invocation. This input can be null.</li> 465 * 466 * <li><em>\2 (Unicode point 0002):</em> a constant. This input passed 467 * through static bootstrap argument. This constant can be any value 468 * representable in constant pool. If necessary, the factory would call 469 * {@code toString} to perform a one-time String conversion.</li> 470 * 471 * <li><em>Any other char value:</em> a single character constant.</li> 472 * </ul> 473 * 474 * <p>Assume the linkage arguments are as follows: 475 * 476 * <ul> 477 * <li>{@code concatType}, describing the {@code CallSite} signature</li> 478 * <li>{@code recipe}, describing the String recipe</li> 479 * <li>{@code constants}, the vararg array of constants</li> 480 * </ul> 481 * 482 * <p>Then the following linkage invariants must hold: 483 * 484 * <ul> 485 * <li>The parameter count in {@code concatType} is less than or equal to 486 * 200</li> 487 * 488 * <li>The parameter count in {@code concatType} equals to number of \1 tags 489 * in {@code recipe}</li> 490 * 491 * <li>The return type in {@code concatType} is assignable 492 * from {@link java.lang.String}, and matches the return type of the 493 * returned {@link MethodHandle}</li> 494 * 495 * <li>The number of elements in {@code constants} equals to number of \2 496 * tags in {@code recipe}</li> 497 * </ul> 498 * 499 * @param lookup Represents a lookup context with the accessibility 500 * privileges of the caller. When used with {@code 501 * invokedynamic}, this is stacked automatically by the 502 * VM. 503 * @param name The name of the method to implement. This name is 504 * arbitrary, and has no meaning for this linkage method. 505 * When used with {@code invokedynamic}, this is provided 506 * by the {@code NameAndType} of the {@code InvokeDynamic} 507 * structure and is stacked automatically by the VM. 508 * @param concatType The expected signature of the {@code CallSite}. The 509 * parameter types represent the types of dynamic concatenation 510 * arguments; the return type is always assignable from {@link 511 * java.lang.String}. When used with {@code 512 * invokedynamic}, this is provided by the {@code 513 * NameAndType} of the {@code InvokeDynamic} structure and 514 * is stacked automatically by the VM. 515 * @param recipe Concatenation recipe, described above. 516 * @param constants A vararg parameter representing the constants passed to 517 * the linkage method. 518 * @return a CallSite whose target can be used to perform String 519 * concatenation, with dynamic concatenation arguments described by the given 520 * {@code concatType}. 521 * @throws StringConcatException If any of the linkage invariants described 522 * here are violated. 523 * @throws NullPointerException If any of the incoming arguments is null, or 524 * any constant in {@code recipe} is null. 525 * This will never happen when a bootstrap method 526 * is called with invokedynamic. 527 * @apiNote Code generators have three distinct ways to process a constant 528 * string operand S in a string concatenation expression. First, S can be 529 * materialized as a reference (using ldc) and passed as an ordinary argument 530 * (recipe '\1'). Or, S can be stored in the constant pool and passed as a 531 * constant (recipe '\2') . Finally, if S contains neither of the recipe 532 * tag characters ('\1', '\2') then S can be interpolated into the recipe 533 * itself, causing its characters to be inserted into the result. 534 * 535 * @jls 5.1.11 String Conversion 536 * @jls 15.18.1 String Concatenation Operator + 537 */ 538 public static CallSite makeConcatWithConstants(MethodHandles.Lookup lookup, 539 String name, 540 MethodType concatType, 541 String recipe, 542 Object... constants) throws StringConcatException { 543 if (DEBUG) { 544 System.out.println("StringConcatFactory " + STRATEGY + " is here for " + concatType + ", {" + recipe + "}, " + Arrays.toString(constants)); 545 } 546 547 return doStringConcat(lookup, name, concatType, false, recipe, constants); 548 } 549 550 private static CallSite doStringConcat(MethodHandles.Lookup lookup, 551 String name, 552 MethodType concatType, 553 boolean generateRecipe, 554 String recipe, 555 Object... constants) throws StringConcatException { 556 Objects.requireNonNull(lookup, "Lookup is null"); 557 Objects.requireNonNull(name, "Name is null"); 558 Objects.requireNonNull(concatType, "Concat type is null"); 559 Objects.requireNonNull(constants, "Constants are null"); 560 561 for (Object o : constants) { 562 Objects.requireNonNull(o, "Cannot accept null constants"); 563 } 564 565 if ((lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) { 566 throw new StringConcatException("Invalid caller: " + 567 lookup.lookupClass().getName()); 568 } 569 570 int cCount = 0; 571 int oCount = 0; 572 if (generateRecipe) { 573 // Mock the recipe to reuse the concat generator code 574 char[] value = new char[concatType.parameterCount()]; 575 Arrays.fill(value, TAG_ARG); 576 recipe = new String(value); 577 oCount = concatType.parameterCount(); 578 } else { 579 Objects.requireNonNull(recipe, "Recipe is null"); 580 581 for (int i = 0; i < recipe.length(); i++) { 582 char c = recipe.charAt(i); 583 if (c == TAG_CONST) cCount++; 584 if (c == TAG_ARG) oCount++; 585 } 586 } 587 588 if (oCount != concatType.parameterCount()) { 589 throw new StringConcatException( 590 "Mismatched number of concat arguments: recipe wants " + 591 oCount + 592 " arguments, but signature provides " + 593 concatType.parameterCount()); 594 } 595 596 if (cCount != constants.length) { 597 throw new StringConcatException( 598 "Mismatched number of concat constants: recipe wants " + 599 cCount + 600 " constants, but only " + 601 constants.length + 602 " are passed"); 603 } 604 605 if (!concatType.returnType().isAssignableFrom(String.class)) { 606 throw new StringConcatException( 607 "The return type should be compatible with String, but it is " + 608 concatType.returnType()); 609 } 610 611 if (concatType.parameterCount() > MAX_INDY_CONCAT_ARG_SLOTS) { 612 throw new StringConcatException("Too many concat argument slots: " + 613 concatType.parameterCount() + 614 ", can only accept " + 615 MAX_INDY_CONCAT_ARG_SLOTS); 616 } 617 618 String className = getClassName(lookup.lookupClass()); 619 MethodType mt = adaptType(concatType); 620 Recipe rec = new Recipe(recipe, constants); 621 622 MethodHandle mh; 623 if (CACHE_ENABLE) { 624 Key key = new Key(className, mt, rec); 625 mh = CACHE.get(key); 626 if (mh == null) { 627 mh = generate(lookup, className, mt, rec); 628 CACHE.put(key, mh); 629 } 630 } else { 631 mh = generate(lookup, className, mt, rec); 632 } 633 return new ConstantCallSite(mh.asType(concatType)); 634 } 635 636 /** 637 * Adapt method type to an API we are going to use. 638 * 639 * This strips the concrete classes from the signatures, thus preventing 640 * class leakage when we cache the concatenation stubs. 641 * 642 * @param args actual argument types 643 * @return argument types the strategy is going to use 644 */ 645 private static MethodType adaptType(MethodType args) { 646 Class<?>[] ptypes = null; 647 for (int i = 0; i < args.parameterCount(); i++) { 648 Class<?> ptype = args.parameterType(i); 649 if (!ptype.isPrimitive() && 650 ptype != String.class && 651 ptype != Object.class) { // truncate to Object 652 if (ptypes == null) { 653 ptypes = args.parameterArray(); 654 } 655 ptypes[i] = Object.class; 656 } 657 // else other primitives or String or Object (unchanged) 658 } 659 return (ptypes != null) 660 ? MethodType.methodType(args.returnType(), ptypes) 661 : args; 662 } 663 664 private static String getClassName(Class<?> hostClass) throws StringConcatException { 665 /* 666 When cache is enabled, we want to cache as much as we can. 667 668 However, there are two peculiarities: 669 670 a) The generated class should stay within the same package as the 671 host class, to allow Unsafe.defineAnonymousClass access controls 672 to work properly. JDK may choose to fail with IllegalAccessException 673 when accessing a VM anonymous class with non-privileged callers, 674 see JDK-8058575. 675 676 b) If we mark the stub with some prefix, say, derived from the package 677 name because of (a), we can technically use that stub in other packages. 678 But the call stack traces would be extremely puzzling to unsuspecting users 679 and profiling tools: whatever stub wins the race, would be linked in all 680 similar callsites. 681 682 Therefore, we set the class prefix to match the host class package, and use 683 the prefix as the cache key too. This only affects BC_* strategies, and only when 684 cache is enabled. 685 */ 686 687 switch (STRATEGY) { 688 case BC_SB: 689 case BC_SB_SIZED: 690 case BC_SB_SIZED_EXACT: { 691 if (CACHE_ENABLE) { 692 String pkgName = hostClass.getPackageName(); 693 return (pkgName != null && !pkgName.isEmpty() ? pkgName.replace('.', '/') + "/" : "") + "Stubs$$StringConcat"; 694 } else { 695 return hostClass.getName().replace('.', '/') + "$$StringConcat"; 696 } 697 } 698 case MH_SB_SIZED: 699 case MH_SB_SIZED_EXACT: 700 case MH_INLINE_SIZED_EXACT: 701 // MethodHandle strategies do not need a class name. 702 return ""; 703 default: 704 throw new StringConcatException("Concatenation strategy " + STRATEGY + " is not implemented"); 705 } 706 } 707 708 private static MethodHandle generate(Lookup lookup, String className, MethodType mt, Recipe recipe) throws StringConcatException { 709 try { 710 switch (STRATEGY) { 711 case BC_SB: 712 return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.DEFAULT); 713 case BC_SB_SIZED: 714 return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.SIZED); 715 case BC_SB_SIZED_EXACT: 716 return BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, Mode.SIZED_EXACT); 717 case MH_SB_SIZED: 718 return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED); 719 case MH_SB_SIZED_EXACT: 720 return MethodHandleStringBuilderStrategy.generate(mt, recipe, Mode.SIZED_EXACT); 721 case MH_INLINE_SIZED_EXACT: 722 return MethodHandleInlineCopyStrategy.generate(mt, recipe); 723 default: 724 throw new StringConcatException("Concatenation strategy " + STRATEGY + " is not implemented"); 725 } 726 } catch (Error | StringConcatException e) { 727 // Pass through any error or existing StringConcatException 728 throw e; 729 } catch (Throwable t) { 730 throw new StringConcatException("Generator failed", t); 731 } 732 } 733 734 private enum Mode { 735 DEFAULT(false, false), 736 SIZED(true, false), 737 SIZED_EXACT(true, true); 738 739 private final boolean sized; 740 private final boolean exact; 741 742 Mode(boolean sized, boolean exact) { 743 this.sized = sized; 744 this.exact = exact; 745 } 746 747 boolean isSized() { 748 return sized; 749 } 750 751 boolean isExact() { 752 return exact; 753 } 754 } 755 756 /** 757 * Bytecode StringBuilder strategy. 758 * 759 * <p>This strategy operates in three modes, gated by {@link Mode}. 760 * 761 * <p><b>{@link Strategy#BC_SB}: "bytecode StringBuilder".</b> 762 * 763 * <p>This strategy spins up the bytecode that has the same StringBuilder 764 * chain javac would otherwise emit. This strategy uses only the public API, 765 * and comes as the baseline for the current JDK behavior. On other words, 766 * this strategy moves the javac generated bytecode to runtime. The 767 * generated bytecode is loaded via Unsafe.defineAnonymousClass, but with 768 * the caller class coming from the BSM -- in other words, the protection 769 * guarantees are inherited from the method where invokedynamic was 770 * originally called. This means, among other things, that the bytecode is 771 * verified for all non-JDK uses. 772 * 773 * <p><b>{@link Strategy#BC_SB_SIZED}: "bytecode StringBuilder, but 774 * sized".</b> 775 * 776 * <p>This strategy acts similarly to {@link Strategy#BC_SB}, but it also 777 * tries to guess the capacity required for StringBuilder to accept all 778 * arguments without resizing. This strategy only makes an educated guess: 779 * it only guesses the space required for known types (e.g. primitives and 780 * Strings), but does not otherwise convert arguments. Therefore, the 781 * capacity estimate may be wrong, and StringBuilder may have to 782 * transparently resize or trim when doing the actual concatenation. While 783 * this does not constitute a correctness issue (in the end, that what BC_SB 784 * has to do anyway), this does pose a potential performance problem. 785 * 786 * <p><b>{@link Strategy#BC_SB_SIZED_EXACT}: "bytecode StringBuilder, but 787 * sized exactly".</b> 788 * 789 * <p>This strategy improves on @link Strategy#BC_SB_SIZED}, by first 790 * converting all arguments to String in order to get the exact capacity 791 * StringBuilder should have. The conversion is done via the public 792 * String.valueOf and/or Object.toString methods, and does not touch any 793 * private String API. 794 */ 795 private static final class BytecodeStringBuilderStrategy { 796 static final Unsafe UNSAFE = Unsafe.getUnsafe(); 797 static final int CLASSFILE_VERSION = 52; 798 static final String METHOD_NAME = "concat"; 799 800 private BytecodeStringBuilderStrategy() { 801 // no instantiation 802 } 803 804 private static MethodHandle generate(Lookup lookup, String className, MethodType args, Recipe recipe, Mode mode) throws Exception { 805 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); 806 807 cw.visit(CLASSFILE_VERSION, 808 ACC_SUPER + ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC, 809 className, // Unsafe.defineAnonymousClass would append an unique ID 810 null, 811 "java/lang/Object", 812 null 813 ); 814 815 MethodVisitor mv = cw.visitMethod( 816 ACC_PUBLIC + ACC_STATIC + ACC_FINAL, 817 METHOD_NAME, 818 args.toMethodDescriptorString(), 819 null, 820 null); 821 822 mv.visitAnnotation("Ljdk/internal/vm/annotation/ForceInline;", true); 823 mv.visitCode(); 824 825 Class<?>[] arr = args.parameterArray(); 826 boolean[] guaranteedNonNull = new boolean[arr.length]; 827 828 if (mode.isExact()) { 829 /* 830 In exact mode, we need to convert all arguments to their String representations, 831 as this allows to compute their String sizes exactly. We cannot use private 832 methods for primitives in here, therefore we need to convert those as well. 833 834 We also record what arguments are guaranteed to be non-null as the result 835 of the conversion. String.valueOf does the null checks for us. The only 836 corner case to take care of is String.valueOf(Object) returning null itself. 837 838 Also, if any conversion happened, then the slot indices in the incoming 839 arguments are not equal to the final local maps. The only case this may break 840 is when converting 2-slot long/double argument to 1-slot String. Therefore, 841 we get away with tracking modified offset, since no conversion can overwrite 842 the upcoming the argument. 843 */ 844 845 int off = 0; 846 int modOff = 0; 847 for (int c = 0; c < arr.length; c++) { 848 Class<?> cl = arr[c]; 849 if (cl == String.class) { 850 if (off != modOff) { 851 mv.visitIntInsn(getLoadOpcode(cl), off); 852 mv.visitIntInsn(ASTORE, modOff); 853 } 854 } else { 855 mv.visitIntInsn(getLoadOpcode(cl), off); 856 mv.visitMethodInsn( 857 INVOKESTATIC, 858 "java/lang/String", 859 "valueOf", 860 getStringValueOfDesc(cl), 861 false 862 ); 863 mv.visitIntInsn(ASTORE, modOff); 864 arr[c] = String.class; 865 guaranteedNonNull[c] = cl.isPrimitive(); 866 } 867 off += getParameterSize(cl); 868 modOff += getParameterSize(String.class); 869 } 870 } 871 872 if (mode.isSized()) { 873 /* 874 When operating in sized mode (this includes exact mode), it makes sense to make 875 StringBuilder append chains look familiar to OptimizeStringConcat. For that, we 876 need to do null-checks early, not make the append chain shape simpler. 877 */ 878 879 int off = 0; 880 for (RecipeElement el : recipe.getElements()) { 881 switch (el.getTag()) { 882 case TAG_CONST: 883 // Guaranteed non-null, no null check required. 884 break; 885 case TAG_ARG: 886 // Null-checks are needed only for String arguments, and when a previous stage 887 // did not do implicit null-checks. If a String is null, we eagerly replace it 888 // with "null" constant. Note, we omit Objects here, because we don't call 889 // .length() on them down below. 890 int ac = el.getArgPos(); 891 Class<?> cl = arr[ac]; 892 if (cl == String.class && !guaranteedNonNull[ac]) { 893 Label l0 = new Label(); 894 mv.visitIntInsn(ALOAD, off); 895 mv.visitJumpInsn(IFNONNULL, l0); 896 mv.visitLdcInsn("null"); 897 mv.visitIntInsn(ASTORE, off); 898 mv.visitLabel(l0); 899 } 900 off += getParameterSize(cl); 901 break; 902 default: 903 throw new StringConcatException("Unhandled tag: " + el.getTag()); 904 } 905 } 906 } 907 908 // Prepare StringBuilder instance 909 mv.visitTypeInsn(NEW, "java/lang/StringBuilder"); 910 mv.visitInsn(DUP); 911 912 if (mode.isSized()) { 913 /* 914 Sized mode requires us to walk through the arguments, and estimate the final length. 915 In exact mode, this will operate on Strings only. This code would accumulate the 916 final length on stack. 917 */ 918 int len = 0; 919 int off = 0; 920 921 mv.visitInsn(ICONST_0); 922 923 for (RecipeElement el : recipe.getElements()) { 924 switch (el.getTag()) { 925 case TAG_CONST: 926 Object cnst = el.getValue(); 927 len += cnst.toString().length(); 928 break; 929 case TAG_ARG: 930 /* 931 If an argument is String, then we can call .length() on it. Sized/Exact modes have 932 converted arguments for us. If an argument is primitive, we can provide a guess 933 for its String representation size. 934 */ 935 Class<?> cl = arr[el.getArgPos()]; 936 if (cl == String.class) { 937 mv.visitIntInsn(ALOAD, off); 938 mv.visitMethodInsn( 939 INVOKEVIRTUAL, 940 "java/lang/String", 941 "length", 942 "()I", 943 false 944 ); 945 mv.visitInsn(IADD); 946 } else if (cl.isPrimitive()) { 947 len += estimateSize(cl); 948 } 949 off += getParameterSize(cl); 950 break; 951 default: 952 throw new StringConcatException("Unhandled tag: " + el.getTag()); 953 } 954 } 955 956 // Constants have non-zero length, mix in 957 if (len > 0) { 958 iconst(mv, len); 959 mv.visitInsn(IADD); 960 } 961 962 mv.visitMethodInsn( 963 INVOKESPECIAL, 964 "java/lang/StringBuilder", 965 "<init>", 966 "(I)V", 967 false 968 ); 969 } else { 970 mv.visitMethodInsn( 971 INVOKESPECIAL, 972 "java/lang/StringBuilder", 973 "<init>", 974 "()V", 975 false 976 ); 977 } 978 979 // At this point, we have a blank StringBuilder on stack, fill it in with .append calls. 980 { 981 int off = 0; 982 for (RecipeElement el : recipe.getElements()) { 983 String desc; 984 switch (el.getTag()) { 985 case TAG_CONST: 986 Object cnst = el.getValue(); 987 mv.visitLdcInsn(cnst); 988 desc = getSBAppendDesc(cnst.getClass()); 989 break; 990 case TAG_ARG: 991 Class<?> cl = arr[el.getArgPos()]; 992 mv.visitVarInsn(getLoadOpcode(cl), off); 993 off += getParameterSize(cl); 994 desc = getSBAppendDesc(cl); 995 break; 996 default: 997 throw new StringConcatException("Unhandled tag: " + el.getTag()); 998 } 999 1000 mv.visitMethodInsn( 1001 INVOKEVIRTUAL, 1002 "java/lang/StringBuilder", 1003 "append", 1004 desc, 1005 false 1006 ); 1007 } 1008 } 1009 1010 if (DEBUG && mode.isExact()) { 1011 /* 1012 Exactness checks compare the final StringBuilder.capacity() with a resulting 1013 String.length(). If these values disagree, that means StringBuilder had to perform 1014 storage trimming, which defeats the purpose of exact strategies. 1015 */ 1016 1017 /* 1018 The logic for this check is as follows: 1019 1020 Stack before: Op: 1021 (SB) dup, dup 1022 (SB, SB, SB) capacity() 1023 (int, SB, SB) swap 1024 (SB, int, SB) toString() 1025 (S, int, SB) length() 1026 (int, int, SB) if_icmpeq 1027 (SB) <end> 1028 1029 Note that it leaves the same StringBuilder on exit, like the one on enter. 1030 */ 1031 1032 mv.visitInsn(DUP); 1033 mv.visitInsn(DUP); 1034 1035 mv.visitMethodInsn( 1036 INVOKEVIRTUAL, 1037 "java/lang/StringBuilder", 1038 "capacity", 1039 "()I", 1040 false 1041 ); 1042 1043 mv.visitInsn(SWAP); 1044 1045 mv.visitMethodInsn( 1046 INVOKEVIRTUAL, 1047 "java/lang/StringBuilder", 1048 "toString", 1049 "()Ljava/lang/String;", 1050 false 1051 ); 1052 1053 mv.visitMethodInsn( 1054 INVOKEVIRTUAL, 1055 "java/lang/String", 1056 "length", 1057 "()I", 1058 false 1059 ); 1060 1061 Label l0 = new Label(); 1062 mv.visitJumpInsn(IF_ICMPEQ, l0); 1063 1064 mv.visitTypeInsn(NEW, "java/lang/AssertionError"); 1065 mv.visitInsn(DUP); 1066 mv.visitLdcInsn("Failed exactness check"); 1067 mv.visitMethodInsn(INVOKESPECIAL, 1068 "java/lang/AssertionError", 1069 "<init>", 1070 "(Ljava/lang/Object;)V", 1071 false); 1072 mv.visitInsn(ATHROW); 1073 1074 mv.visitLabel(l0); 1075 } 1076 1077 mv.visitMethodInsn( 1078 INVOKEVIRTUAL, 1079 "java/lang/StringBuilder", 1080 "toString", 1081 "()Ljava/lang/String;", 1082 false 1083 ); 1084 1085 mv.visitInsn(ARETURN); 1086 1087 mv.visitMaxs(-1, -1); 1088 mv.visitEnd(); 1089 cw.visitEnd(); 1090 1091 byte[] classBytes = cw.toByteArray(); 1092 try { 1093 Class<?> hostClass = lookup.lookupClass(); 1094 Class<?> innerClass = UNSAFE.defineAnonymousClass(hostClass, classBytes, null); 1095 UNSAFE.ensureClassInitialized(innerClass); 1096 dumpIfEnabled(innerClass.getName(), classBytes); 1097 return Lookup.IMPL_LOOKUP.findStatic(innerClass, METHOD_NAME, args); 1098 } catch (Exception e) { 1099 dumpIfEnabled(className + "$$FAILED", classBytes); 1100 throw new StringConcatException("Exception while spinning the class", e); 1101 } 1102 } 1103 1104 private static void dumpIfEnabled(String name, byte[] bytes) { 1105 if (DUMPER != null) { 1106 DUMPER.dumpClass(name, bytes); 1107 } 1108 } 1109 1110 private static String getSBAppendDesc(Class<?> cl) { 1111 if (cl.isPrimitive()) { 1112 if (cl == Integer.TYPE || cl == Byte.TYPE || cl == Short.TYPE) { 1113 return "(I)Ljava/lang/StringBuilder;"; 1114 } else if (cl == Boolean.TYPE) { 1115 return "(Z)Ljava/lang/StringBuilder;"; 1116 } else if (cl == Character.TYPE) { 1117 return "(C)Ljava/lang/StringBuilder;"; 1118 } else if (cl == Double.TYPE) { 1119 return "(D)Ljava/lang/StringBuilder;"; 1120 } else if (cl == Float.TYPE) { 1121 return "(F)Ljava/lang/StringBuilder;"; 1122 } else if (cl == Long.TYPE) { 1123 return "(J)Ljava/lang/StringBuilder;"; 1124 } else { 1125 throw new IllegalStateException("Unhandled primitive StringBuilder.append: " + cl); 1126 } 1127 } else if (cl == String.class) { 1128 return "(Ljava/lang/String;)Ljava/lang/StringBuilder;"; 1129 } else { 1130 return "(Ljava/lang/Object;)Ljava/lang/StringBuilder;"; 1131 } 1132 } 1133 1134 private static String getStringValueOfDesc(Class<?> cl) { 1135 if (cl.isPrimitive()) { 1136 if (cl == Integer.TYPE || cl == Byte.TYPE || cl == Short.TYPE) { 1137 return "(I)Ljava/lang/String;"; 1138 } else if (cl == Boolean.TYPE) { 1139 return "(Z)Ljava/lang/String;"; 1140 } else if (cl == Character.TYPE) { 1141 return "(C)Ljava/lang/String;"; 1142 } else if (cl == Double.TYPE) { 1143 return "(D)Ljava/lang/String;"; 1144 } else if (cl == Float.TYPE) { 1145 return "(F)Ljava/lang/String;"; 1146 } else if (cl == Long.TYPE) { 1147 return "(J)Ljava/lang/String;"; 1148 } else { 1149 throw new IllegalStateException("Unhandled String.valueOf: " + cl); 1150 } 1151 } else if (cl == String.class) { 1152 return "(Ljava/lang/String;)Ljava/lang/String;"; 1153 } else { 1154 return "(Ljava/lang/Object;)Ljava/lang/String;"; 1155 } 1156 } 1157 1158 /** 1159 * The following method is copied from 1160 * org.objectweb.asm.commons.InstructionAdapter. Part of ASM: a very small 1161 * and fast Java bytecode manipulation framework. 1162 * Copyright (c) 2000-2005 INRIA, France Telecom All rights reserved. 1163 */ 1164 private static void iconst(MethodVisitor mv, final int cst) { 1165 if (cst >= -1 && cst <= 5) { 1166 mv.visitInsn(Opcodes.ICONST_0 + cst); 1167 } else if (cst >= Byte.MIN_VALUE && cst <= Byte.MAX_VALUE) { 1168 mv.visitIntInsn(Opcodes.BIPUSH, cst); 1169 } else if (cst >= Short.MIN_VALUE && cst <= Short.MAX_VALUE) { 1170 mv.visitIntInsn(Opcodes.SIPUSH, cst); 1171 } else { 1172 mv.visitLdcInsn(cst); 1173 } 1174 } 1175 1176 private static int getLoadOpcode(Class<?> c) { 1177 if (c == Void.TYPE) { 1178 throw new InternalError("Unexpected void type of load opcode"); 1179 } 1180 return ILOAD + getOpcodeOffset(c); 1181 } 1182 1183 private static int getOpcodeOffset(Class<?> c) { 1184 if (c.isPrimitive()) { 1185 if (c == Long.TYPE) { 1186 return 1; 1187 } else if (c == Float.TYPE) { 1188 return 2; 1189 } else if (c == Double.TYPE) { 1190 return 3; 1191 } 1192 return 0; 1193 } else { 1194 return 4; 1195 } 1196 } 1197 1198 private static int getParameterSize(Class<?> c) { 1199 if (c == Void.TYPE) { 1200 return 0; 1201 } else if (c == Long.TYPE || c == Double.TYPE) { 1202 return 2; 1203 } 1204 return 1; 1205 } 1206 } 1207 1208 /** 1209 * MethodHandle StringBuilder strategy. 1210 * 1211 * <p>This strategy operates in two modes, gated by {@link Mode}. 1212 * 1213 * <p><b>{@link Strategy#MH_SB_SIZED}: "MethodHandles StringBuilder, 1214 * sized".</b> 1215 * 1216 * <p>This strategy avoids spinning up the bytecode by building the 1217 * computation on MethodHandle combinators. The computation is built with 1218 * public MethodHandle APIs, resolved from a public Lookup sequence, and 1219 * ends up calling the public StringBuilder API. Therefore, this strategy 1220 * does not use any private API at all, even the Unsafe.defineAnonymousClass, 1221 * since everything is handled under cover by java.lang.invoke APIs. 1222 * 1223 * <p><b>{@link Strategy#MH_SB_SIZED_EXACT}: "MethodHandles StringBuilder, 1224 * sized exactly".</b> 1225 * 1226 * <p>This strategy improves on @link Strategy#MH_SB_SIZED}, by first 1227 * converting all arguments to String in order to get the exact capacity 1228 * StringBuilder should have. The conversion is done via the public 1229 * String.valueOf and/or Object.toString methods, and does not touch any 1230 * private String API. 1231 */ 1232 private static final class MethodHandleStringBuilderStrategy { 1233 1234 private MethodHandleStringBuilderStrategy() { 1235 // no instantiation 1236 } 1237 1238 private static MethodHandle generate(MethodType mt, Recipe recipe, Mode mode) throws Exception { 1239 int pc = mt.parameterCount(); 1240 1241 Class<?>[] ptypes = mt.parameterArray(); 1242 MethodHandle[] filters = new MethodHandle[ptypes.length]; 1243 for (int i = 0; i < ptypes.length; i++) { 1244 MethodHandle filter; 1245 switch (mode) { 1246 case SIZED: 1247 // In sized mode, we convert all references and floats/doubles 1248 // to String: there is no specialization for different 1249 // classes in StringBuilder API, and it will convert to 1250 // String internally anyhow. 1251 filter = Stringifiers.forMost(ptypes[i]); 1252 break; 1253 case SIZED_EXACT: 1254 // In exact mode, we convert everything to String: 1255 // this helps to compute the storage exactly. 1256 filter = Stringifiers.forAny(ptypes[i]); 1257 break; 1258 default: 1259 throw new StringConcatException("Not supported"); 1260 } 1261 if (filter != null) { 1262 filters[i] = filter; 1263 ptypes[i] = filter.type().returnType(); 1264 } 1265 } 1266 1267 MethodHandle[] lengthers = new MethodHandle[pc]; 1268 1269 // Figure out lengths: constants' lengths can be deduced on the spot. 1270 // All reference arguments were filtered to String in the combinators below, so we can 1271 // call the usual String.length(). Primitive values string sizes can be estimated. 1272 int initial = 0; 1273 for (RecipeElement el : recipe.getElements()) { 1274 switch (el.getTag()) { 1275 case TAG_CONST: 1276 Object cnst = el.getValue(); 1277 initial += cnst.toString().length(); 1278 break; 1279 case TAG_ARG: 1280 final int i = el.getArgPos(); 1281 Class<?> type = ptypes[i]; 1282 if (type.isPrimitive()) { 1283 MethodHandle est = MethodHandles.constant(int.class, estimateSize(type)); 1284 est = MethodHandles.dropArguments(est, 0, type); 1285 lengthers[i] = est; 1286 } else { 1287 lengthers[i] = STRING_LENGTH; 1288 } 1289 break; 1290 default: 1291 throw new StringConcatException("Unhandled tag: " + el.getTag()); 1292 } 1293 } 1294 1295 // Create (StringBuilder, <args>) shape for appending: 1296 MethodHandle builder = MethodHandles.dropArguments(MethodHandles.identity(StringBuilder.class), 1, ptypes); 1297 1298 // Compose append calls. This is done in reverse because the application order is 1299 // reverse as well. 1300 List<RecipeElement> elements = recipe.getElements(); 1301 for (int i = elements.size() - 1; i >= 0; i--) { 1302 RecipeElement el = elements.get(i); 1303 MethodHandle appender; 1304 switch (el.getTag()) { 1305 case TAG_CONST: 1306 Object constant = el.getValue(); 1307 MethodHandle mh = appender(adaptToStringBuilder(constant.getClass())); 1308 appender = MethodHandles.insertArguments(mh, 1, constant); 1309 break; 1310 case TAG_ARG: 1311 int ac = el.getArgPos(); 1312 appender = appender(ptypes[ac]); 1313 1314 // Insert dummy arguments to match the prefix in the signature. 1315 // The actual appender argument will be the ac-ith argument. 1316 if (ac != 0) { 1317 appender = MethodHandles.dropArguments(appender, 1, Arrays.copyOf(ptypes, ac)); 1318 } 1319 break; 1320 default: 1321 throw new StringConcatException("Unhandled tag: " + el.getTag()); 1322 } 1323 builder = MethodHandles.foldArguments(builder, appender); 1324 } 1325 1326 // Build the sub-tree that adds the sizes and produces a StringBuilder: 1327 1328 // a) Start with the reducer that accepts all arguments, plus one 1329 // slot for the initial value. Inject the initial value right away. 1330 // This produces (<ints>)int shape: 1331 MethodHandle sum = getReducerFor(pc + 1); 1332 MethodHandle adder = MethodHandles.insertArguments(sum, 0, initial); 1333 1334 // b) Apply lengthers to transform arguments to lengths, producing (<args>)int 1335 adder = MethodHandles.filterArguments(adder, 0, lengthers); 1336 1337 // c) Instantiate StringBuilder (<args>)int -> (<args>)StringBuilder 1338 MethodHandle newBuilder = MethodHandles.filterReturnValue(adder, NEW_STRING_BUILDER); 1339 1340 // d) Fold in StringBuilder constructor, this produces (<args>)StringBuilder 1341 MethodHandle mh = MethodHandles.foldArguments(builder, newBuilder); 1342 1343 // Convert non-primitive arguments to Strings 1344 mh = MethodHandles.filterArguments(mh, 0, filters); 1345 1346 // Convert (<args>)StringBuilder to (<args>)String 1347 if (DEBUG && mode.isExact()) { 1348 mh = MethodHandles.filterReturnValue(mh, BUILDER_TO_STRING_CHECKED); 1349 } else { 1350 mh = MethodHandles.filterReturnValue(mh, BUILDER_TO_STRING); 1351 } 1352 1353 return mh; 1354 } 1355 1356 private static MethodHandle getReducerFor(int cnt) { 1357 return SUMMERS.computeIfAbsent(cnt, SUMMER); 1358 } 1359 1360 private static MethodHandle appender(Class<?> appendType) { 1361 MethodHandle appender = lookupVirtual(MethodHandles.publicLookup(), StringBuilder.class, "append", 1362 StringBuilder.class, adaptToStringBuilder(appendType)); 1363 1364 // appenders should return void, this would not modify the target signature during folding 1365 MethodType nt = MethodType.methodType(void.class, StringBuilder.class, appendType); 1366 return appender.asType(nt); 1367 } 1368 1369 private static String toStringChecked(StringBuilder sb) { 1370 String s = sb.toString(); 1371 if (s.length() != sb.capacity()) { 1372 throw new AssertionError("Exactness check failed: result length = " + s.length() + ", buffer capacity = " + sb.capacity()); 1373 } 1374 return s; 1375 } 1376 1377 private static int sum(int v1, int v2) { 1378 return v1 + v2; 1379 } 1380 1381 private static int sum(int v1, int v2, int v3) { 1382 return v1 + v2 + v3; 1383 } 1384 1385 private static int sum(int v1, int v2, int v3, int v4) { 1386 return v1 + v2 + v3 + v4; 1387 } 1388 1389 private static int sum(int v1, int v2, int v3, int v4, int v5) { 1390 return v1 + v2 + v3 + v4 + v5; 1391 } 1392 1393 private static int sum(int v1, int v2, int v3, int v4, int v5, int v6) { 1394 return v1 + v2 + v3 + v4 + v5 + v6; 1395 } 1396 1397 private static int sum(int v1, int v2, int v3, int v4, int v5, int v6, int v7) { 1398 return v1 + v2 + v3 + v4 + v5 + v6 + v7; 1399 } 1400 1401 private static int sum(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8) { 1402 return v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8; 1403 } 1404 1405 private static int sum(int initial, int[] vs) { 1406 int sum = initial; 1407 for (int v : vs) { 1408 sum += v; 1409 } 1410 return sum; 1411 } 1412 1413 private static final ConcurrentMap<Integer, MethodHandle> SUMMERS; 1414 1415 // This one is deliberately non-lambdified to optimize startup time: 1416 private static final Function<Integer, MethodHandle> SUMMER = new Function<Integer, MethodHandle>() { 1417 @Override 1418 public MethodHandle apply(Integer cnt) { 1419 if (cnt == 1) { 1420 return MethodHandles.identity(int.class); 1421 } else if (cnt <= 8) { 1422 // Variable-arity collectors are not as efficient as small-count methods, 1423 // unroll some initial sizes. 1424 Class<?>[] cls = new Class<?>[cnt]; 1425 Arrays.fill(cls, int.class); 1426 return lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleStringBuilderStrategy.class, "sum", int.class, cls); 1427 } else { 1428 return lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleStringBuilderStrategy.class, "sum", int.class, int.class, int[].class) 1429 .asCollector(int[].class, cnt - 1); 1430 } 1431 } 1432 }; 1433 1434 private static final MethodHandle NEW_STRING_BUILDER, STRING_LENGTH, BUILDER_TO_STRING, BUILDER_TO_STRING_CHECKED; 1435 1436 static { 1437 SUMMERS = new ConcurrentHashMap<>(); 1438 Lookup publicLookup = MethodHandles.publicLookup(); 1439 NEW_STRING_BUILDER = lookupConstructor(publicLookup, StringBuilder.class, int.class); 1440 STRING_LENGTH = lookupVirtual(publicLookup, String.class, "length", int.class); 1441 BUILDER_TO_STRING = lookupVirtual(publicLookup, StringBuilder.class, "toString", String.class); 1442 if (DEBUG) { 1443 BUILDER_TO_STRING_CHECKED = lookupStatic(MethodHandles.Lookup.IMPL_LOOKUP, 1444 MethodHandleStringBuilderStrategy.class, "toStringChecked", String.class, StringBuilder.class); 1445 } else { 1446 BUILDER_TO_STRING_CHECKED = null; 1447 } 1448 } 1449 1450 } 1451 1452 1453 /** 1454 * <p><b>{@link Strategy#MH_INLINE_SIZED_EXACT}: "MethodHandles inline, 1455 * sized exactly".</b> 1456 * 1457 * <p>This strategy replicates what StringBuilders are doing: it builds the 1458 * byte[] array on its own and passes that byte[] array to String 1459 * constructor. This strategy requires access to some private APIs in JDK, 1460 * most notably, the read-only Integer/Long.stringSize methods that measure 1461 * the character length of the integers, and the private String constructor 1462 * that accepts byte[] arrays without copying. While this strategy assumes a 1463 * particular implementation details for String, this opens the door for 1464 * building a very optimal concatenation sequence. This is the only strategy 1465 * that requires porting if there are private JDK changes occur. 1466 */ 1467 private static final class MethodHandleInlineCopyStrategy { 1468 static final Unsafe UNSAFE = Unsafe.getUnsafe(); 1469 1470 private MethodHandleInlineCopyStrategy() { 1471 // no instantiation 1472 } 1473 1474 static MethodHandle generate(MethodType mt, Recipe recipe) throws Throwable { 1475 1476 // Create filters and obtain filtered parameter types. Filters would be used in the beginning 1477 // to convert the incoming arguments into the arguments we can process (e.g. Objects -> Strings). 1478 // The filtered argument type list is used all over in the combinators below. 1479 Class<?>[] ptypes = mt.parameterArray(); 1480 MethodHandle[] filters = null; 1481 for (int i = 0; i < ptypes.length; i++) { 1482 MethodHandle filter = Stringifiers.forMost(ptypes[i]); 1483 if (filter != null) { 1484 if (filters == null) { 1485 filters = new MethodHandle[ptypes.length]; 1486 } 1487 filters[i] = filter; 1488 ptypes[i] = filter.type().returnType(); 1489 } 1490 } 1491 1492 // Start building the combinator tree. The tree "starts" with (<parameters>)String, and "finishes" 1493 // with the (int, byte[], byte)String in String helper. The combinators are assembled bottom-up, 1494 // which makes the code arguably hard to read. 1495 1496 // Drop all remaining parameter types, leave only helper arguments: 1497 MethodHandle mh; 1498 1499 mh = MethodHandles.dropArguments(NEW_STRING, 3, ptypes); 1500 1501 // Mix in prependers. This happens when (byte[], int, byte) = (storage, index, coder) is already 1502 // known from the combinators below. We are assembling the string backwards, so "index" is the 1503 // *ending* index. 1504 for (RecipeElement el : recipe.getElements()) { 1505 // Do the prepend, and put "new" index at index 1 1506 mh = MethodHandles.dropArguments(mh, 2, int.class); 1507 switch (el.getTag()) { 1508 case TAG_CONST: { 1509 Object cnst = el.getValue(); 1510 MethodHandle prepender = MethodHandles.insertArguments(prepender(cnst.getClass()), 3, cnst); 1511 mh = MethodHandles.foldArguments(mh, 1, prepender, 1512 2, 0, 3 // index, storage, coder 1513 ); 1514 break; 1515 } 1516 case TAG_ARG: { 1517 int pos = el.getArgPos(); 1518 MethodHandle prepender = prepender(ptypes[pos]); 1519 mh = MethodHandles.foldArguments(mh, 1, prepender, 1520 2, 0, 3, // index, storage, coder 1521 4 + pos // selected argument 1522 ); 1523 break; 1524 } 1525 default: 1526 throw new StringConcatException("Unhandled tag: " + el.getTag()); 1527 } 1528 } 1529 1530 // Fold in byte[] instantiation at argument 0 1531 mh = MethodHandles.foldArguments(mh, 0, NEW_ARRAY, 1532 1, 2 // index, coder 1533 ); 1534 1535 // Start combining length and coder mixers. 1536 // 1537 // Length is easy: constant lengths can be computed on the spot, and all non-constant 1538 // shapes have been either converted to Strings, or explicit methods for getting the 1539 // string length out of primitives are provided. 1540 // 1541 // Coders are more interesting. Only Object, String and char arguments (and constants) 1542 // can have non-Latin1 encoding. It is easier to blindly convert constants to String, 1543 // and deduce the coder from there. Arguments would be either converted to Strings 1544 // during the initial filtering, or handled by primitive specializations in CODER_MIXERS. 1545 // 1546 // The method handle shape after all length and coder mixers is: 1547 // (int, byte, <args>)String = ("index", "coder", <args>) 1548 byte initialCoder = INITIAL_CODER; 1549 int initialLen = 0; // initial length, in characters 1550 for (RecipeElement el : recipe.getElements()) { 1551 switch (el.getTag()) { 1552 case TAG_CONST: 1553 Object constant = el.getValue(); 1554 String s = constant.toString(); 1555 initialCoder = (byte) coderMixer(String.class).invoke(initialCoder, s); 1556 initialLen += s.length(); 1557 break; 1558 case TAG_ARG: 1559 int ac = el.getArgPos(); 1560 1561 Class<?> argClass = ptypes[ac]; 1562 MethodHandle lm = lengthMixer(argClass); 1563 MethodHandle cm = coderMixer(argClass); 1564 1565 // Read this bottom up: 1566 1567 // 4. Drop old index and coder, producing ("new-index", "new-coder", <args>) 1568 mh = MethodHandles.dropArguments(mh, 2, int.class, byte.class); 1569 1570 // 3. Compute "new-index", producing ("new-index", "new-coder", "old-index", "old-coder", <args>) 1571 // Length mixer needs old index, plus the appropriate argument 1572 mh = MethodHandles.foldArguments(mh, 0, lm, 1573 2, // old-index 1574 4 + ac // selected argument 1575 ); 1576 1577 // 2. Compute "new-coder", producing ("new-coder", "old-index", "old-coder", <args>) 1578 // Coder mixer needs old coder, plus the appropriate argument. 1579 mh = MethodHandles.foldArguments(mh, 0, cm, 1580 2, // old-coder 1581 3 + ac // selected argument 1582 ); 1583 1584 // 1. The mh shape here is ("old-index", "old-coder", <args>) 1585 break; 1586 default: 1587 throw new StringConcatException("Unhandled tag: " + el.getTag()); 1588 } 1589 } 1590 1591 // Insert initial lengths and coders here. 1592 // The method handle shape here is (<args>). 1593 mh = MethodHandles.insertArguments(mh, 0, initialLen, initialCoder); 1594 1595 // Apply filters, converting the arguments: 1596 if (filters != null) { 1597 mh = MethodHandles.filterArguments(mh, 0, filters); 1598 } 1599 1600 return mh; 1601 } 1602 1603 @ForceInline 1604 private static byte[] newArray(int length, byte coder) { 1605 return (byte[]) UNSAFE.allocateUninitializedArray(byte.class, length << coder); 1606 } 1607 1608 private static MethodHandle prepender(Class<?> cl) { 1609 return PREPENDERS.computeIfAbsent(cl, PREPEND); 1610 } 1611 1612 private static MethodHandle coderMixer(Class<?> cl) { 1613 return CODER_MIXERS.computeIfAbsent(cl, CODER_MIX); 1614 } 1615 1616 private static MethodHandle lengthMixer(Class<?> cl) { 1617 return LENGTH_MIXERS.computeIfAbsent(cl, LENGTH_MIX); 1618 } 1619 1620 // This one is deliberately non-lambdified to optimize startup time: 1621 private static final Function<Class<?>, MethodHandle> PREPEND = new Function<Class<?>, MethodHandle>() { 1622 @Override 1623 public MethodHandle apply(Class<?> c) { 1624 return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "prepend", int.class, int.class, byte[].class, byte.class, c); 1625 } 1626 }; 1627 1628 // This one is deliberately non-lambdified to optimize startup time: 1629 private static final Function<Class<?>, MethodHandle> CODER_MIX = new Function<Class<?>, MethodHandle>() { 1630 @Override 1631 public MethodHandle apply(Class<?> c) { 1632 return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "mixCoder", byte.class, byte.class, c); 1633 } 1634 }; 1635 1636 // This one is deliberately non-lambdified to optimize startup time: 1637 private static final Function<Class<?>, MethodHandle> LENGTH_MIX = new Function<Class<?>, MethodHandle>() { 1638 @Override 1639 public MethodHandle apply(Class<?> c) { 1640 return lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "mixLen", int.class, int.class, c); 1641 } 1642 }; 1643 1644 private static final MethodHandle NEW_STRING; 1645 private static final MethodHandle NEW_ARRAY; 1646 private static final ConcurrentMap<Class<?>, MethodHandle> PREPENDERS; 1647 private static final ConcurrentMap<Class<?>, MethodHandle> LENGTH_MIXERS; 1648 private static final ConcurrentMap<Class<?>, MethodHandle> CODER_MIXERS; 1649 private static final byte INITIAL_CODER; 1650 static final Class<?> STRING_HELPER; 1651 1652 static { 1653 try { 1654 STRING_HELPER = Class.forName("java.lang.StringConcatHelper"); 1655 MethodHandle initCoder = lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "initialCoder", byte.class); 1656 INITIAL_CODER = (byte) initCoder.invoke(); 1657 } catch (Throwable e) { 1658 throw new AssertionError(e); 1659 } 1660 1661 PREPENDERS = new ConcurrentHashMap<>(); 1662 LENGTH_MIXERS = new ConcurrentHashMap<>(); 1663 CODER_MIXERS = new ConcurrentHashMap<>(); 1664 1665 NEW_STRING = lookupStatic(Lookup.IMPL_LOOKUP, STRING_HELPER, "newString", String.class, byte[].class, int.class, byte.class); 1666 NEW_ARRAY = lookupStatic(Lookup.IMPL_LOOKUP, MethodHandleInlineCopyStrategy.class, "newArray", byte[].class, int.class, byte.class); 1667 } 1668 } 1669 1670 /** 1671 * Public gateways to public "stringify" methods. These methods have the form String apply(T obj), and normally 1672 * delegate to {@code String.valueOf}, depending on argument's type. 1673 */ 1674 private static final class Stringifiers { 1675 private Stringifiers() { 1676 // no instantiation 1677 } 1678 1679 private static class StringifierMost extends ClassValue<MethodHandle> { 1680 @Override 1681 protected MethodHandle computeValue(Class<?> cl) { 1682 if (cl == String.class) { 1683 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, Object.class); 1684 } else if (cl == float.class) { 1685 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, float.class); 1686 } else if (cl == double.class) { 1687 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, double.class); 1688 } else if (!cl.isPrimitive()) { 1689 MethodHandle mhObject = lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, Object.class); 1690 1691 // We need the additional conversion here, because String.valueOf(Object) may return null. 1692 // String conversion rules in Java state we need to produce "null" String in this case. 1693 // It can be easily done with applying valueOf the second time. 1694 return MethodHandles.filterReturnValue(mhObject, 1695 mhObject.asType(MethodType.methodType(String.class, String.class))); 1696 } 1697 1698 return null; 1699 } 1700 } 1701 1702 private static class StringifierAny extends ClassValue<MethodHandle> { 1703 @Override 1704 protected MethodHandle computeValue(Class<?> cl) { 1705 if (cl == byte.class || cl == short.class || cl == int.class) { 1706 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, int.class); 1707 } else if (cl == boolean.class) { 1708 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, boolean.class); 1709 } else if (cl == char.class) { 1710 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, char.class); 1711 } else if (cl == long.class) { 1712 return lookupStatic(MethodHandles.publicLookup(), String.class, "valueOf", String.class, long.class); 1713 } else { 1714 MethodHandle mh = STRINGIFIERS_MOST.get(cl); 1715 if (mh != null) { 1716 return mh; 1717 } else { 1718 throw new IllegalStateException("Unknown class: " + cl); 1719 } 1720 } 1721 } 1722 } 1723 1724 private static final ClassValue<MethodHandle> STRINGIFIERS_MOST = new StringifierMost(); 1725 private static final ClassValue<MethodHandle> STRINGIFIERS_ANY = new StringifierAny(); 1726 1727 /** 1728 * Returns a stringifier for references and floats/doubles only. 1729 * Always returns null for other primitives. 1730 * 1731 * @param t class to stringify 1732 * @return stringifier; null, if not available 1733 */ 1734 static MethodHandle forMost(Class<?> t) { 1735 return STRINGIFIERS_MOST.get(t); 1736 } 1737 1738 /** 1739 * Returns a stringifier for any type. Never returns null. 1740 * 1741 * @param t class to stringify 1742 * @return stringifier 1743 */ 1744 static MethodHandle forAny(Class<?> t) { 1745 return STRINGIFIERS_ANY.get(t); 1746 } 1747 } 1748 1749 /* ------------------------------- Common utilities ------------------------------------ */ 1750 1751 static MethodHandle lookupStatic(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) { 1752 try { 1753 return lookup.findStatic(refc, name, MethodType.methodType(rtype, ptypes)); 1754 } catch (NoSuchMethodException | IllegalAccessException e) { 1755 throw new AssertionError(e); 1756 } 1757 } 1758 1759 static MethodHandle lookupVirtual(Lookup lookup, Class<?> refc, String name, Class<?> rtype, Class<?>... ptypes) { 1760 try { 1761 return lookup.findVirtual(refc, name, MethodType.methodType(rtype, ptypes)); 1762 } catch (NoSuchMethodException | IllegalAccessException e) { 1763 throw new AssertionError(e); 1764 } 1765 } 1766 1767 static MethodHandle lookupConstructor(Lookup lookup, Class<?> refc, Class<?> ptypes) { 1768 try { 1769 return lookup.findConstructor(refc, MethodType.methodType(void.class, ptypes)); 1770 } catch (NoSuchMethodException | IllegalAccessException e) { 1771 throw new AssertionError(e); 1772 } 1773 } 1774 1775 static int estimateSize(Class<?> cl) { 1776 if (cl == Integer.TYPE) { 1777 return 11; // "-2147483648" 1778 } else if (cl == Boolean.TYPE) { 1779 return 5; // "false" 1780 } else if (cl == Byte.TYPE) { 1781 return 4; // "-128" 1782 } else if (cl == Character.TYPE) { 1783 return 1; // duh 1784 } else if (cl == Short.TYPE) { 1785 return 6; // "-32768" 1786 } else if (cl == Double.TYPE) { 1787 return 26; // apparently, no larger than this, see FloatingDecimal.BinaryToASCIIBuffer.buffer 1788 } else if (cl == Float.TYPE) { 1789 return 26; // apparently, no larger than this, see FloatingDecimal.BinaryToASCIIBuffer.buffer 1790 } else if (cl == Long.TYPE) { 1791 return 20; // "-9223372036854775808" 1792 } else { 1793 throw new IllegalArgumentException("Cannot estimate the size for " + cl); 1794 } 1795 } 1796 1797 static Class<?> adaptToStringBuilder(Class<?> c) { 1798 if (c.isPrimitive()) { 1799 if (c == Byte.TYPE || c == Short.TYPE) { 1800 return int.class; 1801 } 1802 } else { 1803 if (c != String.class) { 1804 return Object.class; 1805 } 1806 } 1807 return c; 1808 } 1809 1810 private StringConcatFactory() { 1811 // no instantiation 1812 } 1813 1814 }