Refactor: Alternative implementations for dependencies from Thunder#220
Open
AdityaKasar wants to merge 3 commits intonextfrom
Open
Refactor: Alternative implementations for dependencies from Thunder#220AdityaKasar wants to merge 3 commits intonextfrom
AdityaKasar wants to merge 3 commits intonextfrom
Conversation
Replace WPEFramework::Core::ClassNameOnly dependency with alternaetive implementation using <cxxabi.h> library
Remove dependency on WPEFramework::Core::Time by writing alternative implementation using <chrono> standard library
Removed dependency on WPEFramework::Core::CriticalSection by writing alternative implementation using mutex from c++ standard library.
tomasz-blasz
requested changes
Oct 15, 2024
| method_name = full_name.substr(last_colon + 2); | ||
| } | ||
|
|
||
| return method_name; |
Contributor
There was a problem hiding this comment.
In case when a given CLASS is not defined within a namespace, the function returns empty string as a name:
namespace abc
{
class Test_abc {};
}
class Test_glob {};
int main()
{
printf("[%s]\n", Module<abc::Test_abc>().c_str());
printf("[%s]\n", Module<Test_glob>().c_str());
}
# output:
[Test_abc]
[]
| return WPEFramework::Core::ClassNameOnly(typeid(CLASS).name()).Text(); | ||
| char _name[512]; | ||
| size_t _size = sizeof(_name) - 1; | ||
| std::string demangled_name = abi::__cxa_demangle(typeid(CLASS).name(), _name, &_size, 0); |
Contributor
There was a problem hiding this comment.
There is no reason for 3 separate variables: demangled_name, method_name, full_name. The function can be simplified into
template <typename CLASS>
static const std::string Module()
{
char _name[512];
size_t _size = sizeof(_name) - 1;
std::string class_name = abi::__cxa_demangle(typeid(CLASS).name(), _name, &_size, 0);
std::size_t last_colon = class_name.rfind("::");
if (last_colon != std::string::npos) {
class_name = class_name.substr(last_colon + 2);
}
return class_name;
}
Proposed version has already a fix for a class outside of any namesapece:
namespace abc
{
class Test_abc {};
}
class Test_glob {};
int main()
{
printf("[%s]\n", Module<abc::Test_abc>().c_str());
printf("[%s]\n", Module<Test_glob>().c_str());
}
# output:
[Test_abc]
[Test_glob]
| #include "error.h" | ||
| #include "Logger.h" | ||
|
|
||
| std::string GetCurrentTime24HoursFormat() |
Contributor
There was a problem hiding this comment.
No strong opinion here, just a thought that using C to obtain current time would speed up the function ~3 times in average: from 50ns to 17ns per call (on Ubuntu PC), it could be significant in logging functions on embedded device but does not have to, depending on how many logs are being produced.
char* GetTime__c(char *s, size_t len)
{
time_t t = time(NULL);
strftime(s, len, "%H:%M:%S", localtime(&t));
return s;
}
Evaluation:
int main()
{
const char *s1;
constexpr size_t s2_size = 10;
char s2[s2_size];
std::chrono::steady_clock::time_point t1;
std::chrono::steady_clock::time_point t2;
long unsigned duration1 = 0;
long unsigned duration2 = 0;
constexpr unsigned count = 10;
for (unsigned i = 0; i < count; ++i) {
t1 = std::chrono::steady_clock::now();
s1 = GetCurrentTime24HoursFormat().c_str();
t2 = std::chrono::steady_clock::now();
duration1 += std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
printf("t - cpp[%02u]: %s\n", i, s1);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
for (unsigned i = 0; i < count; ++i) {
t1 = std::chrono::steady_clock::now();
GetTime__c(s2, s2_size);
t2 = std::chrono::steady_clock::now();
duration2 += std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count();
printf("t - c [%02u]: %s\n", i, s2);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
printf("t - cpp : count: %u, duration-total: %7luns, avg-duration: %6luns\n", count, duration1, duration1/count);
printf("t - c : count: %u, duration-total: %7luns, avg-duration: %6luns\n", count, duration2, duration2/count);
return 0;
}
# output:
t - cpp[00]: 15:16:58
...
t - cpp[09]: 15:17:00
t - c [00]: 15:17:00
...
t - c [09]: 15:17:03
t - cpp : count: 10, duration-total: 529668ns, avg-duration: 52966ns
t - c : count: 10, duration-total: 175083ns, avg-duration: 17508ns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.