|
26 | 26 | package com.oracle.graal.python.builtins.objects.common;
|
27 | 27 |
|
28 | 28 | import static com.oracle.graal.python.builtins.objects.iterator.IteratorBuiltins.NextHelperNode.STOP_MARKER;
|
| 29 | +import static com.oracle.graal.python.nodes.truffle.TruffleStringMigrationHelpers.assertNoJavaString; |
29 | 30 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.IndexError;
|
30 | 31 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.MemoryError;
|
31 | 32 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
|
|
40 | 41 | import static com.oracle.graal.python.runtime.sequence.storage.SequenceStorage.StorageType.Uninitialized;
|
41 | 42 |
|
42 | 43 | import java.lang.reflect.Array;
|
| 44 | +import java.math.BigInteger; |
43 | 45 | import java.util.Arrays;
|
44 | 46 |
|
45 | 47 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
@@ -3170,10 +3172,39 @@ static SequenceStorage doEmpty(EmptySequenceStorage s) {
|
3170 | 3172 | return s;
|
3171 | 3173 | }
|
3172 | 3174 |
|
3173 |
| - @Specialization(limit = "MAX_BASIC_STORAGES", guards = "s.getClass() == cachedClass") |
3174 |
| - static SequenceStorage doSpecial(BasicSequenceStorage s, |
3175 |
| - @Cached("s.getClass()") Class<? extends BasicSequenceStorage> cachedClass) { |
3176 |
| - return CompilerDirectives.castExact(CompilerDirectives.castExact(s, cachedClass).copy(), cachedClass); |
| 3175 | + @Specialization |
| 3176 | + static SequenceStorage doInt(IntSequenceStorage storage) { |
| 3177 | + return new IntSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalIntArray(), storage.length())); |
| 3178 | + } |
| 3179 | + |
| 3180 | + @Specialization |
| 3181 | + static SequenceStorage doLong(LongSequenceStorage storage) { |
| 3182 | + return new LongSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalLongArray(), storage.length())); |
| 3183 | + } |
| 3184 | + |
| 3185 | + @Specialization |
| 3186 | + static SequenceStorage doDouble(DoubleSequenceStorage storage) { |
| 3187 | + return new DoubleSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalDoubleArray(), storage.length())); |
| 3188 | + } |
| 3189 | + |
| 3190 | + @Specialization |
| 3191 | + static SequenceStorage doByte(ByteSequenceStorage storage) { |
| 3192 | + return new ByteSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalByteArray(), storage.length())); |
| 3193 | + } |
| 3194 | + |
| 3195 | + @Specialization |
| 3196 | + static SequenceStorage doBoolean(BoolSequenceStorage storage) { |
| 3197 | + return new BoolSequenceStorage(PythonUtils.arrayCopyOf(storage.getInternalBoolArray(), storage.length())); |
| 3198 | + } |
| 3199 | + |
| 3200 | + @Specialization |
| 3201 | + static SequenceStorage doObject(ObjectSequenceStorage storage) { |
| 3202 | + return new ObjectSequenceStorage(PythonUtils.arrayCopyOf(storage.getCopyOfInternalArray(), storage.length())); |
| 3203 | + } |
| 3204 | + |
| 3205 | + @Specialization |
| 3206 | + static SequenceStorage doMro(MroSequenceStorage storage) { |
| 3207 | + return new MroSequenceStorage(storage.getClassName(), PythonUtils.arrayCopyOf(storage.getInternalClassArray(), storage.length())); |
3177 | 3208 | }
|
3178 | 3209 |
|
3179 | 3210 | @Specialization
|
@@ -3667,53 +3698,185 @@ static boolean isObjectSequenceStorage(SequenceStorage s) {
|
3667 | 3698 |
|
3668 | 3699 | }
|
3669 | 3700 |
|
| 3701 | + @GenerateCached |
3670 | 3702 | @GenerateUncached
|
3671 | 3703 | @GenerateInline(inlineByDefault = true)
|
3672 |
| - @GenerateCached |
3673 |
| - @ImportStatic(SequenceStorageBaseNode.class) |
3674 |
| - public abstract static class InsertItemNode extends Node { |
3675 |
| - public final SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value) { |
3676 |
| - return execute(inliningTarget, storage, index, value, true); |
| 3704 | + public abstract static class InsertItemArrayBasedStorageNode extends Node { |
| 3705 | + |
| 3706 | + protected abstract SequenceStorage execute(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value); |
| 3707 | + |
| 3708 | + public final SequenceStorage executeCached(ArrayBasedSequenceStorage storage, int index, Object value) { |
| 3709 | + return execute(this, storage, index, value); |
3677 | 3710 | }
|
3678 | 3711 |
|
3679 |
| - public static SequenceStorage executeUncached(SequenceStorage storage, int index, Object value) { |
3680 |
| - return InsertItemNodeGen.getUncached().execute(null, storage, index, value); |
| 3712 | + @Specialization |
| 3713 | + static SequenceStorage doIntStorage(Node inliningTarget, IntSequenceStorage storage, int idx, int value, |
| 3714 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3715 | + final int length = storage.length(); |
| 3716 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3717 | + final int[] values = storage.getInternalIntArray(); |
| 3718 | + |
| 3719 | + // shifting tail to the right by one slot |
| 3720 | + for (int i = values.length - 1; i > idx; i--) { |
| 3721 | + values[i] = values[i - 1]; |
| 3722 | + } |
| 3723 | + |
| 3724 | + values[idx] = value; |
| 3725 | + storage.incLength(); |
| 3726 | + return storage; |
| 3727 | + } |
| 3728 | + |
| 3729 | + @Specialization |
| 3730 | + static SequenceStorage doDoubleStorage(Node inliningTarget, DoubleSequenceStorage storage, int idx, double value, |
| 3731 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3732 | + final int length = storage.length(); |
| 3733 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3734 | + final double[] values = storage.getInternalDoubleArray(); |
| 3735 | + |
| 3736 | + // shifting tail to the right by one slot |
| 3737 | + for (int i = values.length - 1; i > idx; i--) { |
| 3738 | + values[i] = values[i - 1]; |
| 3739 | + } |
| 3740 | + |
| 3741 | + values[idx] = value; |
| 3742 | + storage.incLength(); |
| 3743 | + return storage; |
3681 | 3744 | }
|
3682 | 3745 |
|
3683 |
| - protected final SequenceStorage executeCached(SequenceStorage storage, int index, Object value, boolean recursive) { |
3684 |
| - return execute(this, storage, index, value, recursive); |
| 3746 | + @Specialization |
| 3747 | + static SequenceStorage doLongWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, long value, |
| 3748 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3749 | + return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode); |
3685 | 3750 | }
|
3686 | 3751 |
|
3687 |
| - protected abstract SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value, boolean recursive); |
| 3752 | + @Specialization |
| 3753 | + static SequenceStorage doIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, int value, |
| 3754 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3755 | + return insertLong(inliningTarget, storage, idx, value, ensureCapacityNode); |
| 3756 | + } |
3688 | 3757 |
|
3689 | 3758 | @Specialization
|
3690 |
| - protected static SequenceStorage doEmptyStorage(EmptySequenceStorage storage, int index, Object value, boolean recursive, |
3691 |
| - @Shared @Cached(inline = false) InsertItemNode recursiveNode) { |
3692 |
| - if (!recursive) { |
3693 |
| - throw CompilerDirectives.shouldNotReachHere(); |
| 3759 | + static SequenceStorage doBigIntWithLongStorage(Node inliningTarget, LongSequenceStorage storage, int idx, BigInteger value, |
| 3760 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3761 | + return insertLong(inliningTarget, storage, idx, PInt.longValue(value), ensureCapacityNode); |
| 3762 | + } |
| 3763 | + |
| 3764 | + @Specialization |
| 3765 | + static SequenceStorage doByteWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value, |
| 3766 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3767 | + return insertByte(inliningTarget, storage, idx, value, ensureCapacityNode); |
| 3768 | + } |
| 3769 | + |
| 3770 | + @Specialization |
| 3771 | + static SequenceStorage doIntWithByteStorage(Node inliningTarget, ByteSequenceStorage storage, int idx, int value, |
| 3772 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3773 | + return insertByte(inliningTarget, storage, idx, (byte) value, ensureCapacityNode); |
| 3774 | + } |
| 3775 | + |
| 3776 | + @Specialization |
| 3777 | + static SequenceStorage doBoolStorage(Node inliningTarget, BoolSequenceStorage storage, int idx, boolean value, |
| 3778 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3779 | + final int length = storage.length(); |
| 3780 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3781 | + final boolean[] values = storage.getInternalBoolArray(); |
| 3782 | + |
| 3783 | + // shifting tail to the right by one slot |
| 3784 | + for (int i = values.length - 1; i > idx; i--) { |
| 3785 | + values[i] = values[i - 1]; |
3694 | 3786 | }
|
3695 |
| - SequenceStorage newStorage = storage.generalizeFor(value, null); |
3696 |
| - return recursiveNode.executeCached(newStorage, index, value, false); |
| 3787 | + |
| 3788 | + values[idx] = value; |
| 3789 | + storage.incLength(); |
| 3790 | + return storage; |
3697 | 3791 | }
|
3698 | 3792 |
|
3699 |
| - @Specialization(limit = "MAX_BASIC_STORAGES", guards = {"storage.getClass() == cachedClass"}) |
3700 |
| - protected static SequenceStorage doStorage(BasicSequenceStorage storage, int index, Object value, boolean recursive, |
3701 |
| - @Shared @Cached(inline = false) InsertItemNode recursiveNode, |
3702 |
| - @Cached("storage.getClass()") Class<? extends BasicSequenceStorage> cachedClass) { |
3703 |
| - try { |
3704 |
| - cachedClass.cast(storage).insertItem(index, value); |
3705 |
| - return storage; |
3706 |
| - } catch (SequenceStoreException e) { |
3707 |
| - if (!recursive) { |
3708 |
| - throw CompilerDirectives.shouldNotReachHere(); |
3709 |
| - } |
3710 |
| - SequenceStorage newStorage = cachedClass.cast(storage).generalizeFor(value, null); |
3711 |
| - return recursiveNode.executeCached(newStorage, index, value, false); |
| 3793 | + @Specialization |
| 3794 | + static SequenceStorage doObjectStorage(Node inliningTarget, ObjectSequenceStorage storage, int idx, Object value, |
| 3795 | + @Shared @Cached EnsureCapacityNode ensureCapacityNode) { |
| 3796 | + final int length = storage.length(); |
| 3797 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3798 | + final Object[] values = storage.getInternalArray(); |
| 3799 | + |
| 3800 | + // shifting tail to the right by one slot |
| 3801 | + for (int i = values.length - 1; i > idx; i--) { |
| 3802 | + values[i] = values[i - 1]; |
| 3803 | + } |
| 3804 | + |
| 3805 | + values[idx] = assertNoJavaString(value); |
| 3806 | + storage.incLength(); |
| 3807 | + return storage; |
| 3808 | + } |
| 3809 | + |
| 3810 | + @Specialization |
| 3811 | + static SequenceStorage doMroStorage(MroSequenceStorage storage, int idx, Object value) { |
| 3812 | + throw CompilerDirectives.shouldNotReachHere(); |
| 3813 | + } |
| 3814 | + |
| 3815 | + @Fallback |
| 3816 | + static SequenceStorage doGeneralization(ArrayBasedSequenceStorage storage, int idx, Object value, |
| 3817 | + @Cached(inline = false) InsertItemArrayBasedStorageNode recursiveNode) { |
| 3818 | + ObjectSequenceStorage newStorage = storage.generalize(); |
| 3819 | + return recursiveNode.executeCached(newStorage, idx, value); |
| 3820 | + } |
| 3821 | + |
| 3822 | + private static SequenceStorage insertLong(Node inliningTarget, LongSequenceStorage storage, int idx, long value, EnsureCapacityNode ensureCapacityNode) { |
| 3823 | + final int length = storage.length(); |
| 3824 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3825 | + final long[] values = storage.getInternalLongArray(); |
| 3826 | + |
| 3827 | + // shifting tail to the right by one slot |
| 3828 | + for (int i = values.length - 1; i > idx; i--) { |
| 3829 | + values[i] = values[i - 1]; |
3712 | 3830 | }
|
| 3831 | + |
| 3832 | + values[idx] = value; |
| 3833 | + storage.incLength(); |
| 3834 | + return storage; |
| 3835 | + } |
| 3836 | + |
| 3837 | + private static SequenceStorage insertByte(Node inliningTarget, ByteSequenceStorage storage, int idx, byte value, EnsureCapacityNode ensureCapacityNode) { |
| 3838 | + final int length = storage.length(); |
| 3839 | + ensureCapacityNode.execute(inliningTarget, storage, length + 1); |
| 3840 | + final byte[] values = storage.getInternalByteArray(); |
| 3841 | + |
| 3842 | + // shifting tail to the right by one slot |
| 3843 | + for (int i = values.length - 1; i > idx; i--) { |
| 3844 | + values[i] = values[i - 1]; |
| 3845 | + } |
| 3846 | + |
| 3847 | + values[idx] = value; |
| 3848 | + storage.incLength(); |
| 3849 | + return storage; |
| 3850 | + } |
| 3851 | + } |
| 3852 | + |
| 3853 | + @GenerateUncached |
| 3854 | + @GenerateInline |
| 3855 | + @GenerateCached(false) |
| 3856 | + @ImportStatic(SequenceStorageBaseNode.class) |
| 3857 | + public abstract static class InsertItemNode extends Node { |
| 3858 | + public static SequenceStorage executeUncached(SequenceStorage storage, int index, Object value) { |
| 3859 | + return InsertItemNodeGen.getUncached().execute(null, storage, index, value); |
| 3860 | + } |
| 3861 | + |
| 3862 | + public abstract SequenceStorage execute(Node inliningTarget, SequenceStorage storage, int index, Object value); |
| 3863 | + |
| 3864 | + @Specialization |
| 3865 | + protected static SequenceStorage doEmptyStorage(Node inliningTarget, EmptySequenceStorage storage, int index, Object value, |
| 3866 | + @Shared @Cached InsertItemArrayBasedStorageNode insertArrayBasedNode) { |
| 3867 | + ArrayBasedSequenceStorage newStorage = storage.generalizeFor(value); |
| 3868 | + return insertArrayBasedNode.execute(inliningTarget, newStorage, index, value); |
| 3869 | + } |
| 3870 | + |
| 3871 | + @Specialization |
| 3872 | + static SequenceStorage doArrayBasedStorage(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value, |
| 3873 | + @Shared @Cached InsertItemArrayBasedStorageNode insertArrayBasedNode) { |
| 3874 | + return insertArrayBasedNode.execute(inliningTarget, storage, index, value); |
| 3875 | + |
3713 | 3876 | }
|
3714 | 3877 |
|
3715 | 3878 | @Specialization
|
3716 |
| - protected static SequenceStorage doStorage(Node inliningTarget, NativeSequenceStorage storage, int index, Object value, @SuppressWarnings("unused") boolean recursive, |
| 3879 | + protected static SequenceStorage doNativeStorage(Node inliningTarget, NativeSequenceStorage storage, int index, Object value, |
3717 | 3880 | @Cached EnsureCapacityNode ensureCapacityNode,
|
3718 | 3881 | @Cached(inline = false) GetItemScalarNode getItem,
|
3719 | 3882 | @Cached SetItemScalarNode setItem) {
|
|
0 commit comments