Optimal optional data passing

Optimal optional data passing

Post by cxxn.. » Mon, 04 Aug 2003 07:12:25



Hi,

I'm developing some code which has an abstract interface and with some of
the functions it is useful if I pass in a cached value of one of the
parameters which are otherwise calculated in those functions (and
numerically expensive to calculate). The issue is that this cached
parameter will not always be used, so in some cases this is a wasted
passing of data. Also I can envisage future uses where other data may be
required to be passed in, though of course this is perhaps too
future-looking, and that data won't always be used either, of course.
These functions are called a great many times over the course of the
program.

In this case, would it cause any significant efficiency problems if I
move from passing in about 4 parameters, to passing in fewer but with
one of them a struct/class? Clearly you get a reduction in copying of
the function parameters (good), but then accessing of struct members is
probably slightly slower (bad). Having a struct means that I'm able to
extend the code later without changing the interface, and there is no
(direct) overhead of passing in a wasted parameter. I should note that
wrt changing the interface, the change of struct is not an issue since
this will probably be recompiled...this is more a maintenance issue than
binary compatibility, etc.

Probably I should just do a set of tests :)

Thanks for any thoughts,

--
Neil, going to bed.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Optimal optional data passing

Post by David Lis » Tue, 05 Aug 2003 07:37:36


On 2 Aug 2003 18:12:25 -0400,


...<snip>...

Quote:> In this case, would it cause any significant efficiency problems if I
> move from passing in about 4 parameters, to passing in fewer but with
> one of them a struct/class? Clearly you get a reduction in copying of
> the function parameters (good),

Only if you pass the struct by reference - the same overhead
difference will occur if you pass the full four parameters by value or
reference.

Quote:> but then accessing of struct members is
> probably slightly slower (bad).

No - not if you maintain the values as public members of a struct, at
least.

Quote:> Having a struct means that I'm able to
> extend the code later without changing the interface, and there is no
> (direct) overhead of passing in a wasted parameter. I should note that
> wrt changing the interface, the change of struct is not an issue since
> this will probably be recompiled...this is more a maintenance issue than
> binary compatibility, etc.

> Probably I should just do a set of tests :)

My suggestion would be to create a "bean" struct or class, that you
create an instance of in the first sentence in your function - that
instance will be destroyed when the function body goes out of scope.

Maintain a static timing value inside the "bean" struct or class, that
is counted up as long as an instance of the "bean" exists. This way
you can test how much time is used inside the function body with
either way of passing parameters.

Of course you will have to extend on this if you use concurrency.

--
Med venlig hilsen / Best regards
David List

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

Optimal optional data passing

Post by Alex Hirne » Tue, 05 Aug 2003 07:39:33




Quote:> Hi,

> I'm developing some code which has an abstract interface and with some of
> the functions it is useful if I pass in a cached value of one of the
> parameters which are otherwise calculated in those functions (and
> numerically expensive to calculate). The issue is that this cached
> parameter will not always be used, so in some cases this is a wasted
> passing of data. Also I can envisage future uses where other data may be
> required to be passed in, though of course this is perhaps too
> future-looking, and that data won't always be used either, of course.
> These functions are called a great many times over the course of the
> program.

Well, my proposal:

template <typedef T>
struct value_passable
{
    T value_of_interest;
    bool is_already_computed;

Quote:};

///////////////////////////////

struct value_basics
{
    value_passable<float> foo;
    value_passable<int> bar;

Quote:};

struct value_extended : public value_basics
{
    value_passable<double> blah;

Quote:};

/////////////////////////////

// an example function who processes on these data sets
void compute_basic(value_basic& vb)
{
    if (!vb.foo.is_already_computed)                //compute it here
    {
        vb.foo.value_of_interest = /* anything you want */
        vb.foo.is_already_computed = true;        // dont forget that, next
time it will not computed again
    }
    ...

Quote:}

// another one, now requiring the extended data set
void compute_ex(value_extended& ve)
{
    /* do whatever with foo, bar and blah you want, like see above */

Quote:}

////////////////////////////////
// then you can use it like this:

value_extended ve;
ve.bar.value_of_interest = /* you already have this f.e. */
ve.bar.is_alraedy_computed = true; // do not forget

compute_basic(ve);
compute_ex(ve);

// thats it

Of course someone would enhance value_passable with constructor,
copy constructor, getter/setter and assignment operator (who would
then set is_already_computed to true) to make it errror proof, but
this post is already long enough.

Alex

Quote:> In this case, would it cause any significant efficiency problems if I
> move from passing in about 4 parameters, to passing in fewer but with
> one of them a struct/class? Clearly you get a reduction in copying of

<snip>

PS: Accessing structs is not costly (on common implementations of C++)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

1. optimal parameter passing

In Mike Crawford's article

http://www.kuro5hin.org/story/2002/5/9/205040/3918

it says
===========================
When to Pass Parameters by Value

Paradoxically, performance is also the reason to pass parameters by value.
This will be the case when the object is very small, as with built-in types,
or that have fast constructors - the object's members are few and are all
held by value, especially when the constructor is inlined.

One reason is that references are usually implemented as memory addresses
that are automatically dereferenced for you. If an object is small enough
that it can be held in a register, passing it by pointer or reference will
require that it be copied to RAM and the RAM address passed instead. The
cost is double with binary interfaces that pass parameters in registers like
as is usually the case with PowerPC.

============================

My first question: Will pass by const reference always mean "that it be
copied to RAM and the RAM address passed instead"?

And then,  of course, what is the maximal size of an object before it should
be passed by reference?

Thanks

Thorsten Ottosen, Aalborg university


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. rounding

3. How to pass data between filters...

4. AutoCAD

5. wanna pass OLE data to others using socket like whiteboard.

6. XSL counter

7. Using SafeArray to pass binary data ?

8. help with fmincon

9. Design Question - Passing Data From Server to Client

10. Passing data block via OLE method

11. Automation/Passing Custom Data

12. User instance data passed to the callback mechanism

13. Passing data to external application