"import" inside a function or method should be cached

Bug #155076 reported by Carl Witty
2
Affects Status Importance Assigned to Milestone
Cython
Won't Fix
Wishlist
Unassigned

Bug Description

Compared to normal Cython code, import is very slow. And in a normal program, at every callsite, import will return the same object each time it is called.

It would be nice if Cython's import would cache the return value and just use the same value on subsequent calls. (This does reduce Python compatibility in case somebody is playing games with __builtins__.__import__, so I would suggest a command-line flag to restore the old behavior.)

Revision history for this message
Carl Witty (carl-witty) wrote :

On my system, an import seems to take about 2 microseconds. This isn't much, but I've recently been working on optimizing one particular chunk of Cython code, managing to bring it from 1.2 milliseconds to 500 microseconds. On this scale, every microsecond counts.

Here are my benchmark results:
sage: %timeit test1()
10 loops, best of 3: 20.2 ms per loop
sage: %timeit test2()
10000000 loops, best of 3: 111 ns per loop

test1() does 10,000 imports; test2() probably does nothing (I haven't checked to see if the compiler optimized away my 10,000-count empty loop, but it probably did).

The benchmark is attached to this comment.

Revision history for this message
scoder (scoder) wrote :

Python caches imports internally (sys.modules), so I don't see why Cython should add another level here. Besides, as you said, this introduces compatibility problems.

If you want to avoid redundant imports, you can always add a caching layer yourself, or move the import out of the critical path (as you probably would in Python, too). I think this is too special a problem to handle it inside Cython.

Changed in cython:
importance: Undecided → Wishlist
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.