File: yyjson.h

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (72 lines) | stat: -rw-r--r-- 2,220 bytes parent folder | download
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
65
66
67
68
69
70
71
72
#pragma once

#ifdef SIMDJSON_COMPETITION_YYJSON

#include "large_random.h"

namespace large_random {

struct yyjson_base {
  static constexpr diff_flags DiffFlags = diff_flags::NONE;

  simdjson_inline double get_double(yyjson_val *obj, std::string_view key) {
    yyjson_val *val = yyjson_obj_getn(obj, key.data(), key.length());
    if (!val) { throw "missing point field!"; }
    if (yyjson_get_type(val) != YYJSON_TYPE_NUM) { throw "Number is not a type!"; }

    switch (yyjson_get_subtype(val)) {
      case YYJSON_SUBTYPE_UINT:
        return double(yyjson_get_uint(val));
      case YYJSON_SUBTYPE_SINT:
        return double(yyjson_get_sint(val));
      case YYJSON_SUBTYPE_REAL:
        return yyjson_get_real(val);
      default:
        SIMDJSON_UNREACHABLE();
    }
    SIMDJSON_UNREACHABLE();
    return 0.0; // unreachable
  }

  bool run(yyjson_doc *doc, std::vector<point> &result) {
    if (!doc) { return false; }
    yyjson_val *coords = yyjson_doc_get_root(doc);
    if (!yyjson_is_arr(coords)) { return false; }

    // Walk the document, parsing the tweets as we go
    size_t idx, max;
    yyjson_val *coord;
    yyjson_arr_foreach(coords, idx, max, coord) {
      if (!yyjson_is_obj(coord)) { return false; }
      result.emplace_back(json_benchmark::point{get_double(coord, "x"), get_double(coord, "y"), get_double(coord, "z")});
    }

    return true;
  }
};

struct yyjson : yyjson_base {
  bool run(simdjson::padded_string &json, std::vector<point> &result) {
    yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
    bool b = yyjson_base::run(doc, result);
    yyjson_doc_free(doc);
    return b;
  }
};
BENCHMARK_TEMPLATE(large_random, yyjson)->UseManualTime();

#if SIMDJSON_COMPETITION_ONDEMAND_INSITU
struct yyjson_insitu : yyjson_base {
  bool run(simdjson::padded_string &json, std::vector<point> &result) {
    yyjson_doc *doc = yyjson_read_opts(json.data(), json.size(), YYJSON_READ_INSITU, 0, 0);
    bool b = yyjson_base::run(doc, result);
    yyjson_doc_free(doc);
    return b;
  }
};
BENCHMARK_TEMPLATE(large_random, yyjson_insitu)->UseManualTime();
#endif // SIMDJSON_COMPETITION_ONDEMAND_INSITU

} // namespace large_random

#endif // SIMDJSON_COMPETITION_YYJSON