Below is a few lines from profiling a new quick start application.  All
I did was create an app via quick start, modified the startup file to
enable profiling, started the server, viewed the home page of the app,
and then hit the browser refresh button 6 times.

The line that starts with 23118 number of calls was the first item that
jumped out to me for investigation.  So I opened the code (listed
next).  Now it seams a little absurd that it would be necessary to
retrieve sys.modules 23118 times (takes 0.2 seconds) when TG starts up.
 The reloader_thread function monkey patches CherryPy and I have to be
honest I didn't look into this particular issue any further as I
started to look through other areas.

Any way after looking into other areas I felt I needed to dig in a
little deeper into both TG and CherryPy to get a better understanding
of the internals before trying to make optimizations but it was clear
that many opportunities seam to exist.


def reloader_thread(freq):
    """Monkeypatch for the reloader provided by CherryPy.

    This reloader is designed to reload a single package. This is
    more efficient and, more important, compatible with zipped
    libraries that may not provide access to the individual files."""

    def archive_selector(module):
        if hasattr(module, '__loader__'):
            if hasattr(module.__loader__, 'archive'):
                return module.__loader__.archive
        return module

    mtimes = {}
    package = turbogears.config.get("autoreload.package", None)
    if package is None:
        print \
"""TurboGears requires autoreload.package to be set. It can be an empty
value, which will use CherryPy's default behavior which is to check
every module. Setting an actual package makes the check much faster."""
        return
    while cherrypy.lib.autoreload.RUN_RELOADER:
        if package:
            modnames = filter(lambda modname:
modname.startswith(package), sys.modules.keys())
            modlist = [sys.modules[modname] for modname in modnames]
        else:
            modlist = map( archive_selector, sys.modules.values())
        for filename in filter(lambda v: v, map(lambda m: getattr(m,
"__file__", None), modlist)): # <= line 49
            if filename.endswith(".kid") or filename == "<string>":
                continue
            orig_filename = filename
            if filename.endswith(".pyc"):
                filename = filename[:-1]
            try:
                mtime = os.stat(filename).st_mtime
            except OSError, e:
                if orig_filename.endswith('.pyc') and e[0] == errno.ENOENT:
                        # This prevents us from endlessly restarting if there 
is an old .pyc
lying around
                        # after a .py file has been deleted
                        try: os.unlink(orig_filename)
                        except: pass
                sys.exit(3) # force reload
            if filename not in mtimes:
                mtimes[filename] = mtime
                continue
            if mtime > mtimes[filename]:
                sys.exit(3) # force reload
        time.sleep(freq)




==Begin Profile Data==
         286628 function calls (280258 primitive calls) in 12.944 CPU
seconds



   Ordered by: internal time, call count



   ncalls  tottime  percall  cumtime  percall filename:lineno(function)

       93    2.668    0.029    3.179    0.034 <string>:1(?)

    86482    1.269    0.000    2.226    0.000
\trentm\as\apps\activepython-devel\build\py2_4-win32-ix86\python\modules\pyexpat.c:849(Default)

       49    1.077    0.022    3.311    0.068
g:\python24\lib\site-packages\kid-0.9.1-py2.4.egg\kid\pull.py:318(_buildForeign)

    86482    0.958    0.000    0.958    0.000
g:\python24\lib\site-packages\kid-0.9.1-py2.4.egg\kid\pull.py:434(_default)

  702/132    0.237    0.000    0.583    0.004
g:\python24\lib\sre_parse.py:374(_parse)

       48    0.230    0.005    0.230    0.005
g:\python24\lib\site-packages\kid-0.9.1-py2.4.egg\kid\template_util.py:2(?)

       48    0.224    0.005    0.541    0.011
g:\python24\lib\site-packages\kid-0.9.1-py2.4.egg\kid\__init__.py:9(?)

    23118    0.202    0.000    0.202    0.000
g:\python24\lib\site-packages\turbogears-1.0b1-py2.4.egg\turbogears\startup.py:49(<lambda>)
==End Profile Data==


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to