Armin Ronacher's Thoughts and Writings

The 1000% Speedup, or, the stdlib sucks

written on Sunday, March 1, 2009

I hate the stdlib. There! I said it. Why do I hate it? Cookie, cgi, urllib, n'uff said. Today I can add another module to the “I hate you” list.

I noticed that Werkzeug's SharedDataMiddleware was just delivering 50 requests a second. I tried around a bit but nothing changed. Until I removed the call to mimetypes.guess_type. Suddenly I got 500 requests the second instead of the previous 50.

How badly implemented can that function be I was asking myself. So I had a look:

def guess_type(url, strict=True):
    return guess_type(url, strict)

WTF? And then it hit me. init() was loading the mimetype database and monkey patching the module so that guess_type() was replaced by a new function. Actually a method of the mimetype database.

And I was importing guess_type as follows:

from mimetypes import guess_type

So on every call the database was reloaded. GNAAAaa. So check out If you're doing the same mistake. If yes, change it to the following import:

import mimetypes

And now I file a ticket in the Python bug tracker ticket filed. -.-

This entry was tagged python and rant