Skip to content

Commit 7f34da9

Browse files
committed
Remove calls to asGuestValue when using NFI and use the SignatureLibrary to call instead
1 parent a062c7c commit 7f34da9

File tree

15 files changed

+227
-215
lines changed

15 files changed

+227
-215
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/BZ2Object.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public static class BZ2Decompressor extends BZ2Object {
106106
private int bzsAvailInReal;
107107

108108
private byte[] nextIn;
109-
private Object nextInGuest;
110109
private int nextInIndex;
111110

112111
public BZ2Decompressor(Object cls, Shape instanceShape) {
@@ -119,7 +118,6 @@ public BZ2Decompressor(Object cls, Shape instanceShape) {
119118
this.unusedData = PythonUtils.EMPTY_BYTE_ARRAY;
120119

121120
this.nextIn = null;
122-
this.nextInGuest = null;
123121
this.nextInIndex = 0;
124122
}
125123

@@ -190,19 +188,10 @@ public byte[] getNextIn() {
190188
public void setNextIn(byte[] in) {
191189
assert in != null;
192190
this.nextIn = in;
193-
this.nextInGuest = null;
194191
}
195192

196193
public void clearNextIn() {
197194
this.nextIn = null;
198-
this.nextInGuest = null;
199-
}
200-
201-
public Object getNextInGuest(PythonContext context) {
202-
if (nextInGuest == null) {
203-
this.nextInGuest = context.getEnv().asGuestValue(nextIn);
204-
}
205-
return nextInGuest;
206195
}
207196

208197
public int getNextInIndex() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/bz2/Bz2Nodes.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ static byte[] nativeCompress(BZ2Object.BZ2Compressor self, PythonContext context
115115
@Cached GetOutputNativeBufferNode getBuffer,
116116
@Cached PRaiseNode raiseNode) {
117117
NFIBz2Support bz2Support = context.getNFIBz2Support();
118-
Object inGuest = context.getEnv().asGuestValue(bytes);
119-
int err = bz2Support.compress(self.getBzs(), inGuest, len, action, INITIAL_BUFFER_SIZE, compress);
118+
int err = bz2Support.compress(self.getBzs(), bytes, len, action, INITIAL_BUFFER_SIZE, compress);
120119
if (err != BZ_OK) {
121120
errorHandling(inliningTarget, err, raiseNode);
122121
}
@@ -240,9 +239,9 @@ static byte[] nativeInternalDecompress(BZ2Object.BZ2Decompressor self, int maxLe
240239
@Cached PRaiseNode raiseNode) {
241240
PythonContext context = PythonContext.get(inliningTarget);
242241
NFIBz2Support bz2Support = context.getNFIBz2Support();
243-
Object inGuest = self.getNextInGuest(context);
242+
byte[] in = self.getNextIn();
244243
int offset = self.getNextInIndex();
245-
int err = bz2Support.decompress(self.getBzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal(), decompress);
244+
int err = bz2Support.decompress(self.getBzs(), in, offset, maxLength, INITIAL_BUFFER_SIZE, self.getBzsAvailInReal(), decompress);
246245
long nextInIdx = bz2Support.getNextInIndex(self.getBzs(), getNextInIndex);
247246
long bzsAvailInReal = bz2Support.getBzsAvailInReal(self.getBzs(), getBzsAvailInReal);
248247
try {
@@ -285,9 +284,8 @@ static byte[] getBuffer(Node inliningTarget, Object bzst, PythonContext context,
285284
return PythonUtils.EMPTY_BYTE_ARRAY;
286285
}
287286
byte[] resultArray = new byte[size];
288-
Object out = context.getEnv().asGuestValue(resultArray);
289287
/* this will clear the native output once retrieved */
290-
bz2Support.getOutputBuffer(bzst, out, getBuffer);
288+
bz2Support.getOutputBuffer(bzst, resultArray, getBuffer);
291289
return resultArray;
292290
}
293291
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMAModuleBuiltins.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ public void initialize(Python3Core core) {
179179
super.initialize(core);
180180
}
181181

182-
private static Object as(Python3Core core, int[] a) {
183-
return core.getContext().getEnv().asGuestValue(a);
184-
}
185-
186-
private static Object as(Python3Core core, long[] a) {
187-
return core.getContext().getEnv().asGuestValue(a);
188-
}
189-
190182
@Override
191183
public void postInitialize(Python3Core c) {
192184
super.postInitialize(c);
@@ -199,9 +191,9 @@ public void postInitialize(Python3Core c) {
199191
long[] preset = new long[2];
200192
if (lzmaSupport.isAvailable()) {
201193
try {
202-
lzmaSupport.getMacros(as(c, formats),
203-
as(c, checks), as(c, FILTERS),
204-
as(c, mfs), as(c, modes), as(c, preset));
194+
lzmaSupport.getMacros(formats,
195+
checks, FILTERS,
196+
mfs, modes, preset);
205197
FORMAT_AUTO = formats[FORMAT_AUTO_INDEX];
206198
FORMAT_XZ = formats[FORMAT_XZ_INDEX];
207199
FORMAT_ALONE = formats[FORMAT_ALONE_INDEX];

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,13 @@ private static void setFilterOptions(Node inliningTarget, Object lzmast, long[]
511511
NativeLibrary.InvokeNativeFunction setFilterSpecBCJ,
512512
PRaiseNode raiseNode) {
513513
NFILZMASupport lzmaSupport = context.getNFILZMASupport();
514-
Object opts = context.getEnv().asGuestValue(filter);
515514
int err;
516515
long id = filter[0];
517516
switch (LZMAFilter.from(id)) {
518517
case LZMA_FILTER_LZMA1:
519518
case LZMA_FILTER_LZMA2:
520519
assert filter.length == 10;
521-
err = lzmaSupport.setFilterSpecLZMA(lzmast, fidx, opts, setFilterSpecLZMA);
520+
err = lzmaSupport.setFilterSpecLZMA(lzmast, fidx, filter, setFilterSpecLZMA);
522521
if (err != LZMA_OK) {
523522
if (err == LZMA_PRESET_ERROR) {
524523
throw raiseNode.raise(inliningTarget, LZMAError, INVALID_COMPRESSION_PRESET, filter[LZMAOption.preset.ordinal()]);
@@ -528,7 +527,7 @@ private static void setFilterOptions(Node inliningTarget, Object lzmast, long[]
528527
return;
529528
case LZMA_FILTER_DELTA:
530529
assert filter.length == 2;
531-
err = lzmaSupport.setFilterSpecDelta(lzmast, fidx, opts, setFilterSpecDelta);
530+
err = lzmaSupport.setFilterSpecDelta(lzmast, fidx, filter, setFilterSpecDelta);
532531
if (err != LZMA_OK) {
533532
errorHandling(inliningTarget, err, raiseNode);
534533
}
@@ -540,7 +539,7 @@ private static void setFilterOptions(Node inliningTarget, Object lzmast, long[]
540539
case LZMA_FILTER_ARMTHUMB:
541540
case LZMA_FILTER_SPARC:
542541
assert filter.length == 2;
543-
err = lzmaSupport.setFilterSpecBCJ(lzmast, fidx, opts, setFilterSpecBCJ);
542+
err = lzmaSupport.setFilterSpecBCJ(lzmast, fidx, filter, setFilterSpecBCJ);
544543
if (err != LZMA_OK) {
545544
errorHandling(inliningTarget, err, raiseNode);
546545
}
@@ -877,8 +876,7 @@ static byte[] nativeCompress(Node inliningTarget, LZMACompressor.Native self, Py
877876
@Cached InlinedConditionProfile errProfile,
878877
@Exclusive @Cached PRaiseNode raiseNode) {
879878
NFILZMASupport lzmaSupport = context.getNFILZMASupport();
880-
Object inGuest = context.getEnv().asGuestValue(bytes);
881-
int err = lzmaSupport.compress(self.getLzs(), inGuest, len, action, INITIAL_BUFFER_SIZE, compress);
879+
int err = lzmaSupport.compress(self.getLzs(), bytes, len, action, INITIAL_BUFFER_SIZE, compress);
882880
if (errProfile.profile(inliningTarget, err != LZMA_OK)) {
883881
errorHandling(inliningTarget, err, raiseNode);
884882
}
@@ -1119,7 +1117,7 @@ static byte[] nativeInternalDecompress(Node inliningTarget, LZMADecompressor.Nat
11191117
@Cached InlinedConditionProfile errProfile) {
11201118
PythonContext context = PythonContext.get(inliningTarget);
11211119
NFILZMASupport lzmaSupport = context.getNFILZMASupport();
1122-
Object inGuest = context.getEnv().asGuestValue(self.getNextIn());
1120+
byte[] inGuest = self.getNextIn();
11231121
int offset = self.getNextInIndex();
11241122
int err = lzmaSupport.decompress(self.getLzs(), inGuest, offset, maxLength, INITIAL_BUFFER_SIZE, self.getLzsAvailIn(), decompress);
11251123
long nextInIdx = lzmaSupport.getNextInIndex(self.getLzs(), getNextInIndex);
@@ -1242,9 +1240,8 @@ static byte[] getBuffer(Node inliningTarget, Object lzmast, PythonContext contex
12421240
return PythonUtils.EMPTY_BYTE_ARRAY;
12431241
}
12441242
byte[] resultArray = new byte[size];
1245-
Object out = context.getEnv().asGuestValue(resultArray);
12461243
/* this will clear the native output once retrieved */
1247-
lzmaSupport.getOutputBuffer(lzmast, out, getBuffer);
1244+
lzmaSupport.getOutputBuffer(lzmast, resultArray, getBuffer);
12481245
return resultArray;
12491246
}
12501247
}
@@ -1302,7 +1299,7 @@ static byte[] encodeNative(VirtualFrame frame, Object filter,
13021299
NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport();
13031300
Object lzmast = lzmaSupport.createStream(createStream);
13041301
long[] opts = filterConverter.execute(frame, filter);
1305-
int lzret = lzmaSupport.encodeFilter(lzmast, ctxt.getEnv().asGuestValue(opts), encodeFilter);
1302+
int lzret = lzmaSupport.encodeFilter(lzmast, opts, encodeFilter);
13061303
if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) {
13071304
lzmaSupport.deallocateStream(lzmast, deallocateStream);
13081305
if (lzret == LZMA_PRESET_ERROR) {
@@ -1344,9 +1341,7 @@ static void decodeNative(VirtualFrame frame, long id, byte[] encoded, PDict dict
13441341
NFILZMASupport lzmaSupport = ctxt.getNFILZMASupport();
13451342
long[] opts = new long[MAX_OPTS_INDEX];
13461343
int len = encoded.length;
1347-
Object encodedProps = ctxt.getEnv().asGuestValue(encoded);
1348-
Object filter = ctxt.getEnv().asGuestValue(opts);
1349-
int lzret = lzmaSupport.decodeFilter(id, encodedProps, len, filter, decodeFilter);
1344+
int lzret = lzmaSupport.decodeFilter(id, encoded, len, opts, decodeFilter);
13501345
if (errProfile.profile(inliningTarget, lzret != LZMA_OK)) {
13511346
if (lzret == LZMA_ID_ERROR) {
13521347
throw raiseNode.raise(inliningTarget, LZMAError, INVALID_FILTER, opts[LZMAOption.id.ordinal()]);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/NativeZlibCompObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// initialized
5050
public class NativeZlibCompObject extends ZLibCompObject {
5151
private NFIZlibSupport.Pointer pointer;
52-
Object lastInput;
52+
byte[] lastInput;
5353

5454
public NativeZlibCompObject(Object cls, Shape instanceShape, Object zst, NFIZlibSupport zlibSupport) {
5555
super(cls, instanceShape);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZLibModuleBuiltins.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static long error(Object data, @SuppressWarnings("unused") Object value,
389389
long nativeCrc32(byte[] bytes, int len, int value,
390390
NativeLibrary.InvokeNativeFunction invoke) {
391391
PythonContext ctxt = getContext();
392-
int signedVal = (int) ctxt.getNFIZlibSupport().crc32(value, ctxt.getEnv().asGuestValue(bytes), len, invoke);
392+
int signedVal = (int) ctxt.getNFIZlibSupport().crc32(value, bytes, len, invoke);
393393
return signedVal & 0xFFFFFFFFL;
394394
}
395395
}
@@ -463,7 +463,7 @@ long doJavaObject(VirtualFrame frame, Object data, int value,
463463
long nativeAdler32(byte[] bytes, int len, int value,
464464
PythonContext ctxt,
465465
NativeLibrary.InvokeNativeFunction invoke) {
466-
int signedVal = (int) ctxt.getNFIZlibSupport().adler32(value, ctxt.getEnv().asGuestValue(bytes), len, invoke);
466+
int signedVal = (int) ctxt.getNFIZlibSupport().adler32(value, bytes, len, invoke);
467467
return signedVal & 0xFFFFFFFFL;
468468
}
469469

@@ -648,7 +648,7 @@ static Object doNative(int level, int method, int wbits, int memLevel, int strat
648648
int err;
649649
if (zdict.length > 0) {
650650
err = zlibSupport.compressObjInitWithDict(zst, level, method, wbits, memLevel, strategy,
651-
PythonContext.get(inliningTarget).getEnv().asGuestValue(zdict), zdict.length, compressObjInit);
651+
zdict, zdict.length, compressObjInit);
652652
} else {
653653
err = zlibSupport.compressObjInit(zst, level, method, wbits, memLevel, strategy, compressObjInit);
654654
}
@@ -719,7 +719,7 @@ static Object doNative(int wbits, byte[] zdict,
719719

720720
int err;
721721
if (zdict.length > 0) {
722-
err = zlibSupport.decompressObjInitWithDict(zst, wbits, context.getEnv().asGuestValue(zdict), zdict.length, decompressObjInit);
722+
err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length, decompressObjInit);
723723
} else {
724724
err = zlibSupport.decompressObjInit(zst, wbits, decompressObjInit);
725725
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibCompressBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ static PBytes doit(NativeZlibCompObject self, int mode,
249249
assert self.isInitialized();
250250
PythonContext context = PythonContext.get(inliningTarget);
251251
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
252-
Object lastInput;
252+
byte[] lastInput;
253253
if (self.lastInput == null) {
254254
// all previous input data has been processed or nothing has been compressed.
255-
lastInput = context.getEnv().asGuestValue(PythonUtils.EMPTY_BYTE_ARRAY);
255+
lastInput = PythonUtils.EMPTY_BYTE_ARRAY;
256256
} else {
257257
// pass the last data input to continue processing.
258258
// all other needed info, e.g. size and offset, about the last data input is

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibDecompressorBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static Object doNative(@SuppressWarnings("unused") Object type, int wbits, byte[
133133

134134
int err;
135135
if (zdict.length > 0) {
136-
err = zlibSupport.decompressObjInitWithDict(zst, wbits, context.getEnv().asGuestValue(zdict), zdict.length, decompressObjInit);
136+
err = zlibSupport.decompressObjInitWithDict(zst, wbits, zdict, zdict.length, decompressObjInit);
137137
} else {
138138
err = zlibSupport.decompressObjInit(zst, wbits, decompressObjInit);
139139
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/zlib/ZlibNodes.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static byte[] nativeCompress(Node inliningTarget, NativeZlibCompObject self, Pyt
121121
@Cached GetNativeBufferNode getBuffer,
122122
@Cached ZlibNativeErrorHandling errorHandling) {
123123
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
124-
self.lastInput = context.getEnv().asGuestValue(bytes);
124+
self.lastInput = bytes;
125125
int err = zlibSupport.compressObj(self.getZst(), self.lastInput, len, DEF_BUF_SIZE, compressObj);
126126
if (err != Z_OK) {
127127
errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false);
@@ -146,7 +146,7 @@ static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int lev
146146
@Cached ZlibNativeErrorHandling errorHandling) {
147147
PythonContext context = PythonContext.get(inliningTarget);
148148
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
149-
Object in = context.getEnv().asGuestValue(bytes);
149+
byte[] in = bytes;
150150
Object zst = zlibSupport.createStream(createStream);
151151
int err = zlibSupport.deflateOffHeap(zst, in, len, DEF_BUF_SIZE, level, wbits, deflateOffHeap);
152152
if (err != Z_OK) {
@@ -170,7 +170,7 @@ static byte[] nativeDecompress(Node inliningTarget, NativeZlibCompObject self, P
170170
@Cached GetNativeBufferNode getBuffer,
171171
@Cached ZlibNativeErrorHandling errorHandling) {
172172
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
173-
Object in = context.getEnv().asGuestValue(bytes);
173+
byte[] in = bytes;
174174
int err = zlibSupport.decompressObj(self.getZst(), in, len, DEF_BUF_SIZE, maxLength, decompressObj);
175175
if (err != Z_OK) {
176176
errorHandling.execute(inliningTarget, self.getZst(), err, zlibSupport, false);
@@ -192,7 +192,7 @@ static byte[] nativeDecompressBuf(Node inliningTarget, ZlibDecompressorObject se
192192
@Cached GetNativeBufferNode getBuffer,
193193
@Cached ZlibNativeErrorHandling errorHandling) {
194194
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
195-
Object in = context.getEnv().asGuestValue(bytes);
195+
byte[] in = bytes;
196196
int ret = zlibSupport.decompressor(self.getZst(), in, len, maxLength, decompressor);
197197
if (ret < 0) {
198198
errorHandling.execute(inliningTarget, self.getZst(), ret, zlibSupport, false);
@@ -217,7 +217,7 @@ static byte[] nativeCompress(Node inliningTarget, byte[] bytes, int len, int wbi
217217
@Cached ZlibNativeErrorHandling errorHandling) {
218218
NFIZlibSupport zlibSupport = context.getNFIZlibSupport();
219219
Object zst = zlibSupport.createStream(createStream);
220-
Object in = context.getEnv().asGuestValue(bytes);
220+
byte[] in = bytes;
221221
int err = zlibSupport.inflateOffHeap(zst, in, len, bufsize, wbits, inflateOffHeap);
222222
if (err != Z_OK) {
223223
errorHandling.execute(inliningTarget, zst, err, zlibSupport, true);
@@ -547,8 +547,7 @@ static byte[] getBuffer(Object zst, int option, PythonContext context,
547547
return PythonUtils.EMPTY_BYTE_ARRAY;
548548
}
549549
byte[] resultArray = new byte[size];
550-
Object out = context.getEnv().asGuestValue(resultArray);
551-
zlibSupport.getBuffer(zst, option, out, getBuffer);
550+
zlibSupport.getBuffer(zst, option, resultArray, getBuffer);
552551
return resultArray;
553552
}
554553
}

0 commit comments

Comments
 (0)