mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
5510089d45
* Rename simdjson_really_inline -> simdjson_inline I want to change the simdjson_really_inline macro to sometimes not force inlining. After that upcoming change, the name simdjson_really_inline will no longer makes sense. Rename simdjson_really_inline to simdjson_inline. This patch should not change semantics; simdjson_inline still forces inlining as before. Some functions still need to be really inlined for ABI reasons. (GCC's -Wpsabi complains otherwise.) Leave those functions marked as simdjson_really_inline. * Improve build times for debug builds simdjson_inline is used for most simdjson functions. It forces inlining. In unoptimized/debug builds, this can lead to a lot of machine code being generated (especially with Address Sanitizer), causing slow compilation. Change simdjson_inline to force inlining only for optimized builds. Sometimes, the programmer might want a slightly-optimized build and want fast compilation (e.g. GCC's -Og mode). Allow simdjson users to define the simdjson_inline macro themselves (e.g. on the command line: -Dsimdjson_inline=inline) in cases where the default behavior is undesired. This patch reduced build times by over 75% for ondemand_object_tests.cpp with GCC 9.4.0 and CMAKE_BUILD_TYPE=Debug on my AMD 5950X: Before: 6.885 6.683 6.971 6.957 6.949 seconds (5 samples) After: 1.492 1.551 1.494 1.490 1.531 seconds (5 samples)
121 lines
3.9 KiB
C++
121 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#if SIMDJSON_EXCEPTIONS
|
|
|
|
#include "largerandom.h"
|
|
|
|
namespace largerandom {
|
|
|
|
using namespace simdjson;
|
|
using namespace simdjson::builtin::stage2;
|
|
|
|
class Sax {
|
|
public:
|
|
simdjson_inline bool Run(const padded_string &json) noexcept;
|
|
|
|
simdjson_inline const std::vector<my_point> &Result() { return container; }
|
|
simdjson_inline size_t ItemCount() { return container.size(); }
|
|
|
|
private:
|
|
simdjson_inline error_code RunNoExcept(const padded_string &json) noexcept;
|
|
error_code Allocate(size_t new_capacity);
|
|
std::unique_ptr<uint8_t[]> string_buf{};
|
|
size_t capacity{};
|
|
dom_parser_implementation dom_parser{};
|
|
std::vector<my_point> container{};
|
|
};
|
|
|
|
struct sax_point_reader_visitor {
|
|
public:
|
|
std::vector<my_point> &points;
|
|
enum {GOT_X=0, GOT_Y=1, GOT_Z=2, GOT_SOMETHING_ELSE=4};
|
|
size_t idx{GOT_SOMETHING_ELSE};
|
|
double buffer[3]={};
|
|
|
|
explicit sax_point_reader_visitor(std::vector<my_point> &_points) : points(_points) {}
|
|
|
|
simdjson_inline error_code visit_object_start(json_iterator &) {
|
|
idx = 0;
|
|
return SUCCESS;
|
|
}
|
|
simdjson_inline error_code visit_primitive(json_iterator &, const uint8_t *value) {
|
|
if(idx == GOT_SOMETHING_ELSE) { return simdjson::SUCCESS; }
|
|
return numberparsing::parse_double(value).get(buffer[idx]);
|
|
}
|
|
simdjson_inline error_code visit_object_end(json_iterator &) {
|
|
points.emplace_back(my_point{buffer[0], buffer[1], buffer[2]});
|
|
return SUCCESS;
|
|
}
|
|
|
|
simdjson_inline error_code visit_document_start(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_key(json_iterator &, const uint8_t * key) {
|
|
switch(key[1]) {
|
|
// Technically, we should check the other characters
|
|
// in the key, but we are cheating to go as fast
|
|
// as possible.
|
|
case 'x':
|
|
idx = GOT_X;
|
|
break;
|
|
case 'y':
|
|
idx = GOT_Y;
|
|
break;
|
|
case 'z':
|
|
idx = GOT_Z;
|
|
break;
|
|
default:
|
|
idx = GOT_SOMETHING_ELSE;
|
|
}
|
|
return SUCCESS;
|
|
}
|
|
simdjson_inline error_code visit_array_start(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_array_end(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_document_end(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_empty_array(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_empty_object(json_iterator &) { return SUCCESS; }
|
|
simdjson_inline error_code visit_root_primitive(json_iterator &, const uint8_t *) { return SUCCESS; }
|
|
simdjson_inline error_code increment_count(json_iterator &) { return SUCCESS; }
|
|
};
|
|
|
|
// NOTE: this assumes the dom_parser is already allocated
|
|
bool Sax::Run(const padded_string &json) noexcept {
|
|
auto error = RunNoExcept(json);
|
|
if (error) { std::cerr << error << std::endl; return false; }
|
|
return true;
|
|
}
|
|
|
|
error_code Sax::RunNoExcept(const padded_string &json) noexcept {
|
|
container.clear();
|
|
|
|
// Allocate capacity if needed
|
|
if (capacity < json.size()) {
|
|
SIMDJSON_TRY( Allocate(json.size()) );
|
|
}
|
|
|
|
// Run stage 1 first.
|
|
SIMDJSON_TRY( dom_parser.stage1(json.u8data(), json.size(), false) );
|
|
|
|
// Then walk the document, parsing the tweets as we go
|
|
json_iterator iter(dom_parser, 0);
|
|
sax_point_reader_visitor visitor(container);
|
|
SIMDJSON_TRY( iter.walk_document<false>(visitor) );
|
|
return SUCCESS;
|
|
}
|
|
|
|
error_code Sax::Allocate(size_t new_capacity) {
|
|
// string_capacity copied from document::allocate
|
|
size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 + SIMDJSON_PADDING, 64);
|
|
string_buf.reset(new (std::nothrow) uint8_t[string_capacity]);
|
|
if (auto error = dom_parser.set_capacity(new_capacity)) { return error; }
|
|
if (capacity == 0) { // set max depth the first time only
|
|
if (auto error = dom_parser.set_max_depth(DEFAULT_MAX_DEPTH)) { return error; }
|
|
}
|
|
capacity = new_capacity;
|
|
return SUCCESS;
|
|
}
|
|
|
|
BENCHMARK_TEMPLATE(LargeRandom, Sax);
|
|
|
|
} // namespace largerandom
|
|
|
|
#endif // SIMDJSON_EXCEPTIONS
|