# Лабораторная работа №2: JIT — интерпретация, C1, C2 * **Студент:** Махоткин Андрей Игоревич --- ## Эксперименты ### Эксперимент 1. Один код — разная скорость > Запустить обычным способом. Записать времена всех 10 прогонов. ```run 0: 1.23123 ms run 0: 7.1213 ms run 1: 5.5246 ms run 2: 1.9863 ms run 3: 1.9649 ms run 4: 1.8933 ms run 5: 1.9039 ms run 6: 1.8742 ms run 7: 2.4007 ms run 8: 2.5081 ms run 9: 2.397 ms ``` > Запустить с -Xint. Записать. ```run 0: 1.23123 ms run 0: 88.9515 ms run 1: 87.8346 ms run 2: 87.6601 ms run 3: 87.7495 ms run 4: 87.6847 ms run 5: 87.7426 ms run 6: 87.6961 ms run 7: 87.6527 ms run 8: 87.889 ms run 9: 87.8071 ms ``` > Запустить с -Xcomp. Записать. ```run 0: 1.23123 ms run 0: 109.9494 ms run 1: 5.8966 ms run 2: 1.9138 ms run 3: 2.0293 ms run 4: 1.8765 ms run 5: 1.871 ms run 6: 1.8882 ms run 7: 1.8663 ms run 8: 1.8544 ms run 9: 1.8904 ms ``` > Построить таблицу: режим → первый прогон → последний прогон → во сколько раз быстрее. | Режим | Первый прогон | Последний прогон | Во сколько раз быстрее | |---------|---------------|------------------|--------------------------------------------------------------| | Обычный | 7.1213 ms | 2.397 ms | во много (очень) (ну в 50 раз точно) | | -Xint | 88.9515 ms | 87.8071 ms | самый медленный вариант | | -Xcomp | 109.9494 ms | 1.8904 ms | сначала чуть больше чем -Xint, а затем идёт ближе к обычному | > Вопросы: > Почему в обычном режиме первый прогон медленный, а последующие быстрые? должно же оно инициализировать функцию main() > Почему -Xint стабильно медленный? он компилирует как только он подходит к строке кода, исключая возможность оптимизации > Почему -Xcomp может быть быстрее -Xint, но медленнее обычного режима на старте? > Для отчёта: таблица «режим → время 1-го прогона → время 10-го прогона → ускорение». см. выше? ### Эксперимент 2. Кто и когда компилируется ```time_ms, comp_id, comp_attribute, comp_level, class_method_id, bytecode_size 41 1 3 java.lang.Object:: (1 bytes) 42 4 n 0 jdk.internal.misc.Unsafe::getReferenceVolatile (native) 43 3 3 java.lang.String::isLatin1 (19 bytes) 43 2 3 java.lang.String::hashCode (60 bytes) 44 5 3 java.lang.String::charAt (25 bytes) 44 6 3 java.lang.String::checkIndex (10 bytes) 44 7 3 jdk.internal.util.Preconditions::checkIndex (18 bytes) 46 8 n 0 jdk.internal.vm.Continuation::enterSpecial (native) (static) 46 9 n 0 jdk.internal.vm.Continuation::doYield (native) (static) 50 10 3 java.lang.String::coder (15 bytes) 51 11 3 java.lang.String::length (11 bytes) 52 12 3 java.lang.Byte::toUnsignedInt (6 bytes) 54 13 3 java.lang.StringLatin1::indexOfChar (33 bytes) 55 15 n 0 java.lang.Object::hashCode (native) 55 14 3 java.util.Objects::requireNonNull (14 bytes) 55 16 n 0 java.lang.invoke.MethodHandle::linkToStatic(LLLLLLL)L (native) (static) 57 17 1 java.lang.Enum::ordinal (5 bytes) 58 18 n 0 java.lang.invoke.MethodHandle::linkToStatic(LLLLL)L (native) (static) 60 19 n 0 java.lang.invoke.MethodHandle::invokeBasic(LLLLLL)L (native) 61 20 n 0 java.lang.System::arraycopy (native) (static) 62 21 1 java.lang.Class::isPrimitive (5 bytes) 62 22 3 java.util.concurrent.ConcurrentHashMap::tabAt (21 bytes) 65 25 4 java.lang.Object:: (1 bytes) 67 26 n 0 java.lang.invoke.MethodHandle::linkToSpecial(LLLL)V (native) (static) 67 24 3 java.lang.Enum:: (15 bytes) 68 1 3 java.lang.Object:: (1 bytes) made not entrant: not used 69 23 3 jdk.internal.misc.Unsafe::getReferenceAcquire (7 bytes) 70 27 n 0 java.lang.invoke.MethodHandle::invokeBasic(LL)L (native) 70 29 n 0 java.lang.invoke.MethodHandle::linkToSpecial(LLLL)L (native) (static) 73 28 3 java.util.concurrent.ConcurrentHashMap::spread (10 bytes) 75 30 4 java.lang.Byte::toUnsignedInt (6 bytes) 75 12 3 java.lang.Byte::toUnsignedInt (6 bytes) made not entrant: not used 76 31 n 0 java.lang.ref.Reference::refersTo0 (native) 77 32 1 java.nio.Buffer::position (5 bytes) 77 35 n 0 jdk.internal.misc.Unsafe::getByte (native) 78 33 1 java.nio.Buffer::limit (5 bytes) 78 38 n 0 jdk.internal.misc.Unsafe::getShort (native) 81 34 3 java.lang.ref.Reference::reachabilityFence (1 bytes) 81 44 n 0 jdk.internal.misc.Unsafe::getInt (native) 83 36 3 jdk.internal.misc.Unsafe::pickPos (14 bytes) 84 37 3 jdk.internal.misc.Unsafe::toUnsignedInt (6 bytes) 86 52 4 jdk.internal.util.Preconditions::checkIndex (18 bytes) 87 56 3 java.lang.StringCoding::countPositives (33 bytes) 88 7 3 jdk.internal.util.Preconditions::checkIndex (18 bytes) made not entrant: not used 88 55 4 jdk.internal.misc.Unsafe::pickPos (14 bytes) 90 53 2 jdk.internal.util.ArraysSupport::unsignedHashCode (36 bytes) 90 36 3 jdk.internal.misc.Unsafe::pickPos (14 bytes) made not entrant: not used 91 64 4 java.lang.StringCoding::countPositives (33 bytes) 92 61 3 jdk.internal.util.ArraysSupport::vectorizedHashCode (158 bytes) 95 56 3 java.lang.StringCoding::countPositives (33 bytes) made not entrant: not used 96 60 4 jdk.internal.misc.Unsafe::toUnsignedInt (6 bytes) 98 39 2 java.util.zip.ZipUtils::get16 (40 bytes) 98 37 3 jdk.internal.misc.Unsafe::toUnsignedInt (6 bytes) made not entrant: not used 101 75 % 4 jdk.internal.util.ArraysSupport::unsignedHashCode @ 8 (36 bytes) 101 40 2 jdk.internal.misc.Unsafe::getShortUnaligned (12 bytes) 103 80 n 0 java.lang.invoke.MethodHandle::linkToStatic(LL)L (native) (static) 103 41 2 jdk.internal.misc.Unsafe::getShortUnaligned (33 bytes) 104 81 n 0 java.lang.Object::clone (native) 105 42 2 jdk.internal.misc.Unsafe::convEndian (16 bytes) 106 84 n 0 java.lang.invoke.MethodHandle::linkToSpecial(LL)V (native) (static) 106 43 2 java.lang.Short::toUnsignedInt (5 bytes) 107 85 n 0 java.lang.invoke.MethodHandle::invokeBasic()L (native) 108 46 2 jdk.internal.misc.Unsafe::getIntUnaligned (83 bytes) 109 86 n 0 jdk.internal.misc.Unsafe::compareAndSetLong (native) 110 47 2 java.util.zip.ZipUtils::get32 (40 bytes) 115 90 n 0 java.lang.invoke.MethodHandle::linkToStatic(LLL)L (native) (static) 115 48 2 jdk.internal.misc.Unsafe::getIntUnaligned (12 bytes) 117 91 n 0 java.lang.invoke.MethodHandle::linkToStatic(LLLL)L (native) (static) 118 49 2 jdk.internal.misc.Unsafe::convEndian (16 bytes) 119 92 n 0 java.lang.invoke.MethodHandle::linkToSpecial(LL)L (native) (static) 119 50 2 java.lang.Integer::toUnsignedLong (7 bytes) 121 51 2 jdk.internal.misc.Unsafe::makeShort (26 bytes) 121 93 n 0 jdk.internal.misc.Unsafe::compareAndSetReference (native) 122 54 2 jdk.internal.misc.Unsafe::toUnsignedInt (6 bytes) 123 57 2 java.util.zip.ZipUtils::CENCOM (9 bytes) 128 94 4 jdk.internal.util.ArraysSupport::unsignedHashCode (36 bytes) 129 58 2 java.util.zip.ZipUtils::CENEXT (9 bytes) 130 53 2 jdk.internal.util.ArraysSupport::unsignedHashCode (36 bytes) made not entrant: not used 131 95 4 java.util.zip.ZipUtils::get16 (40 bytes) 132 96 4 jdk.internal.misc.Unsafe::getShortUnaligned (12 bytes) 133 99 2 MinerKt::hot (22 bytes) 134 39 2 java.util.zip.ZipUtils::get16 (40 bytes) made not entrant: not used 134 97 4 java.util.zip.ZipUtils::get32 (40 bytes) 135 63 2 jdk.internal.util.ArraysSupport::hashCodeOfUnsigned (55 bytes) 135 40 2 jdk.internal.misc.Unsafe::getShortUnaligned (12 bytes) made not entrant: not used 136 100 % 4 MinerKt::hot @ 4 (22 bytes) 137 70 2 java.util.zip.ZipUtils::CENNAM (9 bytes) 141 47 2 java.util.zip.ZipUtils::get32 (40 bytes) made not entrant: not used 142 98 4 jdk.internal.misc.Unsafe::getIntUnaligned (12 bytes) 144 76 2 java.util.zip.ZipUtils::CENSIZ (9 bytes) 144 101 4 MinerKt::hot (22 bytes) 146 48 2 jdk.internal.misc.Unsafe::getIntUnaligned (12 bytes) made not entrant: not used 147 68 2 java.util.zip.ZipUtils::CENHOW (9 bytes) 149 99 2 MinerKt::hot (22 bytes) made not entrant: not used 149 45 1 java.util.zip.ZipCoder$UTF8ZipCoder::isUTF8 (2 bytes) 150 65 2 java.util.zip.ZipFile::zipCoderFor (27 bytes) 151 77 2 java.util.zip.ZipUtils::CENLEN (9 bytes) 153 102 % 4 MinerKt::main @ 5 (39 bytes) 153 10 69 2 java.util.zip.ZipUtils::CENFLG (9 bytes) ``` Форматируется вывод -XX:+PrintCompilation примерно так (первые три строчки): | Время с начала (в мс) | ID компиляции | Аттрибуты компиляции | Уровень компиляции | ID класса и метода | Размер байт-кода | |-----------------------|---------------|----------------------|--------------------|------------------------------------------------|------------------| | 41 | 1 | | 3 | java.lang.Object:: | (1 bytes) | | 42 | 4 | n | 0 | jdk.internal.misc.Unsafe::getReferenceVolatile | (native) | | 43 | 3 | | 3 | java.lang.String::isLatin1 | (19 bytes) |