Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
93 changes: 58 additions & 35 deletions src/ResultsIpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

using namespace FiftyoneDegrees;
using namespace FiftyoneDegrees::Common;

// Thread-local storage for performance optimization
thread_local std::stringstream FiftyoneDegrees::IpIntelligence::ResultsIpi::tlsReusableStream;
thread_local std::vector<uint8_t> FiftyoneDegrees::IpIntelligence::ResultsIpi::tlsReusableByteVector;
using namespace FiftyoneDegrees::IpIntelligence;

#define RESULT(r,i) ((ResultIpi*)r->b.items + i)
Expand Down Expand Up @@ -446,8 +450,6 @@ IpIntelligence::ResultsIpi::getValuesAsWeightedUTF8StringList(

vector<WeightedValue<std::vector<uint8_t>>> values;
Common::Value<vector<WeightedValue<std::vector<uint8_t>>>> result;
stringstream stream;
std::vector<uint8_t> byteVector;
iterateWeightedValues(
requiredPropertyIndex,
[&result](const fiftyoneDegreesResultsNoValueReason reason, const char * const reasonStr) {
Expand All @@ -456,45 +458,66 @@ IpIntelligence::ResultsIpi::getValuesAsWeightedUTF8StringList(
[&values](const uint32_t count) {
values.reserve(count);
},
[&values, &stream, &byteVector](
const StoredBinaryValue * const binaryValue,
[this, &values](
const StoredBinaryValue* const binaryValue,
const PropertyValueType storedValueType,
const uint16_t rawWeighting,
Exception * const exception) {
WeightedValue<std::vector<uint8_t>> weightedByteVector;
if (storedValueType == FIFTYONE_DEGREES_PROPERTY_VALUE_TYPE_STRING) {
const String * const rawString = reinterpret_cast<const String *>(binaryValue);
byteVector.reserve(rawString->size);
if (rawString->size) {
const uint8_t * const firstByte = reinterpret_cast<const uint8_t *>(&rawString->value);
const uint8_t * pastLastByte = firstByte + rawString->size;
// strip NUL-terminator
if (!*(pastLastByte - 1)) {
--pastLastByte;
Exception* const exception) {

// Clear buffer. Preserve capacity for performance
tlsReusableByteVector.clear();

if (storedValueType == FIFTYONE_DEGREES_PROPERTY_VALUE_TYPE_STRING) {
const String* const rawString = reinterpret_cast<const String*>(binaryValue);
if (rawString->size) {
// Increase capacity if capacity reached.
if (tlsReusableByteVector.capacity() < rawString->size) {
tlsReusableByteVector.reserve(rawString->size * 3 / 2);
}
const uint8_t* const firstByte = reinterpret_cast<const uint8_t*>(&rawString->value);
const uint8_t* pastLastByte = firstByte + rawString->size;
// strip NUL-terminator
if (!*(pastLastByte - 1)) {
--pastLastByte;
}
tlsReusableByteVector.assign(firstByte, pastLastByte);
}
byteVector.assign(firstByte, pastLastByte);
}
} else {
// Clear stream before the construction
stream.str("");
writeStoredBinaryValueToStringStream(
binaryValue,
storedValueType,
stream,
DefaultWktDecimalPlaces,
exception);
EXCEPTION_THROW;
const std::string valueAsString = stream.str();
byteVector.reserve(valueAsString.size());
const uint8_t * const firstByte = reinterpret_cast<const uint8_t *>(valueAsString.c_str());
byteVector.assign(firstByte, firstByte + valueAsString.size());
}
weightedByteVector.setValue(byteVector);
weightedByteVector.setRawWeight(rawWeighting);
values.push_back(weightedByteVector);
else {
// Reuse thread-local string stream.
// Clear state but keep the buffer
tlsReusableStream.str("");
tlsReusableStream.clear();

writeStoredBinaryValueToStringStream(
binaryValue,
storedValueType,
tlsReusableStream,
DefaultWktDecimalPlaces,
exception);
EXCEPTION_THROW;

const std::string valueAsString = tlsReusableStream.str();

// Ensure sufficient capacity for byte vector
if (tlsReusableByteVector.capacity() < valueAsString.size()) {
tlsReusableByteVector.reserve(valueAsString.size() * 3 / 2);
}

const uint8_t* const firstByte = reinterpret_cast<const uint8_t*>(valueAsString.c_str());
tlsReusableByteVector.assign(firstByte, firstByte + valueAsString.size());
}

// Use emplace_back for efficient construction and move semantics
values.emplace_back();
values.back().setValue(std::move(tlsReusableByteVector));
values.back().setRawWeight(rawWeighting);

// Reset for next iteration (keeps capacity)
tlsReusableByteVector.clear();
},
[&result, &values] {
result.setValue(values);
result.setValue(std::move(values));
});
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ResultsIpi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ namespace FiftyoneDegrees {
const std::function<void()>& onAfterValues);

fiftyoneDegreesResultsIpi *results;

// Reusable buffers
static thread_local std::stringstream tlsReusableStream;
static thread_local std::vector<uint8_t> tlsReusableByteVector;
};
}
}
Expand Down