Skip to content

Commit 3cd92ba

Browse files
committed
Refactor DeleteAtNode to reduce code size + get rid of createCast
1 parent 9925333 commit 3cd92ba

File tree

1 file changed

+15
-37
lines changed

1 file changed

+15
-37
lines changed

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
import com.oracle.truffle.api.dsl.Cached;
104104
import com.oracle.truffle.api.dsl.Cached.Shared;
105105
import com.oracle.truffle.api.dsl.Cached.Exclusive;
106-
import com.oracle.truffle.api.dsl.CreateCast;
107106
import com.oracle.truffle.api.dsl.ImportStatic;
108107
import com.oracle.truffle.api.dsl.NodeChild;
109108
import com.oracle.truffle.api.dsl.ReportPolymorphism;
@@ -767,54 +766,33 @@ public void checkFrozen(Object object) {
767766
@ReportPolymorphism
768767
public abstract static class DeleteAtNode extends CoreMethodNode {
769768

770-
@CreateCast("index")
771-
protected ToIntNode coerceOtherToInt(RubyBaseNodeWithExecute index) {
772-
return ToIntNode.create(index);
773-
}
774-
775-
@Specialization(
776-
guards = "stores.isMutable(store)",
777-
limit = "storageStrategyLimit()")
778-
protected Object deleteAt(RubyArray array, int index,
769+
@Specialization(limit = "storageStrategyLimit()")
770+
protected static Object doDelete(RubyArray array, Object indexObject,
779771
@Bind("array.getStore()") Object store,
780772
@CachedLibrary("store") ArrayStoreLibrary stores,
781-
@Cached @Shared IntValueProfile arraySizeProfile,
782-
@Cached @Shared ConditionProfile negativeIndexProfile,
783-
@Cached @Shared ConditionProfile notInBoundsProfile) {
784-
final int size = arraySizeProfile.profile(array.size);
773+
@Cached ToIntNode toIntNode,
774+
@Cached InlinedIntValueProfile arraySizeProfile,
775+
@Cached InlinedConditionProfile negativeIndexProfile,
776+
@Cached InlinedConditionProfile notInBoundsProfile,
777+
@Cached InlinedConditionProfile isMutableProfile,
778+
@Bind("this") Node node) {
779+
final int size = arraySizeProfile.profile(node, array.size);
780+
final int index = toIntNode.execute(indexObject);
785781
int i = index;
786-
if (negativeIndexProfile.profile(index < 0)) {
782+
if (negativeIndexProfile.profile(node, index < 0)) {
787783
i += size;
788784
}
789785

790-
if (notInBoundsProfile.profile(i < 0 || i >= size)) {
786+
if (notInBoundsProfile.profile(node, i < 0 || i >= size)) {
791787
return nil;
792-
} else {
788+
}
789+
790+
if (isMutableProfile.profile(node, stores.isMutable(store))) {
793791
final Object value = stores.read(store, i);
794792
stores.copyContents(store, i + 1, store, i, size - i - 1);
795793
stores.clear(store, size - 1, 1);
796794
setStoreAndSize(array, store, size - 1);
797795
return value;
798-
}
799-
}
800-
801-
@Specialization(
802-
guards = "!stores.isMutable(store)",
803-
limit = "storageStrategyLimit()")
804-
protected Object deleteAtCopying(RubyArray array, int index,
805-
@Bind("array.getStore()") Object store,
806-
@CachedLibrary("store") ArrayStoreLibrary stores,
807-
@Cached @Shared IntValueProfile arraySizeProfile,
808-
@Cached @Shared ConditionProfile negativeIndexProfile,
809-
@Cached @Shared ConditionProfile notInBoundsProfile) {
810-
final int size = arraySizeProfile.profile(array.size);
811-
int i = index;
812-
if (negativeIndexProfile.profile(index < 0)) {
813-
i += size;
814-
}
815-
816-
if (notInBoundsProfile.profile(i < 0 || i >= size)) {
817-
return nil;
818796
} else {
819797
final Object mutableStore = stores.allocator(store).allocate(size - 1);
820798
stores.copyContents(store, 0, mutableStore, 0, i);

0 commit comments

Comments
 (0)