wingrunr21/gitolite — pull request #38 by jirutka (2012-12-30 13:03:27+00:00)
Title: Optimize performance

There’s big performance issue with saving changes in GitoliteAdmin. It takes seconds with hundreds SSH keys in the keydir (about 10 sec with 250 keys for me). The problem is that it’s always loading all keys from file system even when app doesn’t need them and then writing all the keys back to file system (very expensive operation).

I made two optimizations:
* Lazy load `ssh_keys` and `config` just when they’re needed, not during initialization. Don’t write them back when not loaded (i.e. changed).
* Implement “dirty checking” of keys in `ssh_keys` hash and don’t rewrite keys which hasn’t been changed since load from files. My DirtyProxy is not “bulletproof”, but it’s very simple, universal and good enough for arrays of value objects.

I also made SSHKey immutable. It’s not necessary\* but I highly recommend to make SSHKey immutable. If nothing else than because you’re using field `owner` as a hash key in `ssh_keys` so it should not be changed while being in hash under the `owner` as a hash key.

_\* Note that my current solution with DirtyProxy for Array doesn’t check changes in depth, i.e. inside values of the proxied object (array). However it’s very simply to rewrite it to work also with mutable SSHKey, so if you don’t like change of SSHKey to be immutable, please let me know and I’ll rewrite DirtyProxy._