Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce SmallVector and SmallString usage #7610

Open
wants to merge 54 commits into
base: 2027
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
2319c7b
[cscore] HttpCameraImpl: Remove SmallVector usage
PeterJohnson Dec 30, 2024
777904a
[cscore] Instance: Remove SmallVector
PeterJohnson Dec 30, 2024
339c0f2
[cscore] Property: Remove SmallVector use
PeterJohnson Dec 30, 2024
1f2c018
[cscore] SourceImpl: Remove SmallVector use
PeterJohnson Dec 30, 2024
435f86d
[cscore] Remove SmallVector use from enumerate handles
PeterJohnson Dec 30, 2024
254f25e
[cscore] Remove SmallVector from SinkImpl
PeterJohnson Dec 30, 2024
1a7f721
Remove unused include
PeterJohnson Dec 30, 2024
9970ced
UsbCameraProperty: Don't use SmallString
PeterJohnson Dec 30, 2024
8066a8d
Remove unused includes
PeterJohnson Dec 30, 2024
830398a
Remove more SmallVector uses
PeterJohnson Dec 30, 2024
b272d9e
Linux UsbCameraImpl: Remove SmallString
PeterJohnson Dec 31, 2024
8743238
UsbUtil: Remove some SmallStrign usage
PeterJohnson Dec 31, 2024
fa1b89d
MjpegServerImpl: Remove some SmallString usage
PeterJohnson Dec 31, 2024
325b7e7
HttpUtil: Change from SmallVector to std::string
PeterJohnson Dec 31, 2024
566c953
raw_istream::getline(): Change from SmallVector to std::string
PeterJohnson Dec 31, 2024
2cc59f6
[glass] Reduce SmallString usage
PeterJohnson Dec 31, 2024
23b905d
Finish StringExtras split replacement
PeterJohnson Jan 1, 2025
6852ea4
Fix up includes
PeterJohnson Jan 1, 2025
96e6e02
Remove redundant move
PeterJohnson Jan 1, 2025
be75784
Don't copy string
PeterJohnson Jan 1, 2025
9516ef7
WebSocketServer: Use string/vector
PeterJohnson Jan 1, 2025
c2bbb96
Formatting
PeterJohnson Jan 1, 2025
9d747f5
Header cleanups
PeterJohnson Jan 1, 2025
02bdd2a
Change ConnectionList to vector
PeterJohnson Jan 2, 2025
339f3e9
Remove headers
PeterJohnson Jan 2, 2025
f52d6d9
[ntcore] Remove SmallVector version of NormalizeKey
PeterJohnson Jan 2, 2025
2455757
[ntcore] NetworkTable: Remove SmallString from GetHeirarchy
PeterJohnson Jan 2, 2025
8258e07
[ntcore] LocalStorage: Remove SmallString
PeterJohnson Jan 2, 2025
d27343a
[ntcore] Remove SmallVector and SmallString from most places
PeterJohnson Jan 2, 2025
1e8cdca
Add missing include
PeterJohnson Jan 2, 2025
3364126
Fix windows
PeterJohnson Jan 2, 2025
3d377a3
[wpilibc] Reduce SmallVector usage
PeterJohnson Jan 2, 2025
95e03f1
[wpilibc] Reduce SmallString usage
PeterJohnson Jan 2, 2025
ad7481b
Header cleanup
PeterJohnson Jan 2, 2025
979a587
[wpinet] hostname: Remove SmallVector version
PeterJohnson Jan 2, 2025
cba7bad
[wpinet] Process: Remove SmallVector
PeterJohnson Jan 2, 2025
e4373b3
[wpinet] MulticastServiceAnnouncer: Remove SmallVector
PeterJohnson Jan 2, 2025
e4747ab
[wpinet] JNI: Remove SmallVector use
PeterJohnson Jan 2, 2025
113b09a
More removals
PeterJohnson Jan 2, 2025
09abc34
Remove from sha1
PeterJohnson Jan 2, 2025
7860426
Remove from base64
PeterJohnson Jan 2, 2025
bb247ed
Simplify base64 vector decode
PeterJohnson Jan 2, 2025
7512c9e
windows service resolver
PeterJohnson Jan 2, 2025
b3386d2
More cleanup
PeterJohnson Jan 2, 2025
5db53c4
Remove from SimpleBufferPool
PeterJohnson Jan 2, 2025
25e08bf
Missing includes
PeterJohnson Jan 2, 2025
0f0d35d
Fix one more SimpleBufferPool use case
PeterJohnson Jan 2, 2025
60a2521
CameraServer: Remove SmallString
PeterJohnson Jan 2, 2025
0371a30
Use std::string in more places
PeterJohnson Jan 2, 2025
2602b62
MulticastServiceResolver: Remove SmallString
PeterJohnson Jan 2, 2025
a227f62
DynamicStruct: Don't use SmallString
PeterJohnson Jan 2, 2025
23814d1
[wpiutil] DataLog: use format instead of SmallString
PeterJohnson Jan 2, 2025
243ddc1
[wpiutil] Convert a couple of use cases to std::string
PeterJohnson Jan 3, 2025
790b099
A few more fixups
PeterJohnson Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 12 additions & 33 deletions cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
#include <networktables/StringArrayTopic.h>
#include <networktables/StringTopic.h>
#include <wpi/DenseMap.h>
#include <wpi/SmallString.h>
#include <wpi/StringExtras.h>
#include <wpi/StringMap.h>
#include <wpi/mutex.h>

#include "cameraserver/CameraServerShared.h"
#include "ntcore_cpp.h"

using namespace frc;

Expand Down Expand Up @@ -93,26 +91,14 @@ static Instance& GetInstance() {
return instance;
}

static std::string_view MakeSourceValue(CS_Source source,
wpi::SmallVectorImpl<char>& buf) {
static std::string MakeSourceValue(CS_Source source) {
CS_Status status = 0;
buf.clear();
switch (cs::GetSourceKind(source, &status)) {
case CS_SOURCE_USB: {
std::string_view prefix{"usb:"};
buf.append(prefix.begin(), prefix.end());
auto path = cs::GetUsbCameraPath(source, &status);
buf.append(path.begin(), path.end());
break;
}
case CS_SOURCE_USB:
return fmt::format("usb:{}", cs::GetUsbCameraPath(source, &status));
case CS_SOURCE_HTTP: {
std::string_view prefix{"ip:"};
buf.append(prefix.begin(), prefix.end());
auto urls = cs::GetHttpCameraUrls(source, &status);
if (!urls.empty()) {
buf.append(urls[0].begin(), urls[0].end());
}
break;
return fmt::format("ip:{}", urls.empty() ? "" : urls.front());
}
case CS_SOURCE_CV:
return "cv:";
Expand All @@ -121,8 +107,6 @@ static std::string_view MakeSourceValue(CS_Source source,
default:
return "unknown:";
}

return {buf.begin(), buf.size()};
}

static std::string MakeStreamValue(std::string_view address, int port) {
Expand Down Expand Up @@ -361,10 +345,8 @@ SourcePublisher::SourcePublisher(Instance& inst,
modeEntry{table->GetStringTopic("mode").GetEntry("")},
modesPublisher{table->GetStringArrayTopic("modes").Publish()} {
CS_Status status = 0;
wpi::SmallString<64> buf;
sourcePublisher.Set(MakeSourceValue(source, buf));
wpi::SmallString<64> descBuf;
descriptionPublisher.Set(cs::GetSourceDescription(source, descBuf, &status));
sourcePublisher.Set(MakeSourceValue(source));
descriptionPublisher.Set(cs::GetSourceDescription(source, &status));
connectedPublisher.Set(cs::IsSourceConnected(source, &status));
streamsPublisher.Set(inst.GetSourceStreamValues(source));
auto mode = cs::GetSourceVideoMode(source, &status);
Expand Down Expand Up @@ -404,9 +386,8 @@ Instance::Instance() {
case cs::VideoEvent::kSourceConnected:
if (auto publisher = GetPublisher(event.sourceHandle)) {
// update the description too (as it may have changed)
wpi::SmallString<64> descBuf;
publisher->descriptionPublisher.Set(cs::GetSourceDescription(
event.sourceHandle, descBuf, &status));
publisher->descriptionPublisher.Set(
cs::GetSourceDescription(event.sourceHandle, &status));
publisher->connectedPublisher.Set(true);
}
break;
Expand Down Expand Up @@ -604,8 +585,7 @@ cs::CvSink CameraServer::GetVideo() {

cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) {
auto& inst = ::GetInstance();
wpi::SmallString<64> name{"opencv_"};
name += camera.GetName();
auto name = fmt::format("opencv_{}", camera.GetName());

{
std::scoped_lock lock(inst.m_mutex);
Expand All @@ -622,7 +602,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) {
}
}

cs::CvSink newsink{name.str()};
cs::CvSink newsink{name};
newsink.SetSource(camera);
AddServer(newsink);
return newsink;
Expand All @@ -631,8 +611,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) {
cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera,
cs::VideoMode::PixelFormat pixelFormat) {
auto& inst = ::GetInstance();
wpi::SmallString<64> name{"opencv_"};
name += camera.GetName();
auto name = fmt::format("opencv_{}", camera.GetName());

{
std::scoped_lock lock(inst.m_mutex);
Expand All @@ -649,7 +628,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera,
}
}

cs::CvSink newsink{name.str(), pixelFormat};
cs::CvSink newsink{name, pixelFormat};
newsink.SetSource(camera);
AddServer(newsink);
return newsink;
Expand Down
3 changes: 2 additions & 1 deletion cscore/src/main/native/cpp/ConfigurableSourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>
#include <string>
#include <vector>

#include <wpi/timestamp.h>

Expand Down Expand Up @@ -238,7 +239,7 @@ CS_Property CS_CreateSourcePropertyCallback(
void CS_SetSourceEnumPropertyChoices(CS_Source source, CS_Property property,
const struct WPI_String* choices,
int count, CS_Status* status) {
wpi::SmallVector<std::string, 8> vec;
std::vector<std::string> vec;
vec.reserve(count);
for (int i = 0; i < count; ++i) {
vec.emplace_back(wpi::to_string_view(&choices[i]));
Expand Down
50 changes: 23 additions & 27 deletions cscore/src/main/native/cpp/HttpCameraImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ void HttpCameraImpl::StreamThreadMain() {
}

// connect
wpi::SmallString<64> boundary;
wpi::HttpConnection* conn = DeviceStreamConnect(boundary);
auto [conn, boundary] = DeviceStreamConnect();

if (!m_active) {
break;
Expand All @@ -139,7 +138,7 @@ void HttpCameraImpl::StreamThreadMain() {
SetConnected(true);

// stream
DeviceStream(conn->is, boundary.str());
DeviceStream(conn->is, boundary);
{
std::unique_lock lock(m_mutex);
m_streamConn = nullptr;
Expand All @@ -150,16 +149,16 @@ void HttpCameraImpl::StreamThreadMain() {
SetConnected(false);
}

wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
wpi::SmallVectorImpl<char>& boundary) {
std::pair<wpi::HttpConnection*, std::string>
HttpCameraImpl::DeviceStreamConnect() {
// Build the request
wpi::HttpRequest req;
{
std::scoped_lock lock(m_mutex);
if (m_locations.empty()) {
SERROR("locations array is empty!?");
std::this_thread::sleep_for(std::chrono::seconds(1));
return nullptr;
return {};
}
if (m_nextLocation >= m_locations.size()) {
m_nextLocation = 0;
Expand All @@ -173,7 +172,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
wpi::TCPConnector::connect(req.host.c_str(), req.port, m_logger, 1);

if (!m_active || !stream) {
return nullptr;
return {};
}

auto connPtr = std::make_unique<wpi::HttpConnection>(std::move(stream), 1);
Expand All @@ -191,22 +190,21 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
SWARNING("{}", warn);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
return {};
}

// Parse Content-Type header to get the boundary
auto [mediaType, contentType] = wpi::split(conn->contentType.str(), ';');
auto [mediaType, contentType] = wpi::split(conn->contentType, ';');
mediaType = wpi::trim(mediaType);
if (mediaType != "multipart/x-mixed-replace") {
SWARNING("\"{}\": unrecognized Content-Type \"{}\"", req.host.str(),
mediaType);
SWARNING("\"{}\": unrecognized Content-Type \"{}\"", req.host, mediaType);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
return {};
}

// media parameters
boundary.clear();
std::string boundary;
while (!contentType.empty()) {
std::string_view keyvalue;
std::tie(keyvalue, contentType) = wpi::split(contentType, ';');
Expand All @@ -222,14 +220,13 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
}

if (boundary.empty()) {
SWARNING("\"{}\": empty multi-part boundary or no Content-Type",
req.host.str());
SWARNING("\"{}\": empty multi-part boundary or no Content-Type", req.host);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
return {};
}

return conn;
return {conn, boundary};
}

void HttpCameraImpl::DeviceStream(wpi::raw_istream& is,
Expand Down Expand Up @@ -277,26 +274,25 @@ void HttpCameraImpl::DeviceStream(wpi::raw_istream& is,
bool HttpCameraImpl::DeviceStreamFrame(wpi::raw_istream& is,
std::string& imageBuf) {
// Read the headers
wpi::SmallString<64> contentTypeBuf;
wpi::SmallString<64> contentLengthBuf;
if (!ParseHttpHeaders(is, &contentTypeBuf, &contentLengthBuf)) {
std::string contentType;
std::string contentLength;
if (!ParseHttpHeaders(is, &contentType, &contentLength)) {
SWARNING("disconnected during headers");
PutError("disconnected during headers", wpi::Now());
return false;
}

// Check the content type (if present)
if (!contentTypeBuf.str().empty() &&
!wpi::starts_with(contentTypeBuf, "image/jpeg")) {
auto errMsg = fmt::format("received unknown Content-Type \"{}\"",
contentTypeBuf.str());
if (!contentType.empty() && !wpi::starts_with(contentType, "image/jpeg")) {
auto errMsg =
fmt::format("received unknown Content-Type \"{}\"", contentType);
SWARNING("{}", errMsg);
PutError(errMsg, wpi::Now());
return false;
}

int width, height;
if (auto v = wpi::parse_integer<unsigned int>(contentLengthBuf, 10)) {
if (auto v = wpi::parse_integer<unsigned int>(contentLength, 10)) {
// We know how big it is! Just get a frame of the right size and read
// the data directly into it.
unsigned int contentLength = v.value();
Expand Down Expand Up @@ -659,7 +655,7 @@ CS_Source CS_CreateHttpCamera(const struct WPI_String* name,
CS_Source CS_CreateHttpCameraMulti(const struct WPI_String* name,
const struct WPI_String* urls, int count,
CS_HttpCameraKind kind, CS_Status* status) {
wpi::SmallVector<std::string, 4> vec;
std::vector<std::string> vec;
vec.reserve(count);
for (int i = 0; i < count; ++i) {
vec.emplace_back(wpi::to_string_view(&urls[i]));
Expand All @@ -673,7 +669,7 @@ CS_HttpCameraKind CS_GetHttpCameraKind(CS_Source source, CS_Status* status) {

void CS_SetHttpCameraUrls(CS_Source source, const struct WPI_String* urls,
int count, CS_Status* status) {
wpi::SmallVector<std::string, 4> vec;
std::vector<std::string> vec;
vec.reserve(count);
for (int i = 0; i < count; ++i) {
vec.emplace_back(wpi::to_string_view(&urls[i]));
Expand Down
4 changes: 2 additions & 2 deletions cscore/src/main/native/cpp/HttpCameraImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>

#include <wpi/StringMap.h>
Expand Down Expand Up @@ -99,8 +100,7 @@ class HttpCameraImpl : public SourceImpl {
void StreamThreadMain();

// Functions used by StreamThreadMain()
wpi::HttpConnection* DeviceStreamConnect(
wpi::SmallVectorImpl<char>& boundary);
std::pair<wpi::HttpConnection*, std::string> DeviceStreamConnect();
void DeviceStream(wpi::raw_istream& is, std::string_view boundary);
bool DeviceStreamFrame(wpi::raw_istream& is, std::string& imageBuf);

Expand Down
15 changes: 7 additions & 8 deletions cscore/src/main/native/cpp/Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>
#include <utility>
#include <vector>

#include <wpi/Logger.h>
#include <wpinet/EventLoopRunner.h>
Expand Down Expand Up @@ -86,18 +87,16 @@ class Instance {
void DestroySource(CS_Source handle);
void DestroySink(CS_Sink handle);

std::span<CS_Source> EnumerateSourceHandles(
wpi::SmallVectorImpl<CS_Source>& vec) {
return m_sources.GetAll(vec);
std::vector<CS_Source> EnumerateSourceHandles() {
return m_sources.GetAll<CS_Source>();
}

std::span<CS_Sink> EnumerateSinkHandles(wpi::SmallVectorImpl<CS_Sink>& vec) {
return m_sinks.GetAll(vec);
std::vector<CS_Sink> EnumerateSinkHandles() {
return m_sinks.GetAll<CS_Sink>();
}

std::span<CS_Sink> EnumerateSourceSinks(CS_Source source,
wpi::SmallVectorImpl<CS_Sink>& vec) {
vec.clear();
std::vector<CS_Sink> EnumerateSourceSinks(CS_Source source) {
std::vector<CS_Sink> vec;
m_sinks.ForEach([&](CS_Sink sinkHandle, const SinkData& data) {
if (source == data.sourceHandle.load()) {
vec.push_back(sinkHandle);
Expand Down
Loading
Loading