Cache class: Should Miss be an exception?

K

Kenneth Porter

I've got a system that needs to fetch settings from a remote device over
a communications link. I cache these in a Cache class. When reading a
setting, I first see if it's in the cache. If not, I fetch it from the
external device and then cache the result.

Should a cache miss be implemented as an exception or an extra return
value? What are the relative merits of the two implementions?

My current implementation uses an exception, and so far the only drawback
is that it generates a bunch of debugger noise in MS Visual Studio. But I
don't think VS is being reasonable in reporting every exception without
the ability to silence it. Is gdb subject to acting like this?

Here's an example of how one might use the Cache class, using both an
exception and return code scheme for reporting cache misses:

#if USE_MISS_EXCEPTION

double getRegister(unsigned registerIndex)
{
try {
return settingsCache.getSetting(registerIndex);
} catch (SettingsCache::Miss&) {
return getRegisterUncached(registerIndex);
}
}

#else

double getRegister(unsigned registerIndex)
{
double value;
const bool miss = settingsCache.getSetting(registerIndex, &value);
if (miss)
value = getRegisterUncached(registerIndex);
return value;
}

#endif
 
E

Erik Wikström

I've got a system that needs to fetch settings from a remote device over
a communications link. I cache these in a Cache class. When reading a
setting, I first see if it's in the cache. If not, I fetch it from the
external device and then cache the result.

Should a cache miss be implemented as an exception or an extra return
value? What are the relative merits of the two implementions?

I would not use exceptions since 1) a value not being in the cache is
not an exceptional event, and 2) it can easily be handled by the caller.

An alternative is to use a function that checks if a value is in the
cache and if it is you retrieve it, if it is not you get it from the
remote device and add it to the cache:

if (inCache(key) == true) {
return getFromCache(key);
} else {
double val = getFromRemote(key);
addToCache(key, val);
return val;
}
My current implementation uses an exception, and so far the only drawback
is that it generates a bunch of debugger noise in MS Visual Studio. But I
don't think VS is being reasonable in reporting every exception without
the ability to silence it. Is gdb subject to acting like this?

In the Debug-menu select Exceptions and uncheck C++ exceptions.
 
P

Paavo Helde

I've got a system that needs to fetch settings from a remote device
over a communications link. I cache these in a Cache class. When
reading a setting, I first see if it's in the cache. If not, I fetch
it from the external device and then cache the result.

Should a cache miss be implemented as an exception or an extra return
value? What are the relative merits of the two implementions?

Exceptions are designed to be used in exceptional situations, i.e. which
don't occur often. The compiler writers are trying to optimize the normal
code branches over the exceptional ones, which means throwing an
exception might be significantly slower than using a return value. Lots
of people interpret the above as a guideline that throwing an exception
should always indicate an error, i.e. something went wrong somewhere and
the current operation is aborted.

In your case the speed argument is not very important because the cache
miss is followed by a remote comm link query, which is probably much
slower anyway. However, as the cache miss is not an error in any sense,
it should not be reported as exception, just to follow common
conventions.

If you still decide to keep using exceptions, define a new exception type
(preferrably derived from std::exception) which you can add to the visual
studio debugger exception filters and switch on and off easily.

hth
Paavo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top