views:

378

answers:

7

I have class that I believe should not be a singleton or static class. It has state, albeit state that could be shared by consumers. I like to stay away from singletons when there is shared state, but the argument I'm hearing is that I will reap performance benefits from only ever having 1 instance of the object exist at any given time.

In my particular case, there is not a lot of data associated with this object -- two dictionaries with (at max) 150 entries in each and the dictionary.

At what point--if at all--does the performance argument hold any merit?

FYI - I'm using .NET.

Thanks!

A: 

I don't think performance is a very strong argument, either for or against using the singleton pattern. It's a design issue, either it makes sense to use a singleton, or not:

If you need exactly one instance of the object, use a singleton.

If you need multiple instances, don't.

Greg Dean
If you need exactly one instance, declare exactly one instance. If declaring two instances would cause the world to come crashing down, make it a singleton.
jalf
@jalf - yea yea, we've all heard the signgleton as an anti-pattern point of view. My advice is assuming you don't have some absurd prejudice against singletons.
Greg Dean
+5  A: 

No. The performance argument does NOT hold any merit.

You should benchmark and confirm/identify a performance problem, before assuming you have one. 9 times out of 10 it won't be where you thought.

If a Singleton is required, it simply is.

Mitch Wheat
yea, smells like premature optimization.
Greg Dean
A singleton is almost never required though. Most arguments in favor of singletons don't hold any merit. ;)(The only case I can think of where you absolutely want to enforce the "only one instance" thing is in driver development and similar, mapping directly to hardware.
jalf
+2  A: 

The singleton pattern exists mostly to allow you to specify that you only ever want one instance of the class. Static classes are generally used to provide stateless behavior. What you are describing does not really seem to fit either category. I'd investigate using caching rather than a singleton pattern to improve the performance of the code. Of course, your cache may be a singleton, but in the case of a cache it would make sense.

Of course, if your object is a cache, then I've just talked myself into a circle.

tvanfosson
LOL "Of course, if your object is a cache, then I've just talked myself into a circle."
Greg Dean
+2  A: 

You shouldn't think about creating a Singleton (or a Static class) for performance reasons.

You either need to make it a Singleton by design, or you don't. If multiple instances of your class should exist and be different from each other, then you can't use a singleton.

Groo
A: 

If there is a large cost to construction, then you would probably be better suited to an Abstract Factory pattern that returns a common instance. Singletons have a bad reputation, particularly with the TDD crowd, but effectively AF and Singleton can do the same thing (AF can do more, but that's a different story).

Since you don't have a large cost, then it probably doesn't matter.

Mystere Man
A: 

Usually, a singleton inhibits performance, more than anything. It means you have to wrap the class in locks and synchronization in a multithreaded application. That could possibly have been avoided if singletons or globals had not been used.

So a singleton may be slower, but I can't think of any situation where it'd be faster.

If you want your application to share a single instance of an object, then pass references to that object around. That also leaves you flexiblity to change the design later, when it turns out that for caching or concurrency reasons, using multiple separate instances would actually have been faster.

The most important thing to remember about performance is that it's hard to predict. No matter what you guess your bottleneck is, you're almost certainly wrong. So the best way to ensure good performance is to make it easy to modify your program. Singletons work against you there. Once you have a singleton, it is almost impossible to remove. You're stuck with one universal instance of the class, even if multiple instances would have performed better, even if local variables instead of a global singleton would have improved cache usage.

jalf
Well nitpicking, a singleton can be faster if you don't need to follow more than one reference, but n reality that shouldn't actually affect anything.
Robert Gould
Why would it be faster? Because you have to copy one less reference per method call? I suppose, but we're basically talking about a single cycle then, and it may not even be that on today's CPU's. Or is there other overhead I haven't spotted?
jalf
passing references around is an ugly way of providing the flexibility you speak of. You can get the exact same flexibility out of a static CreateInstance method. It either returns a singleton (lazy initialized, if you so desire), or later you can change it to create as many instances as you want.
Greg Dean
Yep, that's another alternative. There are plenty of ways to avoid singletons and allow flexibility in the implementation details. But however you do it, as long as you stay away from singletons, you're probably not doing too badly. :)
jalf
+1  A: 

You should use a Singleton only in cases where there can conceptually only be one instance of an object, not to artificially restrict the developer to one instance. If there could possibly be two instances then it shouldn't be a Singleton.

If the former is the case, and there is state associated with the object, then a Singleton is useful if there is substantial cost associated with initialization and either a chance the class will never be used, or a reason to defer the initialization. In that case a Singleton is good. The alternative us a static class, and you should use it whenever the above doesn't apply.

DJClayworth