Sunday, March 04, 2007

It's been a while since i sat down and profiled monotorrent, turns out there's not much that has adversely affected my memory optimisations that i implemented a while ago. Things are all still fine and dandy.

However one thing that i never liked were the large amount of allocations due to using the async sockets. It always annoyed me that the worst part of my code i had no control over. Well, it turns out that it isn't *just* the sockets causing those allocations, there's another thing i haven't looked at yet.

My timer!

I use a single timer in the library which fires about 20 times a second. This was originally needed as a way to make sure that certain events happened regularly. However, I've never liked this timer and i have been working to get rid of it. For example i removed the dependence of downloading pieces and piece picking from that timer (as much as is possible). Download/Upload speed monitoring is still dependant on the timer, so once i remove that dependence and the only other thing left is to make connecting to other peers independent of the timer. Currently i try to connect to a new peer every timer tick so long as i haven't reached the limit.

So, before i used to have an allocation graph that looks like this:

That big yellow bar is the allocations due to both the timer and the async sockets. There's a lot of execution contexts there :p After changing the timer interval to 1 second, i get the following allocation graph:

That tiny cyan coloured bar is the severely reduced amount of execution contexts being allocated. So before you all get to feel the benefits of that change, i have to modify a fair bit of code. Once thats done, i'll be releasing beta 3 which is (hopefully) a lot more stable than any previous version, and faster, and uses less ram, and uses less CPU and god knows what else ;)


Paul said...

Nicely Done! I've been profiling a lot of code this weekend as well, and it's always nice to make a find like that.

Ran 'chaosblade' Sagy said...

Nice stuff, Alan!
That reminds me, It's another skill i have to pick up - Profiling, I rarely do that. Do you use whatever comes with VS or other tools?

David said...

I'm using Beta 2 of your MonoTorrent client as part of a little gadget I'm writing to search on the web for specific files within torrents and download them.

Before I start the torrent, I set the priority for all the files except the one I'm interested to do not download. Does this work as expected? Because all of the other files are pre-allocated as well. While this is acceptable, it is kind of annoying. Would an easy fix be possible? (not pre-allocating?)

A little bit more urgent for me is that my school, while allowing peer-to-peer filesharing on paper, has largely blocked non-encrypted BitTorrent (I get a few kilobytes here and there). Thus, adding in encryption would be really, really useful. =)

I know you have limited time, so I might see about making these changes myself. Encryption might take a lot of work, though. I have no experience with the inner workings of the bittorrent protocol, so it will be difficult to tell if I'm doing the right thing. (although the document at for the encryption part looks clear enough)

Hit Counter