Skip to content

Commit edfd75a

Browse files
Optimisation (#127)
1 parent 22884df commit edfd75a

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ and then only convert them to C data structures when you have to call the C func
161161
3. The raylib.* functions are potentially 1.5x faster than the pyray.* equivalents, so if you need a tiny bit more performance
162162
you can switch your inner loop functions to these.
163163

164+
4. There is a version of Python that is faster than Pypy: GraalPy. However it's not fully compatible with all Python
165+
packages. It doesn't work with CFFI and so doesn't work with this binding. But it *is* compatible with the
166+
*Java* binding, Jaylib! There is an example of this here: https://github.com/electronstudio/megabunny/tree/master/raylib-python-jaylib
167+
164168
## Bunnymark
165169

166170

@@ -175,6 +179,8 @@ you can switch your inner loop functions to these.
175179
| Raylib Python CFFI 3.7 | Python 3.9 Nuitka | 8600 | 5.1% |
176180
| Raylib Python CFFI 3.7 Dynamic | Python 3.9 | 6300 | 3.7% |
177181

182+
See also https://github.com/electronstudio/megabunny/
183+
178184
# Packaging your app
179185

180186
You can create a standalone binary using the Nuitka compiler. For example, here is how to package Bunnymark:

pyray/__init__.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,46 @@ def pointer(struct):
5151
# Another way to improve performance might be to special-case simple types before doing the string comparisons
5252

5353
def _wrap_function(original_func):
54+
c_args = [str(x) for x in ffi.typeof(original_func).args]
55+
number_of_args=len(c_args)
56+
c_arg_is_pointer = [x.kind == 'pointer' for x in ffi.typeof(original_func).args]
57+
c_arg_is_string = [str(x) == "<ctype 'char *'>" for x in ffi.typeof(original_func).args]
5458
# print("makefunc ",a, ffi.typeof(a).args)
5559
def wrapped_func(*args):
56-
modified_args = []
57-
for (c_arg, arg) in zip(ffi.typeof(original_func).args, args):
58-
# print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
59-
if c_arg.kind == 'pointer':
60-
if type(arg) is str:
61-
arg = arg.encode('utf-8')
62-
# if c_arg is a 'char *' not a 'const char *' then we ought to raise here because its an out
63-
# parameter and user should supply a ctype pointer, but cffi cant detect const
60+
args=list(args) # tuple is immutable, converting it to mutable list is faster than constructing new list!
61+
for i in range(0, number_of_args):
62+
try:
63+
arg=args[i]
64+
except IndexError:
65+
raise RuntimeError(f"function requires {number_of_args} arguments but you supplied {len(args)}")
66+
if c_arg_is_pointer[i]:
67+
if c_arg_is_string[i]: # we assume c_arg is 'const char *'
68+
try: # if it's a 'char *' then user should be supplying a ctype pointer, not a Python string
69+
args[i] = arg.encode('utf-8') # in that case this conversion will fail
70+
except AttributeError: # but those functions are uncommon, so quicker on average to try the conversion
71+
pass # and ignore the exception
72+
# if user supplied a Python string but c_arg is a 'char *' not a 'const char *' then we ought to raise
73+
# exception because its an out
74+
# parameter and user should supply a ctype pointer, but we cant because cffi cant detect 'const'
6475
# so we would have to get the info from raylib.json
65-
elif type(arg) is list and str(c_arg) == "<ctype 'char * *'>":
66-
arg = [ffi.new("char[]", x.encode('utf-8')) for x in arg]
76+
elif c_args[i] == "<ctype 'char * *'>" and type(arg) is list:
77+
args[i] = [ffi.new("char[]", x.encode('utf-8')) for x in arg]
6778
elif is_cdata(arg) and "*" not in str(arg):
68-
arg = ffi.addressof(arg)
79+
args[i] = ffi.addressof(arg)
6980
elif arg is None:
70-
arg = ffi.NULL
81+
args[i] = ffi.NULL
7182
elif not is_cdata(arg):
72-
if str(c_arg) == "<ctype '_Bool *'>":
83+
if c_args[i] == "<ctype '_Bool *'>":
7384
raise TypeError(
7485
"Argument must be a ctype bool, please create one with: pyray.ffi.new('bool *', True)")
75-
elif str(c_arg) == "<ctype 'int *'>":
86+
elif c_args[i] == "<ctype 'int *'>":
7687
raise TypeError(
7788
"Argument must be a ctype int, please create one with: pyray.ffi.new('int *', 1)")
78-
elif str(c_arg) == "<ctype 'float *'>":
89+
elif c_args[i] == "<ctype 'float *'>":
7990
raise TypeError(
8091
"Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)")
81-
modified_args.append(arg)
82-
result = original_func(*modified_args)
92+
93+
result = original_func(*args)
8394
if result is None:
8495
return
8596
elif is_cdata(result) and str(result).startswith("<cdata 'char *'"):

0 commit comments

Comments
 (0)