File: sajson.h

package info (click to toggle)
simdjson 4.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 31,396 kB
  • sloc: cpp: 195,760; ansic: 20,954; sh: 1,126; python: 885; makefile: 47; ruby: 25; javascript: 13
file content (64 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once

#ifdef SIMDJSON_COMPETITION_SAJSON

#include "large_random.h"

namespace large_random {

struct sajson {
  static constexpr diff_flags DiffFlags = diff_flags::IMPRECISE_FLOATS;

  size_t ast_buffer_size{0};
  size_t *ast_buffer{nullptr};
  ~sajson() { free(ast_buffer); }

  simdjson_inline double get_double(const ::sajson::value &obj, std::string_view key) {
    using namespace sajson;

    auto val = obj.get_value_of_key({key.data(), key.length()});
    switch (val.get_type()) {
      case TYPE_INTEGER:
      case TYPE_DOUBLE:
        return val.get_number_value();
      default:
        throw "field not double";
    }
  }

  bool run(simdjson::padded_string &json, std::vector<point> &result) {
    using namespace sajson;

    if (!ast_buffer) {
      ast_buffer_size = json.size();
      ast_buffer = (size_t *)std::malloc(ast_buffer_size * sizeof(size_t));
    }
    auto doc = parse(
      bounded_allocation(ast_buffer, ast_buffer_size),
      mutable_string_view(json.size(), json.data())
    );
    if (!doc.is_valid()) { return false; }

    auto points = doc.get_root();
    if (points.get_type() != TYPE_ARRAY) { return false; }

    for (size_t i=0; i<points.get_length(); i++) {
      auto point = points.get_array_element(i);
      if (point.get_type() != TYPE_OBJECT) { return false; }
      result.emplace_back(json_benchmark::point{
        get_double(point, "x"),
        get_double(point, "y"),
        get_double(point, "z")
      });
    }

    return true;
  }
};

BENCHMARK_TEMPLATE(large_random, sajson)->UseManualTime();

} // namespace large_random

#endif // SIMDJSON_COMPETITION_SAJSON