Optimizing Brython with Python standard library

57 views
Skip to first unread message

CJS Hayward

unread,
Nov 27, 2023, 3:27:41 PM11/27/23
to brython
I have a site at https://scrolls-of-wonder.com, and I originally implemented it in Brython with the Python standard library, implementation available at https://scrolls-of-wonder.com/index-original.html, and it takes several seconds to start. I migrated it to JavaScript as an optimization and it starts almost immediately. The Python source is available at https://scrolls-of-wonder.com/static/py/app.bpy and it is a short and simple application.

I noticed that the homepage at https://brython.info appears to load quickly and the source calls for Python's standard library to be included.

What optimizations would better allow me to load Brython with the standard library and have a page start more quickly?


Thanks,
C.J.S. Hayward

Denis Migdal

unread,
Nov 27, 2023, 4:01:21 PM11/27/23
to bry...@googlegroups.com
Hi,

Indeed, something doesn't seem right.
Ofc course some stuff aren't optimal (e.g. building the HTML from a string), but it shouldn't take so much time.

First, you can try the last version of Brython that came out today that'd reduce the loading time, but it likely won't solve this issue (you only win ~200ms for load time).

From what I see when profiling your website, there is a big cascade of imports, it might be that one of your dependencies has a big dependency tree ?
You import the module "re", without using it it seems (idem for pickle ?).
You use JQuery, that is a big dependency, maybe removing it could help (I assume it also generates lot of JS<=>Python conversions) ?
Try also to see if json/random is a big dependency or not.

Also don't use "open", but asynchronous AJAX queries to not block the page while waiting for the JSON files.
You can prefetch the images (in the header, link tags with preload ?).
Put your script in the header and add the attribute "defer" to start loading it sooner.

 if ('forward' in view['neighbors'] and view['neighbors']['forward'] !=
      view_number):
        forward_arrow = 'arrow_up.gif'
        forward_clickable = True
    else:
        forward_arrow = 'invalid_arrow_up.gif'
        forward_clickable = False

This is not a performance issue, but a style issue :

forward_clickable = bool( your_condition )
forward_arrow = f"{"" if forward_clickable else "invalid_"}arrow_up.gif"

Also, use a dict + loop.

for dir in ["forward", "right", ...]:

_isClickable = bool( condition )
    isClickable[dir] = _isClickable
    arrows[dir] = f"{"" if _isClickable else "invalid_"}arrow_{dir}.gif"

Then your 28 lines that has lot of commons can be written as 5 lines (4 if you don't use the temporary variable).


if direction == 'E': center = 'center_arrow_right.gif' alt='Facing East' elif direction == 'S': center = 'center_arrow_down.gif' alt='Facing South' elif direction == 'W': center = 'center_arrow_left.gif' alt='Facing West' elif direction == 'N': center = 'center_arrow_up.gif' alt='Facing North'

Use dicts to do the conversions, e.g. :
center = direction2img[direction]

For "myst_click" I think you might have better ways to handle it.



--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/ed429952-9117-417a-8562-80439895d09fn%40googlegroups.com.

Pierre Quentel

unread,
Nov 27, 2023, 4:29:12 PM11/27/23
to brython
Hi Christos,

I just tested the Brython version and it takes less than 2 seconds to load, both on Firefox and Chrome, with a moderately fast PC. How long does it take for you and for Denis ?

Thanks,
Pierre

Denis Migdal

unread,
Nov 27, 2023, 4:41:55 PM11/27/23
to bry...@googlegroups.com
1,92sec, likely due to a slow execution of app.bpy (it seems it takes lot of time doing imports).

Cf enclosed screenshots

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
Capture d’écran_2023-11-27_22-36-14.png
Screenshot 2023-11-27 at 22-40-57 Firefox 119 – Linux – 27_11_2023 21 40 10 UTC – Firefox Profiler.png

J M E

unread,
Nov 28, 2023, 3:38:30 PM11/28/23
to bry...@googlegroups.com
From the description I had expected a significant and obvious load time. On my iPhone 12s the original is operational within < 1s (timing guessed). The breakdown of potential bottlenecks is very useful and probably should be acted upon.
Sent from my iPhone

On 27 Nov 2023, at 21:41, Denis Migdal <denis....@gmail.com> wrote:


To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAFr00W4J%2BhHTg1H8xT39ee5K61ExeMwXmbHPRCv0ihM%3DUaMJ6A%40mail.gmail.com.
<Capture d’écran_2023-11-27_22-36-14.png>
<Screenshot 2023-11-27 at 22-40-57 Firefox 119 – Linux – 27_11_2023 21 40 10 UTC – Firefox Profiler.png>
Reply all
Reply to author
Forward
0 new messages