Skip to content

Commit 2df65c1

Browse files
committed
Do not eagerly materialize native strings from 'char *'.
1 parent 822dea0 commit 2df65c1

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.oracle.graal.python.builtins.objects.function.PKeyword;
7777
import com.oracle.graal.python.builtins.objects.ints.PInt;
7878
import com.oracle.graal.python.builtins.objects.module.PythonModule;
79+
import com.oracle.graal.python.builtins.objects.str.NativeCharSequence;
7980
import com.oracle.graal.python.builtins.objects.str.PString;
8081
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
8182
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
@@ -891,13 +892,12 @@ public static AsCharPointerNode getUncached() {
891892
// -----------------------------------------------------------------------------------------------------------------
892893
@GenerateUncached
893894
public abstract static class FromCharPointerNode extends CExtBaseNode {
894-
public abstract String execute(Object charPtr);
895+
public abstract Object execute(Object charPtr);
895896

896897
@Specialization
897-
public String execute(Object charPtr,
898-
@Cached PCallCapiFunction callCstrToStringNode) {
899-
900-
return (String) callCstrToStringNode.call(FUN_PY_TRUFFLE_CSTR_TO_STRING, charPtr);
898+
PString execute(Object charPtr,
899+
@Cached PythonObjectFactory factory) {
900+
return factory.createString(new NativeCharSequence(charPtr));
901901
}
902902
}
903903

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2019, 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.graal.python.builtins.objects.str;
42+
43+
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
44+
import com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols;
45+
import org.graalvm.nativeimage.ImageInfo;
46+
47+
import com.oracle.graal.python.nodes.PGuards;
48+
import com.oracle.graal.python.runtime.PythonOptions;
49+
import com.oracle.truffle.api.CompilerAsserts;
50+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
51+
import com.oracle.truffle.api.profiles.ConditionProfile;
52+
53+
public class NativeCharSequence implements CharSequence {
54+
55+
private final Object ptr;
56+
private String materialized;
57+
58+
public NativeCharSequence(Object ptr) {
59+
this.ptr = ptr;
60+
}
61+
62+
@Override
63+
public int length() {
64+
return materialize().length();
65+
}
66+
67+
@Override
68+
public char charAt(int index) {
69+
return materialize().charAt(index);
70+
}
71+
72+
@Override
73+
public CharSequence subSequence(int start, int end) {
74+
return materialize().subSequence(start, end);
75+
}
76+
77+
private String materialize() {
78+
if(materialized == null) {
79+
materialized = (String) PCallCapiFunction.getUncached().call(NativeCAPISymbols.FUN_PY_TRUFFLE_CSTR_TO_STRING, ptr);
80+
}
81+
return materialized;
82+
}
83+
84+
@Override
85+
public String toString() {
86+
return materialize();
87+
}
88+
}

0 commit comments

Comments
 (0)