[api] Optimize noise handshake with memcpy for faster connection setup

This commit is contained in:
J. Nick Koston 2025-07-21 11:20:49 -10:00
parent 74ce3d2c0b
commit 4a39f14037
No known key found for this signature in database

View File

@ -73,7 +73,10 @@ APIError APINoiseFrameHelper::init() {
} }
// init prologue // init prologue
prologue_.insert(prologue_.end(), PROLOGUE_INIT, PROLOGUE_INIT + strlen(PROLOGUE_INIT)); size_t old_size = prologue_.size();
size_t init_len = strlen(PROLOGUE_INIT);
prologue_.resize(old_size + init_len);
std::memcpy(prologue_.data() + old_size, PROLOGUE_INIT, init_len);
state_ = State::CLIENT_HELLO; state_ = State::CLIENT_HELLO;
return APIError::OK; return APIError::OK;
@ -223,11 +226,12 @@ APIError APINoiseFrameHelper::state_action_() {
return handle_handshake_frame_error_(aerr); return handle_handshake_frame_error_(aerr);
} }
// ignore contents, may be used in future for flags // ignore contents, may be used in future for flags
// Reserve space for: existing prologue + 2 size bytes + frame data // Resize for: existing prologue + 2 size bytes + frame data
prologue_.reserve(prologue_.size() + 2 + frame.size()); size_t old_size = prologue_.size();
prologue_.push_back((uint8_t) (frame.size() >> 8)); prologue_.resize(old_size + 2 + frame.size());
prologue_.push_back((uint8_t) frame.size()); prologue_[old_size] = (uint8_t) (frame.size() >> 8);
prologue_.insert(prologue_.end(), frame.begin(), frame.end()); prologue_[old_size + 1] = (uint8_t) frame.size();
std::memcpy(prologue_.data() + old_size + 2, frame.data(), frame.size());
state_ = State::SERVER_HELLO; state_ = State::SERVER_HELLO;
} }
@ -237,18 +241,22 @@ APIError APINoiseFrameHelper::state_action_() {
const std::string &mac = get_mac_address(); const std::string &mac = get_mac_address();
std::vector<uint8_t> msg; std::vector<uint8_t> msg;
// Reserve space for: 1 byte proto + name + null + mac + null // Calculate positions and sizes
msg.reserve(1 + name.size() + 1 + mac.size() + 1); size_t name_len = name.size() + 1; // including null terminator
size_t mac_len = mac.size() + 1; // including null terminator
size_t name_offset = 1;
size_t mac_offset = name_offset + name_len;
size_t total_size = 1 + name_len + mac_len;
msg.resize(total_size);
// chosen proto // chosen proto
msg.push_back(0x01); msg[0] = 0x01;
// node name, terminated by null byte // node name, terminated by null byte
const uint8_t *name_ptr = reinterpret_cast<const uint8_t *>(name.c_str()); std::memcpy(msg.data() + name_offset, name.c_str(), name_len);
msg.insert(msg.end(), name_ptr, name_ptr + name.size() + 1);
// node mac, terminated by null byte // node mac, terminated by null byte
const uint8_t *mac_ptr = reinterpret_cast<const uint8_t *>(mac.c_str()); std::memcpy(msg.data() + mac_offset, mac.c_str(), mac_len);
msg.insert(msg.end(), mac_ptr, mac_ptr + mac.size() + 1);
aerr = write_frame_(msg.data(), msg.size()); aerr = write_frame_(msg.data(), msg.size());
if (aerr != APIError::OK) if (aerr != APIError::OK)