1 /*
   2  * Copyright (c) 2015, 2015, 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.graalvm.compiler.microbenchmarks.graal;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.Warmup;
  27 
  28 import org.graalvm.compiler.graph.Graph;
  29 import org.graalvm.compiler.microbenchmarks.graal.util.GraalState;
  30 import org.graalvm.compiler.microbenchmarks.graal.util.GraphState;
  31 import org.graalvm.compiler.microbenchmarks.graal.util.MethodSpec;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 
  34 /**
  35  * Benchmarks the performance of {@link Graph#copy()}.
  36  */
  37 public class GraphCopyBenchmark extends GraalBenchmark {
  38 
  39     @MethodSpec(declaringClass = ConditionalEliminationBenchmark.class, name = "nullnessSnippet")
  40     public static class Nullness extends GraphState {
  41     }
  42 
  43     @SuppressWarnings("unused")
  44     public static int nullnessSnippet(Object a, Object b) {
  45         if (a == null) {
  46             if (a == b) {
  47                 if (b == null) {
  48                     return 1;
  49                 } else {
  50                     return -2;
  51                 }
  52             } else {
  53                 if (b == null) {
  54                     return -3;
  55                 } else {
  56                     return 4;
  57                 }
  58             }
  59         } else {
  60             if (a == b) {
  61                 if (b == null) {
  62                     return -5;
  63                 } else {
  64                     return 6;
  65                 }
  66             } else {
  67                 if (b == null) {
  68                     return 7;
  69                 } else {
  70                     return 8;
  71                 }
  72             }
  73         }
  74     }
  75 
  76     @Benchmark
  77     @Warmup(iterations = 20)
  78     public StructuredGraph nullness(Nullness s, @SuppressWarnings("unused") GraalState g) {
  79         return (StructuredGraph) s.graph.copy();
  80     }
  81 
  82     @MethodSpec(declaringClass = GraphCopyBenchmark.class, name = "searchSnippet")
  83     public static class Search extends GraphState {
  84     }
  85 
  86     static class Entry {
  87         final String name;
  88 
  89         Entry(String name) {
  90             this.name = name;
  91         }
  92     }
  93 
  94     static class EntryWithNext extends Entry {
  95         EntryWithNext(String name, Entry next) {
  96             super(name);
  97             this.next = next;
  98         }
  99 
 100         final Entry next;
 101     }
 102 
 103     public static Entry searchSnippet(Entry start, String name, Entry alternative) {
 104         Entry current = start;
 105         do {
 106             while (current instanceof EntryWithNext) {
 107                 if (name != null && current.name == name) {
 108                     current = null;
 109                 } else {
 110                     Entry next = ((EntryWithNext) current).next;
 111                     current = next;
 112                 }
 113             }
 114 
 115             if (current != null) {
 116                 if (current.name.equals(name)) {
 117                     return current;
 118                 }
 119             }
 120             if (current == alternative) {
 121                 return null;
 122             }
 123             current = alternative;
 124 
 125         } while (true);
 126     }
 127 
 128     @Benchmark
 129     @Warmup(iterations = 20)
 130     public StructuredGraph search(Search s, @SuppressWarnings("unused") GraalState g) {
 131         return (StructuredGraph) s.graph.copy();
 132     }
 133 }