473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1298
On May 23, 1:05 pm, Gordon <gordon.mc...@ntlworld.comwrote:
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.dewrote:
.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
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...
3
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...
11
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*...
9
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...
14
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...
93
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...
206
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...
1
by: kasthurirangan.balaji | last post by:
Hi, Below is the code. #include <fstream> #include <set> #include <string> #include <iterator> #include <algorithm>
20
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.