Cause of decreasing speed?

Hi Folks,

TLDR: Why is my app so slow?

What I’m trying to do: I am working on my first Anvil web application and am hoping one of you more experienced developers might be able to point me towards the cause of slow performance. The app is simple in concept and consists of a number of sliders to rank pairs of options against one another (i.e., A is 2 times more important than B). The app uses a repeating element structure to pull the pairs of options from a data table. When the slider is changed, a dictionary of values is appended to a global list object. When the user submits the rankings at the end, the list of responses are pushed to a data table.

Clone link:
https://anvil.works/build#clone:5OKAX77ANUEC343M=WSJBVGQQMAX4YFUDHVYIAT2A
To access the sliders/submission button when running the app, you need to specify a user_id (from a user_id table). Try Foo or Bar for testing.

The problem: As I increase the number of pairs of options, the performance gets slower and slower, to the point where the app is unsuitable for use. Performance becomes unsuitable with even 5 pairs of options. I am hoping to have 25+ options. I believe the code appends the user’s selection data at each change of the slider, which is not quite what I want. However, even without repeatedly changing a given slider, the performance is terrible.

My suspicions: I don’t know what’s causing the decline in speed. Some ideas: (a) Perhaps appending records to a growing list object is just too slow. (b) I use the get_open_form() function to retrieve a value typed in by a user. Perhaps it’s not what I want. © I am trying to navigate global vs local variables, and may be trying to access/write to objects that are too costly to interact with. (d) Any of a million things I don’t know about python or application development.

There may be more efficient ways of doing what I am trying to do. However, I am just trying to make a crude prototype and would love any insights into ways to speed up what I already have in place. Many thanks for your thoughts.

Hello and welcome,

Here are some popular posts that will likely help to describe the types of things that would make an app run slowly (and proposed solutions).

I think it’s option d - it looks like you’re making a server call for every change event of the slider. Round trips to the server are something you want to reduce when optimising code and can be an expensive operation.

Appending to a python list is a very cheap operation. You’d only get performance problems if your list was orders of magnitude larger. Something like 2**32 in length. You’d likely hit an overflow error if you were exceeding this limit.

Navigating modules is also very cheap since python caches the module when it is first loaded. Getting attributes from a module is as fast as a dictionary lookup.

Similarly get_open_form is also very cheap.

When refactoring try to see if you can reduce your server calls to as few as possible. This will be mentioned in the link above.

Many thanks @alcampopiano and @stucork for your thoughts, and for taking the time to look at the code. I will explore the suggestions and return to flag an answer once I have confirmed that the changes have helped.

Update: Confirming that removing repeated server calls associated with my sliders, did the trick. I just needed to store a single (global) variable from one server call and use it in place of the additional server calls (yes, obvious in retrospect). My app is much faster now. Thank you again for the assistance.

1 Like