Skip to content

Commit

Permalink
[ntcore] Remove SmallVector and SmallString from most places
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Jan 2, 2025
1 parent fb28963 commit 19da3a3
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
5 changes: 2 additions & 3 deletions ntcore/src/main/native/cpp/ListenerStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <utility>
#include <vector>

#include <wpi/SmallVector.h>

#include "ntcore_c.h"

using namespace nt;
Expand Down Expand Up @@ -300,7 +298,8 @@ ListenerStorage::DestroyListenerPoller(NT_ListenerPoller pollerHandle) {
std::scoped_lock lock{m_mutex};
if (auto poller = m_pollers.Remove(pollerHandle)) {
// ensure all listeners that use this poller are removed
wpi::SmallVector<NT_Listener, 16> toRemove;
std::vector<NT_Listener> toRemove;
toRemove.reserve(m_listeners.size());
for (auto&& listener : m_listeners) {
if (listener->poller == poller.get()) {
toRemove.emplace_back(listener->handle);
Expand Down
4 changes: 3 additions & 1 deletion ntcore/src/main/native/cpp/local/LocalStorageImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fmt/format.h>
#include <fmt/ranges.h>
#include <wpi/DataLog.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>

#include "IListenerStorage.h"
Expand Down Expand Up @@ -588,9 +589,10 @@ void StorageImpl::AddListenerImpl(NT_Listener listenerHandle,
.get();

// if we're doing anything immediate, get the list of matching topics
wpi::SmallVector<LocalTopic*, 32> topics;
std::vector<LocalTopic*> topics;
if ((eventMask & NT_EVENT_IMMEDIATE) != 0 &&
(eventMask & (NT_EVENT_PUBLISH | NT_EVENT_VALUE_ALL)) != 0) {
topics.reserve(m_topics.size());
for (auto&& topic : m_topics) {
if (topic->Exists() && subscriber->Matches(topic->name, topic->special)) {
topics.emplace_back(topic.get());
Expand Down
19 changes: 9 additions & 10 deletions ntcore/src/main/native/cpp/ntcore_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <vector>

#include <wpi/MemAlloc.h>
#include <wpi/SmallVector.h>
#include <wpi/json.h>
#include <wpi/timestamp.h>

Expand Down Expand Up @@ -226,7 +225,7 @@ NT_Topic* NT_GetTopics(NT_Inst inst, const struct WPI_String* prefix,
NT_Topic* NT_GetTopicsStr(NT_Inst inst, const struct WPI_String* prefix,
const struct WPI_String* types, size_t types_len,
size_t* count) {
wpi::SmallVector<std::string_view, 4> typesCpp;
std::vector<std::string_view> typesCpp;
typesCpp.reserve(types_len);
for (size_t i = 0; i < types_len; ++i) {
typesCpp.emplace_back(wpi::to_string_view(&types[i]));
Expand All @@ -246,7 +245,7 @@ struct NT_TopicInfo* NT_GetTopicInfosStr(NT_Inst inst,
const struct WPI_String* prefix,
const struct WPI_String* types,
size_t types_len, size_t* count) {
wpi::SmallVector<std::string_view, 4> typesCpp;
std::vector<std::string_view> typesCpp;
typesCpp.reserve(types_len);
for (size_t i = 0; i < types_len; ++i) {
typesCpp.emplace_back(wpi::to_string_view(&types[i]));
Expand Down Expand Up @@ -410,10 +409,10 @@ NT_Topic NT_GetTopicFromHandle(NT_Handle pubsubentry) {
NT_MultiSubscriber NT_SubscribeMultiple(
NT_Inst inst, const struct WPI_String* prefixes, size_t prefixes_len,
const struct NT_PubSubOptions* options) {
wpi::SmallVector<std::string_view, 8> p;
p.resize_for_overwrite(prefixes_len);
std::vector<std::string_view> p;
p.reserve(prefixes_len);
for (size_t i = 0; i < prefixes_len; ++i) {
p[i] = wpi::to_string_view(&prefixes[i]);
p.emplace_back(wpi::to_string_view(&prefixes[i]));
}
return nt::SubscribeMultiple(inst, p, ConvertToCpp(options));
}
Expand Down Expand Up @@ -463,10 +462,10 @@ NT_Listener NT_AddListenerMultiple(NT_Inst inst,
const struct WPI_String* prefixes,
size_t prefixes_len, unsigned int mask,
void* data, NT_ListenerCallback callback) {
wpi::SmallVector<std::string_view, 8> p;
std::vector<std::string_view> p;
p.reserve(prefixes_len);
for (size_t i = 0; i < prefixes_len; ++i) {
p.emplace_back(prefixes[i].str, prefixes[i].len);
p.emplace_back(wpi::to_string_view(&prefixes[i]));
}
return nt::AddListener(inst, p, mask, [=](auto& event) {
NT_Event event_c;
Expand Down Expand Up @@ -497,10 +496,10 @@ NT_Listener NT_AddPolledListenerMultiple(NT_ListenerPoller poller,
const struct WPI_String* prefixes,
size_t prefixes_len,
unsigned int mask) {
wpi::SmallVector<std::string_view, 8> p;
std::vector<std::string_view> p;
p.reserve(prefixes_len);
for (size_t i = 0; i < prefixes_len; ++i) {
p.emplace_back(prefixes[i].str, prefixes[i].len);
p.emplace_back(wpi::to_string_view(&prefixes[i]));
}
return nt::AddPolledListener(poller, p, mask);
}
Expand Down
11 changes: 5 additions & 6 deletions ntcore/src/main/native/cpp/server/ServerClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ void ServerClient::UpdateMetaClientSub() {
}
}

std::span<ServerSubscriber*> ServerClient::GetSubscribers(
std::string_view name, bool special,
wpi::SmallVectorImpl<ServerSubscriber*>& buf) {
buf.resize(0);
std::vector<ServerSubscriber*> ServerClient::GetSubscribers(
std::string_view name, bool special) {
std::vector<ServerSubscriber*> out;
for (auto&& subPair : m_subscribers) {
ServerSubscriber* subscriber = subPair.getSecond().get();
if (subscriber->Matches(name, special)) {
buf.emplace_back(subscriber);
out.emplace_back(subscriber);
}
}
return {buf.data(), buf.size()};
return out;
}
8 changes: 3 additions & 5 deletions ntcore/src/main/native/cpp/server/ServerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <wpi/json_fwd.h>

Expand All @@ -22,8 +23,6 @@

namespace wpi {
class Logger;
template <typename T>
class SmallVectorImpl;
} // namespace wpi

namespace nt::server {
Expand Down Expand Up @@ -67,9 +66,8 @@ class ServerClient {
void UpdateMetaClientPub();
void UpdateMetaClientSub();

std::span<ServerSubscriber*> GetSubscribers(
std::string_view name, bool special,
wpi::SmallVectorImpl<ServerSubscriber*>& buf);
std::vector<ServerSubscriber*> GetSubscribers(std::string_view name,
bool special);

std::string_view GetName() const { return m_name; }
int GetId() const { return m_id; }
Expand Down
4 changes: 1 addition & 3 deletions ntcore/src/main/native/cpp/server/ServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ void ServerImpl::SendAnnounce(ServerTopic* topic, ServerClient* client) {
}

// look for subscriber matching prefixes
wpi::SmallVector<ServerSubscriber*, 16> subscribersBuf;
auto subscribers =
aClient->GetSubscribers(topic->name, topic->special, subscribersBuf);
auto subscribers = aClient->GetSubscribers(topic->name, topic->special);

// don't announce to this client if no subscribers
if (subscribers.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion ntcore/src/main/native/cpp/server/ServerStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void ServerStorage::SetValue(ServerClient* client, ServerTopic* topic,

void ServerStorage::RemoveClient(ServerClient* client) {
// remove all publishers and subscribers for this client
wpi::SmallVector<ServerTopic*, 16> toDelete;
std::vector<ServerTopic*> toDelete;
toDelete.reserve(m_topics.size());
for (auto&& topic : m_topics) {
bool pubChanged = false;
bool subChanged = false;
Expand Down
5 changes: 0 additions & 5 deletions ntcore/src/main/native/include/ntcore_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
#include "ntcore_c.h"
#include "ntcore_cpp_types.h"

namespace wpi {
template <typename T>
class SmallVectorImpl;
} // namespace wpi

namespace wpi::log {
class DataLog;
} // namespace wpi::log
Expand Down

0 comments on commit 19da3a3

Please sign in to comment.