Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Alternative implementations for dependencies from Thunder #220

Open
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

AdityaKasar
Copy link
Collaborator

No description provided.

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.
@CLAassistant
Copy link

CLAassistant commented Sep 25, 2024

CLA assistant check
All committers have signed the CLA.

method_name = full_name.substr(last_colon + 2);
}

return method_name;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 "Module.h"
#include "error.h"
#include "Logger.h"

std::string GetCurrentTime24HoursFormat()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants