Skip to content

Commit 329cbd6

Browse files
committed
Use primitives for string_pointer* calls in cext.rb
* Removes the overhead of an extra call. (cherry picked from commit 82178d9)
1 parent 5096db4 commit 329cbd6

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,31 +149,31 @@ def initialize(string)
149149
end
150150

151151
def size
152-
Truffle::CExt.string_pointer_size(@string)
152+
TrufflePrimitive.string_pointer_size(@string)
153153
end
154154

155155
def polyglot_pointer?
156156
true
157157
end
158158

159159
def polyglot_address
160-
@address ||= Truffle::CExt.string_pointer_to_native(@string)
160+
@address ||= TrufflePrimitive.string_pointer_to_native(@string)
161161
end
162162

163163
# Every IS_POINTER object should also have TO_NATIVE
164164
def polyglot_to_native
165165
end
166166

167167
def [](index)
168-
Truffle::CExt.string_pointer_read(@string, index)
168+
TrufflePrimitive.string_pointer_read(@string, index)
169169
end
170170

171171
def []=(index, value)
172-
Truffle::CExt.string_pointer_write(@string, index, value)
172+
TrufflePrimitive.string_pointer_write(@string, index, value)
173173
end
174174

175175
def native?
176-
Truffle::CExt.string_pointer_is_native?(@string)
176+
TrufflePrimitive.string_pointer_is_native?(@string)
177177
end
178178

179179
alias_method :to_str, :string
@@ -231,7 +231,7 @@ def polyglot_pointer?
231231
end
232232

233233
def polyglot_address
234-
@address ||= Truffle::CExt.string_pointer_to_native(@string) + @string.bytesize
234+
@address ||= TrufflePrimitive.string_pointer_to_native(@string) + @string.bytesize
235235
end
236236

237237
# Every IS_POINTER object should also have TO_NATIVE
@@ -1846,6 +1846,10 @@ def rb_enc_from_native_encoding(rb_encoding)
18461846
RbEncoding.get_encoding_from_native(rb_encoding)
18471847
end
18481848

1849+
def native_string?(string)
1850+
TrufflePrimitive.string_pointer_is_native?(string)
1851+
end
1852+
18491853
def RSTRING_PTR(string)
18501854
RStringPtr.new(string)
18511855
end

spec/truffle/capi/string_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
it "does not store the String to native memory if not needed" do
1919
str = "foobar"
2020
@s.string_ptr(str).should == "f"
21-
Truffle::CExt.string_pointer_is_native?(str).should == false
21+
Truffle::CExt.native_string?(str).should == false
2222
end
2323

2424
it "stores the String to native memory if the address is returned" do
2525
str = "foobar"
2626
@s.string_ptr_return_address(str).should be_kind_of(Integer)
27-
Truffle::CExt.string_pointer_is_native?(str).should == true
27+
Truffle::CExt.native_string?(str).should == true
2828
end
2929
end
3030

@@ -35,8 +35,8 @@
3535

3636
it "ensures the String is stored in native memory" do
3737
str = "foobar"
38-
Truffle::CExt.string_pointer_is_native?(str).should == false
38+
Truffle::CExt.native_string?(str).should == false
3939
@s.NATIVE_RSTRING_PTR(str)
40-
Truffle::CExt.string_pointer_is_native?(str).should == true
40+
Truffle::CExt.native_string?(str).should == true
4141
end
4242
end

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,8 @@ protected Object rbHash(Object object) {
957957
}
958958
}
959959

960-
@CoreMethod(names = "string_pointer_size", onSingleton = true, required = 1)
961-
public abstract static class StringPointerSizeNode extends CoreMethodArrayArgumentsNode {
960+
@Primitive(name = "string_pointer_size")
961+
public abstract static class StringPointerSizeNode extends PrimitiveArrayArgumentsNode {
962962

963963
@Specialization(guards = "isRubyString(string)")
964964
protected int size(DynamicObject string) {
@@ -1010,8 +1010,8 @@ protected NativeRope toNative(DynamicObject string,
10101010

10111011
}
10121012

1013-
@CoreMethod(names = "string_pointer_to_native", onSingleton = true, required = 1)
1014-
public abstract static class StringPointerToNativeNode extends CoreMethodArrayArgumentsNode {
1013+
@Primitive(name = "string_pointer_to_native")
1014+
public abstract static class StringPointerToNativeNode extends PrimitiveArrayArgumentsNode {
10151015

10161016
@Specialization(guards = "isRubyString(string)")
10171017
protected long toNative(DynamicObject string,
@@ -1038,8 +1038,8 @@ protected DynamicObject toNative(DynamicObject string,
10381038

10391039
}
10401040

1041-
@CoreMethod(names = "string_pointer_is_native?", onSingleton = true, required = 1)
1042-
public abstract static class StringPointerIsNativeNode extends CoreMethodArrayArgumentsNode {
1041+
@Primitive(name = "string_pointer_is_native?")
1042+
public abstract static class StringPointerIsNativeNode extends PrimitiveArrayArgumentsNode {
10431043

10441044
@Specialization(guards = "isRubyString(string)")
10451045
protected boolean isNative(DynamicObject string) {
@@ -1048,8 +1048,8 @@ protected boolean isNative(DynamicObject string) {
10481048

10491049
}
10501050

1051-
@CoreMethod(names = "string_pointer_read", onSingleton = true, required = 2, lowerFixnum = 2)
1052-
public abstract static class StringPointerReadNode extends CoreMethodArrayArgumentsNode {
1051+
@Primitive(name = "string_pointer_read", lowerFixnum = 1)
1052+
public abstract static class StringPointerReadNode extends PrimitiveArrayArgumentsNode {
10531053

10541054
@Specialization(guards = "isRubyString(string)")
10551055
protected Object read(DynamicObject string, int index,
@@ -1068,8 +1068,8 @@ protected Object read(DynamicObject string, int index,
10681068

10691069
}
10701070

1071-
@CoreMethod(names = "string_pointer_write", onSingleton = true, required = 3, lowerFixnum = { 2, 3 })
1072-
public abstract static class StringPointerWriteNode extends CoreMethodArrayArgumentsNode {
1071+
@Primitive(name = "string_pointer_write", lowerFixnum = { 1, 2 })
1072+
public abstract static class StringPointerWriteNode extends PrimitiveArrayArgumentsNode {
10731073

10741074
@Specialization(guards = "isRubyString(string)")
10751075
protected int write(DynamicObject string, int index, int value,

0 commit comments

Comments
 (0)