• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton object usage  RSS feed

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a situation whereby there are a number of classes to handle different scenario( for example download file from some host, creating a new file to specific location, rename of a file etc).

This is the sistuation
1. a number of rules are running on a near realtime basis and corresponding to each rule there is a class
2. Each class has some execute method (similar to what we do for Command pattern) and the execute method performs the specific task (for example rename)
3. The execute method has no global variables. All the details are passed in as argument and the method extracts value from the arguments.
4. There are a number of threads (say 10) which keeps running all the rules. While running the rule, based on some runtime parameter, thread determines which rule to run and creates an object of the rule and calls its execute.

I am planning to do some performance improvement and am planning to keep a cache of the rule object being created (kind of Singleton pattern whereby a rule object is created only once and can be used by multiple threads). This way there will be no need to create a specific rule object everytime before it gets executed (there are around 20-25 rules and each rule gets executed more than 10000 times on a day. Doing the caching will avoid object creation 10000 times)

Though I don't see any issue doing this , I am trying to understand if there is something more that I can do to do performance improvement. If someone can provide pointers , it will be helpful.

Regards,


 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote: (there are around 20-25 rules and each rule gets executed more than 10000 times on a day. Doing the caching will avoid object creation 10000 times)

I've no idea what's in these rule objects and thus how complex they are to create but before you go to all this trouble have you timed how long it takes to create your 25 x 10000 objects - You may be surprised at how little time it takes.
 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thanks for your reply. Object creation takes around 10-20 milli second but the class dsign will not allow my approach since there are some instance variables.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you use the words "singleton" and "threads" in the same sentence, you should probably take a step back and think if you *really* need what you want. You should try to avoid sharing state between threads.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote: . . . Object creation takes around 10-20 milli second . . .
My, that is slow. I would expect most objects to be created under 0.1μs, so are you sure you are timing that correctly?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . . You should try to avoid sharing state between threads.
Unless of course it is immutable.

I think this discussion is too difficult for “beginning”, so I shall move it.
 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
thanks a lot for your suggestion. I went ahead with object caching and saw an improvement of around 30% in the application (since the application has an uptime of 6 days -starting on Sunday morning and stopping at Saturday morning).

Regards,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!