Improve string view to reduce string allocation. (#6644)

This commit is contained in:
Jiaming Yuan 2021-01-27 19:08:52 +08:00 committed by GitHub
parent bc08e0c9d1
commit 1b70a323a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*! /*!
* Copyright (c) by XGBoost Contributors 2019-2020 * Copyright (c) by XGBoost Contributors 2019-2021
*/ */
#ifndef XGBOOST_JSON_H_ #ifndef XGBOOST_JSON_H_
#define XGBOOST_JSON_H_ #define XGBOOST_JSON_H_
@ -301,12 +301,14 @@ class JsonBoolean : public Value {
struct StringView { struct StringView {
private: private:
using CharT = char; // unsigned char using CharT = char; // unsigned char
using Traits = std::char_traits<CharT>;
CharT const* str_; CharT const* str_;
size_t size_; size_t size_;
public: public:
StringView() = default; StringView() = default;
StringView(CharT const* str, size_t size) : str_{str}, size_{size} {} StringView(CharT const* str, size_t size) : str_{str}, size_{size} {}
explicit StringView(CharT const* str) : str_{str}, size_{Traits::length(str)} {}
CharT const& operator[](size_t p) const { return str_[p]; } CharT const& operator[](size_t p) const { return str_[p]; }
CharT const& at(size_t p) const { // NOLINT CharT const& at(size_t p) const { // NOLINT
@ -322,9 +324,16 @@ struct StringView {
CHECK_LE(beg, size_); CHECK_LE(beg, size_);
return std::string {str_ + beg, n < (size_ - beg) ? n : (size_ - beg)}; return std::string {str_ + beg, n < (size_ - beg) ? n : (size_ - beg)};
} }
char const* c_str() const { return str_; } // NOLINT CharT const* c_str() const { return str_; } // NOLINT
CharT const* cbegin() const { return str_; } // NOLINT
CharT const* cend() const { return str_ + size(); } // NOLINT
CharT const* begin() const { return str_; } // NOLINT
CharT const* end() const { return str_ + size(); } // NOLINT
}; };
std::ostream &operator<<(std::ostream &os, StringView const v);
/*! /*!
* \brief Data structure representing JSON format. * \brief Data structure representing JSON format.
* *

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014-2020 by Contributors // Copyright (c) 2014-2021 by Contributors
#include <rabit/rabit.h> #include <rabit/rabit.h>
#include <rabit/c_api.h> #include <rabit/c_api.h>
@ -50,8 +50,7 @@ XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char*)) {
XGB_DLL int XGBSetGlobalConfig(const char* json_str) { XGB_DLL int XGBSetGlobalConfig(const char* json_str) {
API_BEGIN(); API_BEGIN();
std::string str{json_str}; Json config{Json::Load(StringView{json_str})};
Json config{Json::Load(StringView{str.data(), str.size()})};
for (auto& items : get<Object>(config)) { for (auto& items : get<Object>(config)) {
switch (items.second.GetValue().Type()) { switch (items.second.GetValue().Type()) {
case xgboost::Value::ValueKind::kInteger: { case xgboost::Value::ValueKind::kInteger: {
@ -514,8 +513,7 @@ XGB_DLL int XGBoosterBoostedRounds(BoosterHandle handle, int* out) {
XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const* json_parameters) { XGB_DLL int XGBoosterLoadJsonConfig(BoosterHandle handle, char const* json_parameters) {
API_BEGIN(); API_BEGIN();
CHECK_HANDLE(); CHECK_HANDLE();
std::string str {json_parameters}; Json config { Json::Load(StringView{json_parameters}) };
Json config { Json::Load(StringView{str.c_str(), str.size()}) };
static_cast<Learner*>(handle)->LoadConfig(config); static_cast<Learner*>(handle)->LoadConfig(config);
API_END(); API_END();
} }

View File

@ -1,5 +1,5 @@
/*! /*!
* Copyright (c) by Contributors 2019-2020 * Copyright (c) by Contributors 2019-2021
*/ */
#include <cctype> #include <cctype>
#include <cstddef> #include <cstddef>
@ -744,4 +744,11 @@ void Json::Dump(Json json, std::string* str) {
} }
Json& Json::operator=(Json const &other) = default; Json& Json::operator=(Json const &other) = default;
std::ostream &operator<<(std::ostream &os, StringView const v) {
for (auto c : v) {
os.put(c);
}
return os;
}
} // namespace xgboost } // namespace xgboost

View File

@ -1,5 +1,5 @@
/*! /*!
* Copyright (c) by Contributors 2019 * Copyright (c) by Contributors 2019-2021
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <dmlc/filesystem.h> #include <dmlc/filesystem.h>
@ -574,4 +574,14 @@ TEST(Json, DISABLED_RoundTripExhaustive) {
test(static_cast<uint32_t>(i)); test(static_cast<uint32_t>(i));
} }
} }
TEST(StringView, Basic) {
StringView str{"This is a string."};
std::stringstream ss;
ss << str;
std::string res = ss.str();
ASSERT_EQ(str.size(), res.size());
ASSERT_TRUE(std::equal(res.cbegin(), res.cend(), str.cbegin()));
}
} // namespace xgboost } // namespace xgboost