[ntcore] Compute Value memory size when creating value (#5657)

This avoids a later switch-based computation in NetworkLoopQueue.
This commit is contained in:
Peter Johnson
2023-09-17 20:01:57 -07:00
committed by GitHub
parent 5383589f99
commit e93c233d60
3 changed files with 53 additions and 55 deletions

View File

@@ -12,37 +12,7 @@ static constexpr size_t kMaxSize = 2 * 1024 * 1024;
void NetworkLoopQueue::SetValue(NT_Publisher pubHandle, const Value& value) {
std::scoped_lock lock{m_mutex};
switch (value.type()) {
case NT_STRING:
m_size += value.GetString().size(); // imperfect but good enough
break;
case NT_RAW:
m_size += value.GetRaw().size_bytes();
break;
case NT_BOOLEAN_ARRAY:
m_size += value.GetBooleanArray().size_bytes();
break;
case NT_INTEGER_ARRAY:
m_size += value.GetIntegerArray().size_bytes();
break;
case NT_FLOAT_ARRAY:
m_size += value.GetFloatArray().size_bytes();
break;
case NT_DOUBLE_ARRAY:
m_size += value.GetDoubleArray().size_bytes();
break;
case NT_STRING_ARRAY: {
auto arr = value.GetStringArray();
m_size += arr.size_bytes();
for (auto&& s : arr) {
m_size += s.capacity();
}
break;
}
default:
break;
}
m_size += sizeof(ClientMessage);
m_size += sizeof(ClientMessage) + value.size();
if (m_size > kMaxSize) {
if (!m_sizeErrored) {
WPI_ERROR(m_logger, "NT: dropping value set due to memory limits");