Comments on: MonoTorrent and advanced C# http://jrwren.wrenfam.com/blog/2006/11/26/monotorrent-and-advanced-c/ babblings of a computer loving fool Mon, 21 Nov 2016 19:37:12 +0000 hourly 1 https://wordpress.org/?v=4.7.2 By: john http://jrwren.wrenfam.com/blog/2006/11/26/monotorrent-and-advanced-c/comment-page-1/#comment-10717 Tue, 05 Dec 2006 01:02:19 +0000 http://little.xmtp.net/blog/2006/11/26/monotorrent-and-advanced-c/#comment-10717 There’s one or two flaws in your argument there (but bear in mind i agree 100% that GC.Collect() should rarely (if ever) get called).

Firstly, I think the original complaint was that the biggest allocations were from methods internal to the framework and so there’s no chance of changing those methods to decrease allocations. With sockets, you pretty much have to use Async IO for high performance IO involving a lot of active connections. It lets you use IOCP under windows which is a pretty damn good reason to not use the classic Socket.Select way of doing things.

Secondly, stackalloc can’t be used on classes as you correctly pointed out, it can only be used on primitive types *and* on structs. Now, you say you noticed no difference between stackalloc’ing a struct and just declaring it, that’d be right. There is no difference. Structs are allocated on the stack, not the heap. Classes are allocated on the heap. However, an array of structs is allocated on the heap (as all arrays derive from System.Array or Array). If you want to see the benefits of stackalloc, change the code above to the following: MyObject *pa = stackalloc MyObject[100]; and compare it to MyObject[] pa = new MyObject[100]; called 100,000 times.

]]>