question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ideas for improving performance of function calls

See original GitHub issue

I saw that simple function calls in Brython take around 5x the time of CPython, so I looked into it a bit to see if there were some quick ways to speed it up. I found a few things that together led to a 10x speedup. I’m not sure if two of them are safe, so I’m just listing them here instead of making a PR.

  1. Add if(frame[1].$has_yield_in_cm){ test around the call to exit_ctx_managers_in_generators in $B.leave_frame_exec. This saves a ton of time (when you are running inside of an exec), and is analogous to the code in $B.leave_frame. Hopefully it is possible to do this, but I don’t know for sure.

  2. Every function body contains this line $locals.__annotations__ = _b_.dict.$factory(), but I don’t think it is possible to annotate local variables inside the function, so it is never used. Can it be removed?

  3. In $B.leave_frame, remove the call to $B.del_exc and add frame[1].$current_exception = undefined after var frame = $B.frames_stack.pop().

  4. In $B.enter_frame, return null everywhere you currently use _b_.None, and change the autogenerated trace code from if($locals.$f_trace !== _b_.None){$B.trace_line()} to if($locals.$f_trace){$B.trace_line()}. This didn’t make nearly as large a difference as the first three, but it’s a simple change that also reduces the size of the generated code, so it still seems worth doing.

Thanks for taking a look at these!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
schmavecommented, Jun 26, 2020

You can specify annotations on local variables, but I believe that they are not available at runtime (from PEP 526):

Store variable annotations also in function scope: The value of having the annotations available locally is just not enough to significantly offset the cost of creating and populating the dictionary on each function call.

1reaction
schmavecommented, Jun 26, 2020

Looks good to me! When I run the benchmark named “function call” using http://localhost:8000/speed on the master branch, it takes 1900-2000ms for 1,000,000 calls to f(). When I run it on the function_call_speed branch, it takes 150ms. I disabled caching in my browser and cleared local storage and indexed DB between runs.

In CPython, I see that __annotations__ inside a function seems to refer to the module level __annotations__, and I think this will continue to work in Brython:

x: int = 5
print(__annotations__)
def f():
  print('in f', __annotations__)

f()

def g():
   y: int = 7
   print('in g', __annotations__)
 
g()

CPython 3.8.0 output:

{'x': <class 'int'>}
in f {'x': <class 'int'>}
in g {'x': <class 'int'>}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Successfully improving function call performance - Bekk
We haven't had much luck in our attempts to improve the runtime performance of Elm's function calls, but there's still one thing left...
Read more >
Make your programs run faster: avoid function calls
Avoid recursive functions · Make sure your recursive function is tail recursive. This will allow compiler to tail-call optimizations on your ...
Read more >
24 Improving performance | Advanced R - Hadley Wickham
It's difficult to provide general advice on improving performance, but I try my best with four techniques that can be applied in many...
Read more >
Techniques to Improve Performance - MATLAB & Simulink
To speed up the performance of your code, there are several techniques that you can consider.
Read more >
What's the performance cost of a function call?
As a general rule, there's no need to worry about the performance impact of function calls or list iterations. That's because of three...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found