diff --git a/ntcore/src/main/native/cpp/net/ServerImpl.cpp b/ntcore/src/main/native/cpp/net/ServerImpl.cpp index 2813128249..ecfa4f03db 100644 --- a/ntcore/src/main/native/cpp/net/ServerImpl.cpp +++ b/ntcore/src/main/native/cpp/net/ServerImpl.cpp @@ -650,6 +650,11 @@ void ClientData4Base::ClientSubscribe(int64_t subuid, } // see if this immediately subscribes to any topics + // for transmit efficiency, we want to batch announcements and values, so + // send announcements in first loop and remember what we want to send in + // second loop. + std::vector dataToSend; + dataToSend.reserve(m_server.m_topics.size()); for (auto&& topic : m_server.m_topics) { bool removed = false; if (replace) { @@ -687,11 +692,15 @@ void ClientData4Base::ClientSubscribe(int64_t subuid, // send last value if (added && !sub->options.topicsOnly && !wasSubscribedValue && topic->lastValue) { - DEBUG4("send last value for {} to client {}", topic->name, m_id); - SendValue(topic.get(), topic->lastValue, kSendAll); + dataToSend.emplace_back(topic.get()); } } + for (auto topic : dataToSend) { + DEBUG4("send last value for {} to client {}", topic->name, m_id); + SendValue(topic, topic->lastValue, kSendAll); + } + // update meta data UpdateMetaClientSub();