|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.truffle.api.benchmark; |
| 42 | + |
| 43 | +import java.util.stream.IntStream; |
| 44 | + |
| 45 | +import org.graalvm.polyglot.Context; |
| 46 | +import org.graalvm.polyglot.Engine; |
| 47 | +import org.openjdk.jmh.annotations.Benchmark; |
| 48 | +import org.openjdk.jmh.annotations.Scope; |
| 49 | +import org.openjdk.jmh.annotations.Setup; |
| 50 | +import org.openjdk.jmh.annotations.State; |
| 51 | +import org.openjdk.jmh.annotations.TearDown; |
| 52 | +import org.openjdk.jmh.annotations.Threads; |
| 53 | +import org.openjdk.jmh.annotations.Warmup; |
| 54 | + |
| 55 | +import com.oracle.truffle.api.object.DynamicObject; |
| 56 | +import com.oracle.truffle.api.object.DynamicObjectLibrary; |
| 57 | +import com.oracle.truffle.api.object.Shape; |
| 58 | + |
| 59 | +@Warmup(iterations = 10, time = 1) |
| 60 | +@SuppressWarnings("deprecation") |
| 61 | +public class DynamicObjectBenchmark extends TruffleBenchmark { |
| 62 | + |
| 63 | + static final String TEST_LANGUAGE = "benchmark-test-language"; |
| 64 | + private static final int PROPERTY_KEYS_PER_ITERATION = 1000; |
| 65 | + |
| 66 | + private static final class MyDynamicObject extends DynamicObject { |
| 67 | + private MyDynamicObject(Shape shape) { |
| 68 | + super(shape); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @State(Scope.Benchmark) |
| 73 | + public static class SharedEngineState { |
| 74 | + final Engine engine = Engine.newBuilder().allowExperimentalOptions(true).option("engine.Compilation", "false").build(); |
| 75 | + final Shape rootShape = Shape.newBuilder().build(); |
| 76 | + final String[] propertyKeys = IntStream.range(0, PROPERTY_KEYS_PER_ITERATION).mapToObj(i -> "testKey" + i).toArray(String[]::new); |
| 77 | + final Shape[] expectedShapes = new Shape[PROPERTY_KEYS_PER_ITERATION]; |
| 78 | + |
| 79 | + @TearDown |
| 80 | + public void tearDown() { |
| 81 | + engine.close(); |
| 82 | + } |
| 83 | + |
| 84 | + private void assertSameShape(int i, Shape actualShape) { |
| 85 | + Shape expectedShape = expectedShapes[i]; |
| 86 | + if (expectedShape == null) { |
| 87 | + expectedShapes[i] = actualShape; |
| 88 | + } else if (expectedShape != actualShape) { |
| 89 | + throw new AssertionError("Expected shape: " + expectedShape + " but was: " + actualShape); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @State(Scope.Thread) |
| 95 | + public static class PerThreadContextState { |
| 96 | + Context context; |
| 97 | + |
| 98 | + @Setup |
| 99 | + public void setup(SharedEngineState shared) { |
| 100 | + context = Context.newBuilder(TEST_LANGUAGE).engine(shared.engine).build(); |
| 101 | + } |
| 102 | + |
| 103 | + @TearDown |
| 104 | + public void tearDown() { |
| 105 | + context.close(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Benchmark that stresses the shape transition cache with multi-threaded lookups. |
| 111 | + */ |
| 112 | + @Benchmark |
| 113 | + @Threads(8) |
| 114 | + public void shapeTransitionMapContended(SharedEngineState shared, PerThreadContextState perThread) { |
| 115 | + perThread.context.enter(); |
| 116 | + for (int i = 0; i < PROPERTY_KEYS_PER_ITERATION; i++) { |
| 117 | + DynamicObject object = new MyDynamicObject(shared.rootShape); |
| 118 | + DynamicObjectLibrary.getUncached().put(object, shared.propertyKeys[i], "testValue"); |
| 119 | + shared.assertSameShape(i, object.getShape()); |
| 120 | + } |
| 121 | + perThread.context.leave(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Benchmark that stresses the shape transition cache with single-threaded lookups. |
| 126 | + */ |
| 127 | + @Benchmark |
| 128 | + @Threads(1) |
| 129 | + public void shapeTransitionMapUncontended(SharedEngineState shared, PerThreadContextState perThread) { |
| 130 | + perThread.context.enter(); |
| 131 | + for (int i = 0; i < PROPERTY_KEYS_PER_ITERATION; i++) { |
| 132 | + DynamicObject object = new MyDynamicObject(shared.rootShape); |
| 133 | + DynamicObjectLibrary.getUncached().put(object, shared.propertyKeys[i], "testValue"); |
| 134 | + shared.assertSameShape(i, object.getShape()); |
| 135 | + } |
| 136 | + perThread.context.leave(); |
| 137 | + } |
| 138 | +} |
0 commit comments