Skip to content

Commit b593756

Browse files
committed
opt GIL: DynamicObjectNativeWrapper, PythonNativeWrapper
1 parent fdf159d commit b593756

File tree

2 files changed

+57
-155
lines changed

2 files changed

+57
-155
lines changed

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

Lines changed: 49 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* without restriction, including without limitation the rights to copy, create
2222
* 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
23+
* use, sell, offer for sale, import, export, have made, and have sold the// skip GIL
2424
* Software and the Larger Work(s), and to sublicense the foregoing rights on
2525
* either these or other terms.
2626
*
@@ -213,13 +213,8 @@ public DynamicObjectStorage getNativeMemberStore() {
213213

214214
@ExportMessage
215215
protected boolean isNull(
216-
@CachedLibrary("this") PythonNativeWrapperLibrary lib, @Exclusive @Cached GilNode gil) {
217-
boolean mustRelease = gil.acquire();
218-
try {
219-
return lib.getDelegate(this) == PNone.NO_VALUE;
220-
} finally {
221-
gil.release(mustRelease);
222-
}
216+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
217+
return lib.getDelegate(this) == PNone.NO_VALUE;
223218
}
224219

225220
// READ
@@ -235,13 +230,8 @@ protected boolean isMemberReadable(@SuppressWarnings("unused") String member) {
235230
}
236231

237232
@ExportMessage
238-
protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @Exclusive @Cached GilNode gil) {
239-
boolean mustRelease = gil.acquire();
240-
try {
241-
return new InteropArray(new String[]{DynamicObjectNativeWrapper.GP_OBJECT});
242-
} finally {
243-
gil.release(mustRelease);
244-
}
233+
protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
234+
return new InteropArray(new String[]{DynamicObjectNativeWrapper.GP_OBJECT});
245235
}
246236

247237
@ExportMessage(name = "readMember")
@@ -1356,13 +1346,8 @@ protected boolean isMemberModifiable(String member) {
13561346
}
13571347

13581348
@ExportMessage
1359-
protected boolean isMemberInsertable(@SuppressWarnings("unused") String member, @Exclusive @Cached GilNode gil) {
1360-
boolean mustRelease = gil.acquire();
1361-
try {
1362-
return false;
1363-
} finally {
1364-
gil.release(mustRelease);
1365-
}
1349+
protected boolean isMemberInsertable(@SuppressWarnings("unused") String member) {
1350+
return false;
13661351
}
13671352

13681353
@ExportMessage
@@ -1378,33 +1363,18 @@ protected void writeMember(String member, Object value,
13781363
}
13791364

13801365
@ExportMessage
1381-
protected boolean isMemberRemovable(@SuppressWarnings("unused") String member, @Exclusive @Cached GilNode gil) {
1382-
boolean mustRelease = gil.acquire();
1383-
try {
1384-
return false;
1385-
} finally {
1386-
gil.release(mustRelease);
1387-
}
1366+
protected boolean isMemberRemovable(@SuppressWarnings("unused") String member) {
1367+
return false;
13881368
}
13891369

13901370
@ExportMessage
1391-
protected void removeMember(@SuppressWarnings("unused") String member, @Exclusive @Cached GilNode gil) throws UnsupportedMessageException {
1392-
boolean mustRelease = gil.acquire();
1393-
try {
1394-
throw UnsupportedMessageException.create();
1395-
} finally {
1396-
gil.release(mustRelease);
1397-
}
1371+
protected void removeMember(@SuppressWarnings("unused") String member) throws UnsupportedMessageException {
1372+
throw UnsupportedMessageException.create();
13981373
}
13991374

14001375
@ExportMessage
1401-
protected boolean isExecutable(@Exclusive @Cached GilNode gil) {
1402-
boolean mustRelease = gil.acquire();
1403-
try {
1404-
return true;
1405-
} finally {
1406-
gil.release(mustRelease);
1407-
}
1376+
protected boolean isExecutable() {
1377+
return true;
14081378
}
14091379

14101380
@ExportMessage
@@ -1563,25 +1533,15 @@ abstract static class IsMemberReadable {
15631533
@Specialization(guards = {"stringEquals(cachedName, name, stringProfile)", "isValid(cachedName)"})
15641534
static boolean isReadableNativeMembers(PythonObjectNativeWrapper receiver, String name,
15651535
@Cached("createBinaryProfile()") ConditionProfile stringProfile,
1566-
@Cached(value = "name", allowUncached = true) String cachedName, @Exclusive @Cached GilNode gil) {
1567-
boolean mustRelease = gil.acquire();
1568-
try {
1569-
return true;
1570-
} finally {
1571-
gil.release(mustRelease);
1572-
}
1536+
@Cached(value = "name", allowUncached = true) String cachedName) {
1537+
return true;
15731538
}
15741539

15751540
@SuppressWarnings("unused")
15761541
@Specialization(guards = "stringEquals(GP_OBJECT, name, stringProfile)")
15771542
static boolean isReadableCachedGP(PythonObjectNativeWrapper receiver, String name,
1578-
@Cached("createBinaryProfile()") ConditionProfile stringProfile, @Exclusive @Cached GilNode gil) {
1579-
boolean mustRelease = gil.acquire();
1580-
try {
1581-
return true;
1582-
} finally {
1583-
gil.release(mustRelease);
1584-
}
1543+
@Cached("createBinaryProfile()") ConditionProfile stringProfile) {
1544+
return true;
15851545
}
15861546

15871547
static boolean isPyTimeMemberReadable(PythonObjectNativeWrapper receiver, PythonNativeWrapperLibrary lib, PythonObjectLibrary plib, GetNameNode getNameNode) {
@@ -1593,28 +1553,18 @@ static boolean isPyTimeMemberReadable(PythonObjectNativeWrapper receiver, Python
15931553
static boolean isReadablePyTime(PythonObjectNativeWrapper receiver, String name,
15941554
@CachedLibrary("receiver") PythonNativeWrapperLibrary lib,
15951555
@CachedLibrary(limit = "4") PythonObjectLibrary plib,
1596-
@Cached GetNameNode getNameNode, @Exclusive @Cached GilNode gil) {
1597-
boolean mustRelease = gil.acquire();
1598-
try {
1599-
return true;
1600-
} finally {
1601-
gil.release(mustRelease);
1602-
}
1556+
@Cached GetNameNode getNameNode) {
1557+
return true;
16031558
}
16041559

16051560
@Specialization
16061561
@TruffleBoundary
16071562
static boolean isReadableFallback(PythonObjectNativeWrapper receiver, String name,
16081563
@CachedLibrary("receiver") PythonNativeWrapperLibrary lib,
16091564
@CachedLibrary(limit = "4") PythonObjectLibrary plib,
1610-
@Cached GetNameNode getNameNode, @Exclusive @Cached GilNode gil) {
1611-
boolean mustRelease = gil.acquire();
1612-
try {
1613-
return DynamicObjectNativeWrapper.GP_OBJECT.equals(name) || NativeMember.isValid(name) ||
1565+
@Cached GetNameNode getNameNode) {
1566+
return DynamicObjectNativeWrapper.GP_OBJECT.equals(name) || NativeMember.isValid(name) ||
16141567
ReadObjectNativeMemberNode.isPyDateTimeCAPIType(getNameNode.execute(plib.getLazyPythonClass(lib.getDelegate(receiver))));
1615-
} finally {
1616-
gil.release(mustRelease);
1617-
}
16181568
}
16191569
}
16201570

@@ -1627,13 +1577,8 @@ abstract static class IsMemberModifiable {
16271577
static boolean isModifiableCached(PythonObjectNativeWrapper receiver, String name,
16281578
@Cached("createBinaryProfile()") ConditionProfile stringProfile,
16291579
@Cached(value = "name", allowUncached = true) String cachedName,
1630-
@Cached(value = "isValid(name)", allowUncached = true) boolean isValid, @Exclusive @Cached GilNode gil) {
1631-
boolean mustRelease = gil.acquire();
1632-
try {
1633-
return isValid;
1634-
} finally {
1635-
gil.release(mustRelease);
1636-
}
1580+
@Cached(value = "isValid(name)", allowUncached = true) boolean isValid) {
1581+
return isValid;
16371582
}
16381583
}
16391584
}
@@ -1896,73 +1841,48 @@ protected static boolean isObType(String key) {
18961841

18971842
@ExportMessage
18981843
@TruffleBoundary
1899-
int identityHashCode(@Exclusive @Cached GilNode gil) {
1900-
boolean mustRelease = gil.acquire();
1901-
try {
1902-
int val = Byte.hashCode(state) ^ Long.hashCode(value);
1903-
if (Double.isNaN(dvalue)) {
1904-
return val;
1905-
} else {
1906-
return val ^ Double.hashCode(dvalue);
1907-
}
1908-
} finally {
1909-
gil.release(mustRelease);
1844+
int identityHashCode() {
1845+
int val = Byte.hashCode(state) ^ Long.hashCode(value);
1846+
if (Double.isNaN(dvalue)) {
1847+
return val;
1848+
} else {
1849+
return val ^ Double.hashCode(dvalue);
19101850
}
19111851
}
19121852

19131853
@ExportMessage
1914-
TriState isIdenticalOrUndefined(Object obj, @Exclusive @Cached GilNode gil) {
1915-
boolean mustRelease = gil.acquire();
1916-
try {
1917-
if (obj instanceof PrimitiveNativeWrapper) {
1918-
// This basically emulates singletons for boxed values. However, we need to do
1919-
// so to
1920-
// preserve the invariant that storing an object into a list and getting it out
1921-
// (in
1922-
// the same critical region) returns the same object.
1923-
PrimitiveNativeWrapper other = (PrimitiveNativeWrapper) obj;
1924-
return TriState.valueOf(other.state == state && other.value == value &&
1925-
(other.dvalue == dvalue || Double.isNaN(dvalue) && Double.isNaN(other.dvalue)));
1926-
} else {
1927-
return TriState.UNDEFINED;
1928-
}
1929-
} finally {
1930-
gil.release(mustRelease);
1854+
TriState isIdenticalOrUndefined(Object obj) {
1855+
if (obj instanceof PrimitiveNativeWrapper) {
1856+
// This basically emulates singletons for boxed values. However, we need to do
1857+
// so to
1858+
// preserve the invariant that storing an object into a list and getting it out
1859+
// (in
1860+
// the same critical region) returns the same object.
1861+
PrimitiveNativeWrapper other = (PrimitiveNativeWrapper) obj;
1862+
return TriState.valueOf(other.state == state && other.value == value &&
1863+
(other.dvalue == dvalue || Double.isNaN(dvalue) && Double.isNaN(other.dvalue)));
1864+
} else {
1865+
return TriState.UNDEFINED;
19311866
}
19321867
}
19331868
}
19341869

19351870
@ExportMessage
19361871
protected boolean isPointer(
1937-
@Cached IsPointerNode pIsPointerNode, @Exclusive @Cached GilNode gil) {
1938-
boolean mustRelease = gil.acquire();
1939-
try {
1940-
return pIsPointerNode.execute(this);
1941-
} finally {
1942-
gil.release(mustRelease);
1943-
}
1872+
@Cached IsPointerNode pIsPointerNode) {
1873+
return pIsPointerNode.execute(this);
19441874
}
19451875

19461876
@ExportMessage
19471877
protected long asPointer(
1948-
@Cached PAsPointerNode pAsPointerNode, @Exclusive @Cached GilNode gil) {
1949-
boolean mustRelease = gil.acquire();
1950-
try {
1951-
return pAsPointerNode.execute(this);
1952-
} finally {
1953-
gil.release(mustRelease);
1954-
}
1878+
@Cached PAsPointerNode pAsPointerNode) {
1879+
return pAsPointerNode.execute(this);
19551880
}
19561881

19571882
@ExportMessage
19581883
protected void toNative(
1959-
@Cached ToNativeNode toNativeNode, @Exclusive @Cached GilNode gil) {
1960-
boolean mustRelease = gil.acquire();
1961-
try {
1962-
toNativeNode.execute(this);
1963-
} finally {
1964-
gil.release(mustRelease);
1965-
}
1884+
@Cached ToNativeNode toNativeNode) {
1885+
toNativeNode.execute(this);
19661886
}
19671887

19681888
@ExportMessage
@@ -1972,12 +1892,7 @@ protected boolean hasNativeType() {
19721892

19731893
@ExportMessage
19741894
protected Object getNativeType(
1975-
@Cached PGetDynamicTypeNode getDynamicTypeNode, @Exclusive @Cached GilNode gil) {
1976-
boolean mustRelease = gil.acquire();
1977-
try {
1978-
return getDynamicTypeNode.execute(this);
1979-
} finally {
1980-
gil.release(mustRelease);
1981-
}
1895+
@Cached PGetDynamicTypeNode getDynamicTypeNode) {
1896+
return getDynamicTypeNode.execute(this);
19821897
}
19831898
}

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41+
// skip GIL
4142
package com.oracle.graal.python.builtins.objects.cext.capi;
4243

4344
import com.oracle.graal.python.PythonLanguage;
44-
import com.oracle.graal.python.runtime.GilNode;
4545
import com.oracle.truffle.api.Assumption;
4646
import com.oracle.truffle.api.CompilerAsserts;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848
import com.oracle.truffle.api.Truffle;
49-
import com.oracle.truffle.api.dsl.Bind;
5049
import com.oracle.truffle.api.dsl.Cached;
51-
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5250
import com.oracle.truffle.api.dsl.Specialization;
5351
import com.oracle.truffle.api.interop.TruffleObject;
5452
import com.oracle.truffle.api.library.ExportLibrary;
@@ -183,32 +181,21 @@ public void setNativePointer(Object nativePointer) {
183181
protected static class IsNative {
184182
@Specialization(guards = {"cachedWrapper == wrapper", "nativePointer != null"}, assumptions = {"singleContextAssumption()", "cachedAssumption"})
185183
protected static boolean isCachedNative(@SuppressWarnings("unused") PythonNativeWrapper wrapper,
186-
@Exclusive @Cached GilNode gil,
187-
@Bind("gil.acquire()") boolean mustRelease,
188184
@SuppressWarnings("unused") @Cached(value = "wrapper", weak = true) PythonNativeWrapper cachedWrapper,
189185
@SuppressWarnings("unused") @Cached("wrapper.getHandleValidAssumption()") Assumption cachedAssumption,
190186
@SuppressWarnings("unused") @Cached(value = "wrapper.getNativePointerPrivate()", weak = true) Object nativePointer) {
191-
try {
192-
return true;
193-
} finally {
194-
gil.release(mustRelease);
195-
}
187+
return true;
196188
}
197189

198190
@Specialization(replaces = "isCachedNative")
199191
protected static boolean isNative(PythonNativeWrapper wrapper,
200-
@Cached ConditionProfile hasNativePointerProfile, @Exclusive @Cached GilNode gil) {
201-
boolean mustRelease = gil.acquire();
202-
try {
203-
if (hasNativePointerProfile.profile(wrapper.nativePointer != null)) {
204-
Assumption handleValidAssumption = wrapper.getHandleValidAssumption();
205-
// If an assumption exists, it must be valid
206-
return handleValidAssumption == null || isValid(handleValidAssumption);
207-
}
208-
return false;
209-
} finally {
210-
gil.release(mustRelease);
192+
@Cached ConditionProfile hasNativePointerProfile) {
193+
if (hasNativePointerProfile.profile(wrapper.nativePointer != null)) {
194+
Assumption handleValidAssumption = wrapper.getHandleValidAssumption();
195+
// If an assumption exists, it must be valid
196+
return handleValidAssumption == null || isValid(handleValidAssumption);
211197
}
198+
return false;
212199
}
213200
}
214201
}

0 commit comments

Comments
 (0)