dsc/bunch — pull request #5 by dsuch (2012-03-02 21:44:05+00:00)
Title: Added a SimpleBunch C extension type for speeding the most common operations up

Hi David,

care to take a look at this pull request?

The thing is that there are some core operations, the most common ones, that I'd like to speed up. If you compile the package now and run this gist for instance

https://gist.github.com/1961505

you'll notice that for what I consider the most commonly used access patterns, which is

```python
import bunch

d = {1:11, 2:22, 3:33}
sb = bunch.SimpleBunch(d)

sb.aa = 'bb'
'aa' in sb
del sb['aa']
```

well, the difference is an order of magnitude. bunch.SimpleBunch is about 10-15x faster than bunch.Bunch and some 20-30% slower than the plain dict.

There's a caveat though - the reason I'm calling it a 'simple' Bunch is that only \__getattr\__ and \__setattr\__ are implemented and even then, \__setattr\__ assumes everything should be stored as the SimpleBunch's keys, in other words - I haven't seriously played with the whole \__getattribute\__ machinery on C API level - might be a field for expansion to explore by future contributors and then it may be possible that bunch.Bunch will become a subclass of bunch.SimpleBunch, but that's future for now.

The whole thing is imported optionally, if the extension isn't available, bunch will define SimpleBunch as an alias to regular Bunch.

You might want to have a look at '_simple_bunch_systems' in setup.py and add some other systems that are likely to have a C compiler handy. This of course assumes a C compiler will be always available on any such a system but I hope this isn't that bad an assumption.

What do you think of it?

Cheers!