1 /*
   2  * Copyright (c) 2014, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.vm.compiler;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.Setup;
  31 import org.openjdk.jmh.annotations.State;
  32 
  33 import java.util.concurrent.TimeUnit;
  34 
  35 /**
  36  * Benchmark measuring effect of loop optimizations.
  37  */
  38 @BenchmarkMode(Mode.AverageTime)
  39 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  40 @State(Scope.Thread)
  41 public class CopyLoop {
  42 
  43     private MyString s;
  44     private Buf b;
  45 
  46     @Setup
  47     public void setup() {
  48         s = new MyString("foobarba");
  49         b = new Buf();
  50     }
  51 
  52     /**
  53      * Basic implementation, as a Java programmer would write it.
  54      */
  55     @Benchmark
  56     public void testCharAtLoop() throws Exception {
  57         for (int i = 0; i < s.length(); i++) {
  58             int c = s.charAt(i);
  59             b.byteBuf[i] = (byte) c;
  60         }
  61     }
  62 
  63     /** Inline charAt and remove the redundant bounds checks. */
  64     @Benchmark
  65     public void testInlineCharAtLoop() throws Exception {
  66         for (int i = 0; i < s.count; i++) {
  67             int c = s.value[i + s.offset];
  68             b.byteBuf[i] = (byte) c;
  69         }
  70     }
  71 
  72     /**
  73      * Unroll the loop (I cheat here because I know that the String will always be an even length, real implementation
  74      * would require a pre-loop).
  75      */
  76     @Benchmark
  77     public void testInlineAndUnrollCharAtLoop() throws Exception {
  78         int startI = 0;
  79         if ((s.count & 0xFFFE) != s.count) {
  80             int c = s.value[s.offset];
  81             b.byteBuf[0] = (byte) c;
  82             startI = 1;
  83         }
  84         for (int i = startI; i < s.count - 1; i += 2) {
  85             int c = s.value[i + s.offset];
  86             int d = s.value[i + s.offset + 1];
  87             b.byteBuf[i] = (byte) c;
  88             b.byteBuf[i + 1] = (byte) d;
  89         }
  90     }
  91 
  92     /** Hoist computation of constants outside of the loop. */
  93     @Benchmark
  94     public void testInlineAndUnrollAndHoistCharAtLoop() throws Exception {
  95         byte[] byteBuf = b.byteBuf;
  96         if (byteBuf == null) {
  97             throw new NullPointerException();
  98         }
  99         char[] value = s.value;
 100         int offset = s.offset;
 101 
 102         int startI = offset;
 103 
 104         if ((s.count & 0xFFFE) != s.count) {
 105             int c = value[offset];
 106             byteBuf[0] = (byte) c;
 107             startI++;
 108         }
 109 
 110         int maxI = s.count + offset - 1;
 111         for (int i = startI; i < maxI; i += 2) {
 112             int c = value[i];
 113             int d = value[i + 1];
 114             byteBuf[i] = (byte) c;
 115             byteBuf[i + 1] = (byte) d;
 116         }
 117     }
 118 
 119     private static class Buf {
 120         private byte[] byteBuf = new byte[100];
 121     }
 122 
 123     public static final class MyString {
 124         private char value[];
 125 
 126         private int offset;
 127 
 128         private int count;
 129 
 130         public MyString(String original) {
 131             value = original.toCharArray();
 132             offset = 0;
 133             count = value.length;
 134         }
 135 
 136         public int length() {
 137             return count;
 138         }
 139 
 140         public char charAt(int index) {
 141             if ((index < 0) || (index >= count)) {
 142                 throw new StringIndexOutOfBoundsException(index);
 143             }
 144             return value[index + offset];
 145         }
 146 
 147     }
 148 
 149 }