Optimize API connection memory usage by removing client_peername_

This commit is contained in:
J. Nick Koston 2025-06-26 11:59:03 +02:00
parent 2930c8e9a8
commit ffd442624f
No known key found for this signature in database
3 changed files with 6 additions and 8 deletions

View File

@ -77,7 +77,6 @@ void APIConnection::start() {
return;
}
this->client_info_ = helper_->getpeername();
this->client_peername_ = this->client_info_;
this->helper_->set_log_info(this->client_info_);
}
@ -1550,12 +1549,11 @@ bool APIConnection::try_send_log_message(int level, const char *tag, const char
HelloResponse APIConnection::hello(const HelloRequest &msg) {
this->client_info_ = msg.client_info;
this->client_peername_ = this->helper_->getpeername();
this->helper_->set_log_info(this->get_client_combined_info());
this->client_api_version_major_ = msg.api_version_major;
this->client_api_version_minor_ = msg.api_version_minor;
ESP_LOGV(TAG, "Hello from client: '%s' | %s | API Version %" PRIu32 ".%" PRIu32, this->client_info_.c_str(),
this->client_peername_.c_str(), this->client_api_version_major_, this->client_api_version_minor_);
this->helper_->getpeername().c_str(), this->client_api_version_major_, this->client_api_version_minor_);
HelloResponse resp;
resp.api_version_major = 1;
@ -1575,7 +1573,7 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) {
if (correct) {
ESP_LOGD(TAG, "%s connected", this->get_client_combined_info().c_str());
this->connection_state_ = ConnectionState::AUTHENTICATED;
this->parent_->get_client_connected_trigger()->trigger(this->client_info_, this->client_peername_);
this->parent_->get_client_connected_trigger()->trigger(this->client_info_, this->helper_->getpeername());
#ifdef USE_HOMEASSISTANT_TIME
if (homeassistant::global_homeassistant_time != nullptr) {
this->send_time_request();

View File

@ -276,11 +276,12 @@ class APIConnection : public APIServerConnection {
bool send_buffer(ProtoWriteBuffer buffer, uint16_t message_type) override;
std::string get_client_combined_info() const {
if (this->client_info_ == this->client_peername_) {
std::string peername = this->helper_->getpeername();
if (this->client_info_ == peername) {
// Before Hello message, both are the same (just IP:port)
return this->client_info_;
}
return this->client_info_ + " (" + this->client_peername_ + ")";
return this->client_info_ + " (" + peername + ")";
}
// Buffer allocator methods for batch processing
@ -452,7 +453,6 @@ class APIConnection : public APIServerConnection {
// Strings (12 bytes each on 32-bit)
std::string client_info_;
std::string client_peername_;
// 2-byte aligned types
uint16_t client_api_version_major_{0};

View File

@ -184,7 +184,7 @@ void APIServer::loop() {
}
// Rare case: handle disconnection
this->client_disconnected_trigger_->trigger(client->client_info_, client->client_peername_);
this->client_disconnected_trigger_->trigger(client->client_info_, client->helper_->getpeername());
ESP_LOGV(TAG, "Remove connection %s", client->client_info_.c_str());
// Swap with the last element and pop (avoids expensive vector shifts)