Skip to content

Commit

Permalink
fix: Remove LOG_MEMORY_ALLOCATIONS test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 16, 2024
1 parent 563ad61 commit d291f63
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 56 deletions.
57 changes: 2 additions & 55 deletions packages/react-native-nitro-modules/cpp/core/HybridObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,11 @@

#include "HybridObject.hpp"
#include "JSIConverter.hpp"
#include "NitroLogger.hpp"

#define LOG_MEMORY_ALLOCATIONS false

namespace margelo::nitro {

#if LOG_MEMORY_ALLOCATIONS
static std::mutex _instanceCounterMutex;
static std::unordered_map<const char*, uint32_t> _aliveInstances;
static std::unordered_map<const char*, int> _instanceIds;

static uint32_t incrementAliveInstancesAndGet(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
return ++_aliveInstances[name];
}

static uint32_t decrementAliveInstancesAndGet(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
return --_aliveInstances[name];
}

static uint32_t getTotalAliveInstances() {
std::unique_lock lock(_instanceCounterMutex);
uint32_t total = 0;
for (const auto& iter : _aliveInstances) {
total += iter.second;
}
return total;
}

static int getId(const char* name) {
std::unique_lock lock(_instanceCounterMutex);
if (_instanceIds.find(name) == _instanceIds.end()) {
_instanceIds.insert({name, 1});
}
auto iterator = _instanceIds.find(name);
return iterator->second++;
}
#endif

HybridObject::HybridObject(const char* name) : HybridObjectPrototype(), _name(name) {
#if LOG_MEMORY_ALLOCATIONS
_instanceId = getId(name);
uint32_t alive = incrementAliveInstancesAndGet(_name);
uint32_t totalObjects = getTotalAliveInstances();
Logger::log(LogLevel::Info, TAG, "(MEMORY) ✅ Creating %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i)", _name, _instanceId,
_name, alive, totalObjects);
#endif
}

HybridObject::~HybridObject() {
#if LOG_MEMORY_ALLOCATIONS
uint32_t alive = decrementAliveInstancesAndGet(_name);
uint32_t totalObjects = getTotalAliveInstances();
Logger::log(LogLevel::Info, TAG, "(MEMORY) ❌ Deleting %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i) ", _name, _instanceId,
_name, alive, totalObjects);
#endif
}
HybridObject::HybridObject(const char* name) : HybridObjectPrototype(), _name(name) {}
HybridObject::~HybridObject() {}

std::string HybridObject::toString() {
return "[HybridObject " + std::string(_name) + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class HybridObject : public virtual jsi::NativeState, public HybridObjectPrototy
private:
static constexpr auto TAG = "HybridObject";
const char* _name = TAG;
int _instanceId = 1;
std::unordered_map<jsi::Runtime*, OwningReference<jsi::WeakObject>> _objectCache;
};

Expand Down

0 comments on commit d291f63

Please sign in to comment.