2

Let's suppose that I have this function:

void foo (struct1 &v1, struct2 &v2, ...,  structn &vn){
  //do something with a subset some of the arguments...
  foo1(v1, v2, ..., vn);
}

void foo1 (struct1 &v1, struct2 &v2, ...,  structn &vn){
  //do something with a subset some of the arguments...
  foo2(v1, v2, ..., vn);
}

...

void foom (struct1 &v1, struct2 &v2, ...,  structn &vn){
  //do something with a subset some of the arguments...
}

Is it a good practice to encapsulate all these arguments inside a dedicated structure and use it instead as only argument? Something like:

struct wrapper{
  strcut1 v1;
  struct2 v2;
  ...
  structn vn;
}

void foo (wrapper & w){
  //use some of w's fields
  foo1(w);
}

void foo1 (wrapper & w){
  //use some of w's fields
  foo2(w);
}

...

void foom (wrapper & w){
  //use some of w's fields
}

I think that this approach has 2 advantages:

  1. It's more maintainable: with the first method, if I have to add a new argument to foom, then I have to modify also all the others m-1 foo headers...a nightmare. With this method instead the only thing to do is to add a new field to wrapper.
  2. It's more efficient: supposing that both n (the number of arguments) and m (the number of functions) is big (say both 10) this means that we have to copy m*n addresses, which is not a big overhead, but for sure bigger than m.

Is everything correct? Did I miss some advantage/disadvantage? Notice that performance here are the priority

Why I need this?

Well, read this question to know more about it.

Community
  • 1
  • 1
justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • Could anybody explain how will this effect struct aligns? Will there be a huge blank space in the wrapper struct? – zhm Apr 13 '17 at 08:28
  • @MartinZhai Thanks for your comment. Please, would explain me why this should be a problem? From my knowledge, data aligment is useful when we want to exploit vectorization, which is not this case. Or did you mean something else? – justHelloWorld Apr 13 '17 at 08:50
  • When you put multi structures in a wrapper struct, the sub structures have different size, will compiler align them to improve performance? https://en.wikipedia.org/wiki/Data_structure_alignment – zhm Apr 13 '17 at 09:26
  • @MartinZhai I think that this is compiler dependent. But anyway, in what scenario this could be a problem? Usually aligment problem arises when you want to process elements inside a struct such as an array in a loop. How this is related to this problem? – justHelloWorld Apr 13 '17 at 09:30
  • I wonder if wrap structures into a struct will increase the size of memory usage. – zhm Apr 13 '17 at 09:32
  • I think that the memory used will be equal or larger depending on the compiler. But, again, I think that this is not going to affect performance in this case, since we are copying and passing around the wrapper address, which is constant (no matter how many structures are inside or how big they are) – justHelloWorld Apr 13 '17 at 09:48

2 Answers2

1

It depends on the number of parameters that you have.

In general what is common is to encapsulate many parameters into one single struct called "params" for example, so that the prototype of the function is not too lengthy.

Example of this scheme are FLANN, kd-GeRaF (mine) and FALCONN.

I haven't seen any case where the parameters are structs to be honest, so that is in your personal judgement.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

Yes, it's better. You basically have a pack of variables that you need to manipulate together, and which have to be passed around together. The main advantage is, that by wrapping this pack of variables in a struct, you give it a name, and simplify the function calls.

Regarding performance: Well, probably it's irrelevant. Yes, you are passing less addresses on the stack with the wrapped approach, however, that won't speed up things a bit unless that function call is made really often (= several million times a second). If those function calls are not part of the inner loop of your application (like 99% of all code that is written), the speed difference is irrelevant.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106