> This is not a bug per se, but I find it a little odd that the MVOdb functions RS,
> RSA, RSAI, and WSA use std::string as their type, while WS ans WSAI use const
> char*
>
> Seems to me like simple overloading a la
> void WS(const char* varname, const std::string v, MVOdbError* error = NULL){
> WS(varname, v.c_str(), v.size(), error);
> }
>
> should be all that's needed, right?
No short answer to this one.
This situation is an excellent example of c++ bloat. Reduced to bare basics:
1) "naive" c++ code:
void foo(std::string xxx) { ... };
int main() { foo("bar"); }
nominally:
a new string object is created to hold "bar"
a new string object is copy-created to pass it as argument to foo()
result:
two object creations (two calls to malloc + constructors)
plus memcpy() of string data. (compiler may or may not optimize the 2nd string)
2) "advanced" c++ code:
void foo(const std::string& xxx) { ... };
int main() { foo("bar"); }
copy-created 2nd string is avoided, but string object to hold "bar" is still must be
made, 1 malloc(), 1 memcpy().
3) "pure C" code:
void foo(const char xxx) { ... };
int main() { foo("bar"); }
address of "bar" (placed in read-only memory) is passed in a register, no malloc(), no
memcpy(), nada, zilch.
One can argue that bloat does not matter, "just buy a bigger computer".
This ignores the fact that malloc() is quite expensive, nominally requires taking a
mutex, and suddenly multiple threads calling foo() are unexpectedly serialized against
the malloc() internal mutex.
I guess you can have an advanced malloc() that uses per-thread memory pools, but now
instead of deterministic "always take a lock", we have non-deterministic "take a lock
sometimes, when per-thread memory pools decide to jockey for more memory".
This type of non-deterministic behaviour is bad for real-time applications.
Ultimately it boils down to personal style, I prefer "C-like" efficiency and
transparency, when I call foo() it is obvious there will be no hidden malloc(), no
hidden mutex.
I guess mvodb could have "const std::string&" version of each "const char*" function,
as if there is too few functions there already...
This problem is not isolated to mvodb, but pertains to any API, including midas.h.
I would say, if most function calls are foo("abc"); then "const char*" version is
sufficient, if most calls are foo(string + "something"); then "const std::string&" is
more appropriate.
K.O. |