[Python-Dev] xmlrpclib speed boost
Guido van Rossum
guido@python.org
Mon, 01 Oct 2001 14:54:34 -0400
> > This change may be deemed not to be the correct fix as far
> > backwards compatibility is concerned (it uses the "from m import x
> > as y" feature which was new with 2.0 I think). If someone alters
> > this fix, please don't put the import back into the functions that
> > call cgi.escape.
>
> Why not ? Moving the import to the top level just causes the slowdown to
> occur at a different moment. If this is really the problem, the slowdown
> should occur only the first time you use a particular function (unless you
> explicitly un-import cgi somehow ?)
>
> Importing cgi only in the functions that actually use it, in order to avoid
> the slowdown unless it's really necessary, sure seems like a sensible
> solution to me :-)
There's a cost to an import inside a function even if the module is
already imported. If the function is very small and heavily used (as
it apparently was in this case) the function can slow down
considerably.
It's kind of sad that the escape() function, which is only a few
lines, comes from the cgi module which is thousands of lines that are
not normally used except by actual CGI scripts.
--Guido van Rossum (home page: http://www.python.org/~guido/)