[pypy-issue] [issue1666] Calling lots of lambdas is slow

Simon Sapin tracker at bugs.pypy.org
Thu Jan 2 23:51:09 CET 2014


New submission from Simon Sapin <simon.sapin at kozea.fr>:

The program I’m working on "compiles" CSS selectors to boolean expressions in
Python code, creates lambdas with `eval('lambda element: ' + expression)`, and
calls them each on every HTML element of a document.

The last step is about 6 times slower on PyPy than CPython. Alex Gaynor says on
IRC that this sounds like the "chain of guards" issue.

The loops being timed are:

https://github.com/SimonSapin/cssselect2/blob/006f19f203/example.py#L38
https://github.com/SimonSapin/cssselect2/blob/006f19f203/cssselect2/__init__.py#L63

where `test` is a lambda.


To reproduce:

    pip install html5lib tinycss2  # In a virtualenv or something
    git clone https://github.com/SimonSapin/cssselect2.git
    cd cssselect2
    git checkout 006f19f20305f5d42173c6a7d063d273c1263058
    python example.py

The output is the number of selectors (and lambdas), then the results from
`timeit.repeat`. On my Linux x64 machine, with CPython 2.7.6:

368
1.351
1.337
1.344

With PyPy 2.2.1:

368
17.077
13.098
10.307

Still on PyPy, adding `repeat=10` to the `timeit.repeat` call to give the JIT
more warm-up time:

368
16.708
13.201
10.273
9.874
9.537
9.409
9.509
9.306
9.365
9.166

----------
messages: 6448
nosy: SimonSapin, pypy-issue
priority: performance bug
status: unread
title: Calling lots of lambdas is slow

________________________________________
PyPy bug tracker <tracker at bugs.pypy.org>
<https://bugs.pypy.org/issue1666>
________________________________________


More information about the pypy-issue mailing list