473,788 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization idea that might not be good OO

I'm using a factory pattern to generate objects, so I can check the
success of the operation in the factory, return the object on success
or return NULL on failure. The objects are abstractions of rows in a
database so a database lookup is done for each object created.

I noticed that running certain scripts could result in the same object
being created in the factory multiple times (ie the same database row
might be used to instantise an object more than once). This gave me
the idea that I could get better performance if I maintained a cache
of objects in the factory, if the requested item hasn't already been
requested then do the DB fetch and instantise as usual, if the item is
already in the cache return a reference to it.

Thinking more about it though, I have a feeling that this might just
be a Singleton pattern in disguise. There's plenty of documentation
out there on why singletons are a bad thing and how they're against
the spirit of OO. But the elimination of object creation overhead is
still quite tempting...

So basically I'm wondering if it's worth trying to implement the
object cache. I can see 3 possibilities:

Leave things as they are
Return references from a cache of items when the same item ID is
requested more than once (Eliminate object creation and DB lookup
overhead but might be a Singleton pattern)
Return clones from a cache of items when the same item ID is requested
more than once (Eliminate DB lookup overhead but still have object
creation overhead).

What would you guys recommend?
Jun 2 '08 #1
4 1315
On May 23, 1:05 pm, Gordon <gordon.mc...@n tlworld.comwrot e:
I'm using a factory pattern to generate objects, so I can check the
success of the operation in the factory, return the object on success
or return NULL on failure. The objects are abstractions of rows in a
database so a database lookup is done for each object created.

I noticed that running certain scripts could result in the same object
being created in the factory multiple times (ie the same database row
might be used to instantise an object more than once). This gave me
the idea that I could get better performance if I maintained a cache
of objects in the factory, if the requested item hasn't already been
requested then do the DB fetch and instantise as usual, if the item is
already in the cache return a reference to it.

Thinking more about it though, I have a feeling that this might just
be a Singleton pattern in disguise. There's plenty of documentation
out there on why singletons are a bad thing and how they're against
the spirit of OO. But the elimination of object creation overhead is
still quite tempting...

So basically I'm wondering if it's worth trying to implement the
object cache. I can see 3 possibilities:

Leave things as they are
Return references from a cache of items when the same item ID is
requested more than once (Eliminate object creation and DB lookup
overhead but might be a Singleton pattern)
Return clones from a cache of items when the same item ID is requested
more than once (Eliminate DB lookup overhead but still have object
creation overhead).

What would you guys recommend?
You're trading performance for code complexity.

If this is a stateless script (web request or message invoked - but
specifically short-lived) and you can determine a mapping without
going back to the database, then I'd say don't use a cache - keep a
array, indexed on the map, of all the factories progeny. This is a
much simpler proposal than a cache where you need to keep making space
for new objects once it fills.

C.
Jun 2 '08 #2
..oO(Gordon)
>I'm using a factory pattern to generate objects, so I can check the
success of the operation in the factory, return the object on success
or return NULL on failure. The objects are abstractions of rows in a
database so a database lookup is done for each object created.

I noticed that running certain scripts could result in the same object
being created in the factory multiple times (ie the same database row
might be used to instantise an object more than once). This gave me
the idea that I could get better performance if I maintained a cache
of objects in the factory, if the requested item hasn't already been
requested then do the DB fetch and instantise as usual, if the item is
already in the cache return a reference to it.

Thinking more about it though, I have a feeling that this might just
be a Singleton pattern in disguise. There's plenty of documentation
out there on why singletons are a bad thing and how they're against
the spirit of OO. But the elimination of object creation overhead is
still quite tempting...
Singletons are perfectly fine in many situations.

There's never a wrong tool, just wrong usage.
>So basically I'm wondering if it's worth trying to implement the
object cache. I can see 3 possibilities:

Leave things as they are
Return references from a cache of items when the same item ID is
requested more than once (Eliminate object creation and DB lookup
overhead but might be a Singleton pattern)
You don't need references here. PHP will handle that quite well on its
own (I assume we're talking about PHP 5 here, PHP 4 wouldn't make sense
anymore).
>Return clones from a cache of items when the same item ID is requested
more than once (Eliminate DB lookup overhead but still have object
creation overhead).

What would you guys recommend?
Let the factory create the instance if it doesn't exist yet and simply
return it without any explicit reference. The next time just return the
same object again. That's it.

Keep one thing in mind: If the object is modified somewhere "outside"
the factory, it will also affect the one "inside" the factory, because
internally it's always the same object. In such cases you might want to
clone the object, but only if really necessary.

Micha
Jun 2 '08 #3
Gordon wrote:
I'm using a factory pattern to generate objects, so I can check the
success of the operation in the factory, return the object on success
or return NULL on failure. The objects are abstractions of rows in a
database so a database lookup is done for each object created.
"Return NULL on failure" If it really is a failure (i.e. the record
asked for must exist), you can throw an Exception. If it is normal, ther
is something called a "Null object". This is an object that implements
the default actions that would normally be taken if null was returned.
Such a null object is not applicable in all circumstances, but it is
good to know that such a pattern exists.

Ik have though a lot about database code and some old thoughts (I know I
should update them) can be found at:

http://www.w-p.dds.nl/article/wrtabrec.htm
>
I noticed that running certain scripts could result in the same object
being created in the factory multiple times (ie the same database row
might be used to instantise an object more than once). This gave me
the idea that I could get better performance if I maintained a cache
of objects in the factory, if the requested item hasn't already been
requested then do the DB fetch and instantise as usual, if the item is
already in the cache return a reference to it.
This is called "laziness". I do that for most of my database work.
>
Thinking more about it though, I have a feeling that this might just
be a Singleton pattern in disguise. There's plenty of documentation
out there on why singletons are a bad thing and how they're against
the spirit of OO. But the elimination of object creation overhead is
still quite tempting...
I don't like Singletons at all, but that is a matter of taste. The
difference between a Singleton and an instance held by the Application
object is not that big. I prefer the second, but again this is a matter
of taste. If Singletons get in the way of unit testing, I have an "in
between" pattern to deal with it:

http://www.w-p.dds.nl/pathalfs.php
>
So basically I'm wondering if it's worth trying to implement the
object cache. I can see 3 possibilities:

Leave things as they are
Return references from a cache of items when the same item ID is
requested more than once (Eliminate object creation and DB lookup
overhead but might be a Singleton pattern)
Return clones from a cache of items when the same item ID is requested
more than once (Eliminate DB lookup overhead but still have object
creation overhead).

What would you guys recommend?
I usually prefer the reference variant. As Michael Fesser already said,
references come quite natural to PHP5, so you do not need the reference
operator.

Good luck!
Jun 2 '08 #4
On Fri, 23 May 2008 14:19:32 +0200, Michael Fesser <ne*****@gmx.de wrote:
.oO(Gordon)
>I'm using a factory pattern to generate objects, so I can check the
success of the operation in the factory, return the object on success
or return NULL on failure. The objects are abstractions of rows in a
database so a database lookup is done for each object created.

I noticed that running certain scripts could result in the same object
being created in the factory multiple times (ie the same database row
might be used to instantise an object more than once). This gave me
the idea that I could get better performance if I maintained a cache
of objects in the factory, if the requested item hasn't already been
requested then do the DB fetch and instantise as usual, if the item is
already in the cache return a reference to it.

Thinking more about it though, I have a feeling that this might just
be a Singleton pattern in disguise. There's plenty of documentation
out there on why singletons are a bad thing and how they're against
the spirit of OO. But the elimination of object creation overhead is
still quite tempting...

Singletons are perfectly fine in many situations.

There's never a wrong tool, just wrong usage.
>So basically I'm wondering if it's worth trying to implement the
object cache. I can see 3 possibilities:

Leave things as they are
Return references from a cache of items when the same item ID is
requested more than once (Eliminate object creation and DB lookup
overhead but might be a Singleton pattern)

You don't need references here. PHP will handle that quite well on its
own (I assume we're talking about PHP 5 here, PHP 4 wouldn't make sense
anymore).
>Return clones from a cache of items when the same item ID is requested
more than once (Eliminate DB lookup overhead but still have object
creation overhead).

What would you guys recommend?

Let the factory create the instance if it doesn't exist yet and simply
return it without any explicit reference. The next time just return the
same object again. That's it.

Keep one thing in mind: If the object is modified somewhere "outside"
the factory, it will also affect the one "inside" the factory, because
internally it's always the same object. In such cases you might want to
clone the object, but only if really necessary.
Well, actually, that's the reason I employ this kind of 'object maps'
sometimes: the fact that a particular object representing a particular
dataset only has one instance, and an alteration at one point in the code
shouldn't be reversed by another (overwritten, yes, but not unrelated
properties to the specific actions reversed to previous state). It is
something to keep in mind if you don't expect it though :).
--
Rik Wasmus
....spamrun finished
Jun 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2050
by: Carl | last post by:
I do a project using C# and C++ .NET and have some functionality to convert imperial to metric units and vice versa. In VC6 I just used a simple header file with some constants (e.g. const double MM_TO_INCH = 1.0/25.4;). For .NET I thought it might be a good idea to have a class with static methods for that to use in C# as well as C++ code. C# didn't work well for that because I could not overload methods if they only differ in types.
3
3334
by: PWalker | last post by:
Hi, I have written code that I would like to optimize. I need to push it to the limit interms of speed as the accuracy of results are proportional to runtime. First off, would anyone know any resources that explains how to optimize code i.e. give some rules on c++ optimization? e.g. using memcpy to copy an array (which i have done). Also, what is the best sorting algorithm out there for sorting an array of of size 100 or less? I have...
11
1782
by: Bonj | last post by:
When performance benchmark testing one of my own functions against the equivalent runtime function, I found that with /Ox on it optimized away its own function so that it didn't call it *at all* after the first loop. <psuedocode> #ifdef USE_MY_FUNC #define testfunc(x) myfunc(x) #else #define testfunc(x) runtimefunc(x) #endif QPC(start);
9
1538
by: Bob Bedford | last post by:
I've a question about generating pages for search engines. It's possible to detect a bot coming on a website and then show a complete other page for it ? My main page is mainly graphic, with a form. Quite no text at all. I would preffer instead to show a site description to bots ! Not spamming at all, just trying to tell to the bots that my site is not what people see and just a search engine, but a real site with real potential. How...
14
3148
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab compiler for the embedded target. My question is about how compilers optimize certain code sequences. As an example, take the code below. Will the compiler eliminate the actual function call to foo() in the object code generated and just store...
93
3687
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the source code and recompile) is a myth started 20-30 years ago when world consisted of UNIX systems. Well, world does not consist of UNIX systems anymore, but there are 100s of different systems running in cell-phones, DVD players, game consoles...
206
8374
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
1
1917
by: kasthurirangan.balaji | last post by:
Hi, Below is the code. #include <fstream> #include <set> #include <string> #include <iterator> #include <algorithm>
20
2359
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.