Repository navigation
Rewrote remap_stats plugin to use C++ - #6006
Conversation
f7cb390 to
fd56f6a
Compare
|
I think am not running the whole testsuite locally, right now I'm doing it think like so: Is it wrong or am I missing something? Sorry but I can't find any documentation how to run the tests. |
|
It seems like compile errors. Here's one of the errors. Please run the configure script with |
fd56f6a to
73e0f23
Compare
|
Thanks, should be all right now. I had to create a VM with Ubuntu 19.04 after all since on Arch I get also deprecation warnings for |
| static std::string | ||
| create_stat_name(const std::string &h, const std::string &b) | ||
| { | ||
| std::string res(std::string("plugin.") + PLUGIN_NAME + '.' + h + '.' + b); |
There was a problem hiding this comment.
This is more efficiently done with
std::string_view PREFIX{"plugin."};
static const size_t pn_len = strlen(PLUGIN_NAME);
std::string res;
res.reserve(PREFIX.size() + pn_len + 1+ h.size() + 1 + b.size() + 1);
res = PREFIX;
res += PLUGIN_NAME;
res += '.';
res += h;
res += '.';
res += b;
// Or this for the daring -
// snprintf(res.data(), res.capacity(), "%s.%s.%s.%s", PREFIX.data(), PLUGIN_NAME, h.c_str(), b.c_str());
return std::move(res);
}
Yes, it's a lot more code, but it does at most one allocation. It's shorter if you abuse snprintf.
There was a problem hiding this comment.
Ok, I'll go with the longer version. 👍
There was a problem hiding this comment.
Note that in C++17, std::string::data() was changed to return char* instead of const char* precisely to allow this kind of use of snprintf and equivalents.
There was a problem hiding this comment.
If you prefer the version with snprintf I can go with that.
There was a problem hiding this comment.
gcc9 may complain about the unnecessary call to std::move in the return as res is already local scope. Depends on the compiler debug levels I'm guessing.
There was a problem hiding this comment.
@traeak I've been doing that quite a bit lately, using gcc 9, and haven't had it complain. I think it's not until C++20 that the compiler is allowed to move returned local variables.
| char *remap, *hostname; | ||
| char *unknown = "unknown"; | ||
| char stat_name[MAX_STAT_LENGTH]; | ||
| std::string remap, hostname; |
There was a problem hiding this comment.
These three should be std::string_view. Note the original code presumes them to be C strings.
There was a problem hiding this comment.
Ok, would it make sense for stat_name to be std::string_view too? Or just remap, hostname and unknown?
I guess it shouldn't since in stat_add I call TSStatFindName and that expects a const char *, just want to be sure. :)
There was a problem hiding this comment.
The primary reason to use string_view instead of string is to avoid unnecessary memory allocation. I would be tempted to keep the original char stat_name[MAX_STAT_LENGTH]; for that reason. Or if you want to use std::string, then reserve that amount of space to have only 1 allocation. You can pass a string_view to something that expects const char*, you just need to be careful about whether it's expected to be a C string. In this particular case, the original logic clearly assumed that.
You could also look at ts::LocalBufferWriter which was written for this situation. E.g.
ts::LocalBufferWriter<MAX_STAT_LENGTH> stat_w;
// ...
stat_w.reset().reduce(1); // clear any current data, reserve 1 byte for later.
stat_w.print("plugin.{}.{}.{}", PLUGIN_NAME, remap, "in_bytes");
stat_w.extend(1).write('\0'); // get back the byte and null terminate the string.
The reduce / extend calls make sure the name is a null terminated C string even if the
output is larger than MAX_STAT_LENGTH. LocalBufferWriter stack allocates its internal buffer and clips output to that size.
There was a problem hiding this comment.
ts::LocalBufferWriter seems to be perfect for this, should I pass a const ts::LocalBufferWriter & to stat_add though? Or a const char * would be better? After all I'll always use it like a C string in that method. If I call stat_add with const ts::LocalBufferWriter & should also use it as a key for the internal hash?
I'm leaning toward calling stat_add with const char * but I want to know your opinion too.
There was a problem hiding this comment.
You should definitely not call it with ts::LocalBufferWriter. Generally you would use the view method to pass a string_view after writing to the buffer. In this case, since it's dealing with a predefined low level C interface, I would go ahead and pass in a const char*, via stat_w.data().
There was a problem hiding this comment.
Ah, didn't see the key for a hash question. The Buffer Writer stuff is useful only for writing to buffers, it's not suitable for persistence. The normal use for that is to use string_view as the key and get use the view method of the Buffer Writer for it. In these cases you need to be careful of the buffer lifetime. If you need it to persist (so as to serve as a key, for instance), you should use ts::FixedBufferWriter which writes on an externally defined buffer. You could also use ts::bwprint which does the busy work of doing all this to a std::string. Then you can use that string as the key. E.g.
std::string stat_name;
ts::bwprint(stat_name, "plugin.{}.{}.{}", ...);
That takes care of resizing the string as needed and you get the null termination without explicit work. It does potentially require an allocation, but that's probably OK here since this is startup and not run time code.
I really need to improve the documentation on this, although there is some - https://docs.trafficserver.apache.org/en/latest/developer-guide/internal-libraries/buffer-writer.en.html
|
For the relatively small number of stats elements in the hash table I'm wondering if std::map<> might be faster. I know for small numbers of entries (thousands?) std::map<> is faster, especially with string based keys. |
I don't know the number of entries in this case but I'd try to run a benchmark, or find someone that already tried and talks about it. |
|
The std::map is only a suggestion based on curiousity. I don't think anyone would block the PR over it. |
SolidWallOfCode
left a comment
There was a problem hiding this comment.
Really, I think this is good enough as a first pass to accept, I don't want to hang it up by requiring maximum optimization.
73e0f23 to
f6cf0da
Compare
|
Cherry-picked to v9.0.x branch. |
This should fix #5737