<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/1.5.1.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Object pooling, part 4</title>
	<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/</link>
	<description>General musings and programming stuff</description>
	<pubDate>Sat, 22 Nov 2008 04:10:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=1.5.1.3</generator>

	<item>
		<title>by: Oliver Sturm&#8217;s weblog - Object pooling, part 6 - growing and shrinking</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3181</link>
		<pubDate>Thu, 22 Sep 2005 10:47:47 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3181</guid>
					<description>[...] This is the sixth article in my mini series about object pooling. Be sure to read part 1, part 2, part 3, part 4 and part 5 first. [...]</description>
		<content:encoded><![CDATA[	<p>[&#8230;] This is the sixth article in my mini series about object pooling. Be sure to read part 1, part 2, part 3, part 4 and part 5 first. [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Oliver Sturm</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3046</link>
		<pubDate>Tue, 13 Sep 2005 09:14:40 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3046</guid>
					<description>I still don't get why &quot;the functionality that Invoice represents&quot; should be executed on the main thread. Who starts that code in the main thread? There's got to be a caller, hasn't there? When the call to that functionality comes from the request thread, why would the code be executed on the main thread?

Even if the Invoice object had some functionality where code is being executed without a caller, that would mean the Invoice object would have to run that code itself. When does it do that, in its constructor? Maybe that's what you mean... in that case you should simply make the Invoice object start its own thread for its own code, so that it doesn't block the thread in which it is constructed - but that's still not the main thread most of the time; it should be the thread where the new object gets constructed, which would depend on the growing mechanism(s) you have in your pool.</description>
		<content:encoded><![CDATA[	<p>I still don&#8217;t get why &#8220;the functionality that Invoice represents&#8221; should be executed on the main thread. Who starts that code in the main thread? There&#8217;s got to be a caller, hasn&#8217;t there? When the call to that functionality comes from the request thread, why would the code be executed on the main thread?</p>
	<p>Even if the Invoice object had some functionality where code is being executed without a caller, that would mean the Invoice object would have to run that code itself. When does it do that, in its constructor? Maybe that&#8217;s what you mean&#8230; in that case you should simply make the Invoice object start its own thread for its own code, so that it doesn&#8217;t block the thread in which it is constructed - but that&#8217;s still not the main thread most of the time; it should be the thread where the new object gets constructed, which would depend on the growing mechanism(s) you have in your pool.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Shawn B.</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3045</link>
		<pubDate>Mon, 12 Sep 2005 22:54:32 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3045</guid>
					<description>I hope I'm correct with my assessment on ASP.NET threading issues... its been a while, I'll go double-check when I get home tonight.

Thanks,
Shawn</description>
		<content:encoded><![CDATA[	<p>I hope I&#8217;m correct with my assessment on ASP.NET threading issues&#8230; its been a while, I&#8217;ll go double-check when I get home tonight.</p>
	<p>Thanks,<br />
Shawn
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Shawn B.</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3044</link>
		<pubDate>Mon, 12 Sep 2005 22:49:26 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3044</guid>
					<description>The way my ObjectPool works... is it acts as a generic list like yours, but I derive a strongly typed pool, where InvoiceObjectPool : ObjectPool something like that.

Anyway, I have a method &quot;GetInstance()&quot; that returns an instance of type T (in List) (in this case, Invoice).  These are heavy objects.

So... InvoiceObjectPool lives in Application[] of ASP.NET.  When my page request says:

Invoice = InvoiceObjectPool.GetInstance();

it is actually creating or reusing an instance that was created and resides on the same thread that Application[] resides in.

That said, all of the functionality that Invoice respresents, is actually being executed on the main thread, not the thread servicing the Request.  Thus, it is a bottleneck in a high load environment.

Using a ReaderWriterLock (or the lock() in your case) on the object pool assists with locking issues for creating/removing items from the pool (in ASP.NET) but not which thread the object in the pool executes in (it is not actually executing on the working/Request thread).

Hope that clarifies.

Thanks,
Shawn</description>
		<content:encoded><![CDATA[	<p>The way my ObjectPool works&#8230; is it acts as a generic list like yours, but I derive a strongly typed pool, where InvoiceObjectPool : ObjectPool something like that.</p>
	<p>Anyway, I have a method &#8220;GetInstance()&#8221; that returns an instance of type T (in List) (in this case, Invoice).  These are heavy objects.</p>
	<p>So&#8230; InvoiceObjectPool lives in Application[] of ASP.NET.  When my page request says:</p>
	<p>Invoice = InvoiceObjectPool.GetInstance();</p>
	<p>it is actually creating or reusing an instance that was created and resides on the same thread that Application[] resides in.</p>
	<p>That said, all of the functionality that Invoice respresents, is actually being executed on the main thread, not the thread servicing the Request.  Thus, it is a bottleneck in a high load environment.</p>
	<p>Using a ReaderWriterLock (or the lock() in your case) on the object pool assists with locking issues for creating/removing items from the pool (in ASP.NET) but not which thread the object in the pool executes in (it is not actually executing on the working/Request thread).</p>
	<p>Hope that clarifies.</p>
	<p>Thanks,<br />
Shawn
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Oliver Sturm</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3027</link>
		<pubDate>Mon, 12 Sep 2005 19:04:03 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3027</guid>
					<description>I'm not specifically an expert on ASP.NET, so maybe I'm missing something. But I don't see why you say &quot;... but will execute the object on the same thread as the Application/Cache space ...&quot;. The way I see it, you'll need one instance of the pool, for which the application/cache space is probably a good place. But I wouldn't say that this pool object runs on a particular thread, because it doesn't really do anything on its own at all, does it? It just sits there, waiting for requests.

Now, when a request comes in in the form of a call to GetObject(), the code in the GetObject() method is executed in the thread of the caller, which is why I protect access to the management structures from that code with a lock. 

Finally, when an object is returned to the calling object by the GetObject() method, the caller will presumably &quot;use&quot; that object for whatever it's intended to be used for. Any executions of code in the pooled object will then occur in the thread that the caller is running in, be that a Page's thread or whatever else. 

If you look at the origin of the objects in the pool, you might find that they were all created on very different threads - the first few might have been created when the pool was created, any number of others were possibly created when the pool ran dry within a GetObject() call, so this would have happened on the thread of the caller that just happened to call at that moment. But an object is just a piece of data, it doesn't &quot;belong&quot; to a specific thread, so there shouldn't be a problem as long as the object is only ever used by one caller at a time - something that the pool should guarantee, as well as it can.

So, maybe these explanations help you? If they don't, maybe I'm missing something that's specific to ASP.NET - or could you explain the problem you're seeing in more detail?</description>
		<content:encoded><![CDATA[	<p>I&#8217;m not specifically an expert on ASP.NET, so maybe I&#8217;m missing something. But I don&#8217;t see why you say &#8220;&#8230; but will execute the object on the same thread as the Application/Cache space &#8230;&#8221;. The way I see it, you&#8217;ll need one instance of the pool, for which the application/cache space is probably a good place. But I wouldn&#8217;t say that this pool object runs on a particular thread, because it doesn&#8217;t really do anything on its own at all, does it? It just sits there, waiting for requests.</p>
	<p>Now, when a request comes in in the form of a call to GetObject(), the code in the GetObject() method is executed in the thread of the caller, which is why I protect access to the management structures from that code with a lock. </p>
	<p>Finally, when an object is returned to the calling object by the GetObject() method, the caller will presumably &#8220;use&#8221; that object for whatever it&#8217;s intended to be used for. Any executions of code in the pooled object will then occur in the thread that the caller is running in, be that a Page&#8217;s thread or whatever else. </p>
	<p>If you look at the origin of the objects in the pool, you might find that they were all created on very different threads - the first few might have been created when the pool was created, any number of others were possibly created when the pool ran dry within a GetObject() call, so this would have happened on the thread of the caller that just happened to call at that moment. But an object is just a piece of data, it doesn&#8217;t &#8220;belong&#8221; to a specific thread, so there shouldn&#8217;t be a problem as long as the object is only ever used by one caller at a time - something that the pool should guarantee, as well as it can.</p>
	<p>So, maybe these explanations help you? If they don&#8217;t, maybe I&#8217;m missing something that&#8217;s specific to ASP.NET - or could you explain the problem you&#8217;re seeing in more detail?
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Shawn B.</title>
		<link>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3022</link>
		<pubDate>Mon, 12 Sep 2005 17:59:16 +0000</pubDate>
		<guid>http://www.sturmnet.org/blog/archives/2005/09/12/object-pooling4/#comment-3022</guid>
					<description>Interesting set of articles.  I created an object pool a while back (about 9 months ago) for .NET 2.0.  Architecturally, we have very similar approaches (except naming conventions).  But one problem I haven't solved yet (admittedly, because I haven't tried), is making it work in ASP.NET where it actually counts for all of my scenarios.

When I put a pool in the Application (or Cache) space in ASP.NET, each page is contending for an object from the pool, but will execute the object on the same thread as the Application/Cache space (which is not the same thread as the page).  Needless to say, performance wasn't all that rockin'.

The only way I know of to solve that is that have a custom thread pool (so as not to starve ASP.NET by using the default ThreadPool) and have each instance reside on its own thread.  What's your take?

Thanks,
Shawn</description>
		<content:encoded><![CDATA[	<p>Interesting set of articles.  I created an object pool a while back (about 9 months ago) for .NET 2.0.  Architecturally, we have very similar approaches (except naming conventions).  But one problem I haven&#8217;t solved yet (admittedly, because I haven&#8217;t tried), is making it work in ASP.NET where it actually counts for all of my scenarios.</p>
	<p>When I put a pool in the Application (or Cache) space in ASP.NET, each page is contending for an object from the pool, but will execute the object on the same thread as the Application/Cache space (which is not the same thread as the page).  Needless to say, performance wasn&#8217;t all that rockin&#8217;.</p>
	<p>The only way I know of to solve that is that have a custom thread pool (so as not to starve ASP.NET by using the default ThreadPool) and have each instance reside on its own thread.  What&#8217;s your take?</p>
	<p>Thanks,<br />
Shawn
</p>
]]></content:encoded>
				</item>
</channel>
</rss>
