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

Implement fast numeric printing routines. #5772

Merged
merged 16 commits into from
Jun 17, 2020
1 change: 1 addition & 0 deletions amalgamation/xgboost-all0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "../src/learner.cc"
#include "../src/logging.cc"
#include "../src/common/common.cc"
#include "../src/common/charconv.cc"
#include "../src/common/timer.cc"
#include "../src/common/host_device_vector.cc"
#include "../src/common/hist_util.cc"
Expand Down
12 changes: 3 additions & 9 deletions include/xgboost/json.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*!
* Copyright (c) by Contributors 2019
* Copyright (c) by XGBoost Contributors 2019-2020
*/
#ifndef XGBOOST_JSON_H_
#define XGBOOST_JSON_H_

#include <xgboost/logging.h>
#include <xgboost/parameter.h>
#include <string>

#include <map>
#include <memory>
#include <vector>
#include <functional>
#include <utility>
#include <string>

namespace xgboost {

Expand Down Expand Up @@ -331,13 +331,7 @@ class Json {
static Json Load(StringView str);
/*! \brief Pass your own JsonReader. */
static Json Load(JsonReader* reader);
/*! \brief Dump json into stream. */
static void Dump(Json json, std::ostream* stream,
bool pretty = ConsoleLogger::ShouldLog(
ConsoleLogger::LogVerbosity::kDebug));
static void Dump(Json json, std::string* out,
bool pretty = ConsoleLogger::ShouldLog(
ConsoleLogger::LogVerbosity::kDebug));
static void Dump(Json json, std::string* out);

Json() : ptr_{new JsonNull} {}

Expand Down
62 changes: 15 additions & 47 deletions include/xgboost/json_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#ifndef XGBOOST_JSON_IO_H_
#define XGBOOST_JSON_IO_H_
#include <xgboost/json.h>
#include <xgboost/base.h>

#include <vector>
#include <memory>
#include <string>
#include <cinttypes>
Expand All @@ -15,20 +17,6 @@
#include <locale>

namespace xgboost {

template <typename Allocator>
class FixedPrecisionStreamContainer : public std::basic_stringstream<
char, std::char_traits<char>, Allocator> {
public:
FixedPrecisionStreamContainer() {
this->precision(std::numeric_limits<double>::max_digits10);
this->imbue(std::locale("C"));
this->setf(std::ios::scientific);
}
};

using FixedPrecisionStream = FixedPrecisionStreamContainer<std::allocator<char>>;

/*
* \brief A json reader, currently error checking and utf-8 is not fully supported.
*/
Expand All @@ -45,13 +33,11 @@ class JsonReader {
SourceLocation() = default;
size_t Pos() const { return pos_; }

SourceLocation& Forward() {
void Forward() {
pos_++;
return *this;
}
SourceLocation& Forward(uint32_t n) {
void Forward(uint32_t n) {
pos_ += n;
return *this;
}
} cursor_;

Expand Down Expand Up @@ -82,9 +68,9 @@ class JsonReader {
return GetNextChar();
}

char GetChar(char c) {
char result = GetNextNonSpaceChar();
if (result != c) { Expect(c, result); }
char GetConsecutiveChar(char c) {
char result = GetNextChar();
if (XGBOOST_EXPECT(result != c, false)) { Expect(c, result); }
trivialfis marked this conversation as resolved.
Show resolved Hide resolved
return result;
}

Expand All @@ -95,7 +81,11 @@ class JsonReader {
std::string msg = "Expecting: \"";
msg += c;
msg += "\", got: \"";
msg += std::string {got} + " \"";
if (got == -1) {
msg += "EOF\"";
} else {
msg += std::to_string(got) + " \"";
}
Error(msg);
}

Expand All @@ -119,38 +109,16 @@ class JsonReader {

class JsonWriter {
static constexpr size_t kIndentSize = 2;
FixedPrecisionStream convertor_;

size_t n_spaces_;
std::ostream* stream_;
bool pretty_;
std::vector<char>* stream_;

public:
JsonWriter(std::ostream* stream, bool pretty) :
n_spaces_{0}, stream_{stream}, pretty_{pretty} {}
explicit JsonWriter(std::vector<char>* stream) :
n_spaces_{0}, stream_{stream} {}

virtual ~JsonWriter() = default;

void NewLine() {
if (pretty_) {
*stream_ << u8"\n" << std::string(n_spaces_, ' ');
}
}

void BeginIndent() {
n_spaces_ += kIndentSize;
}
void EndIndent() {
n_spaces_ -= kIndentSize;
}

void Write(std::string str) {
*stream_ << str;
}
void Write(StringView str) {
stream_->write(str.c_str(), str.size());
}

void Save(Json json);

virtual void Visit(JsonArray const* arr);
Expand Down
Loading