summaryrefslogtreecommitdiff
path: root/benchmarks/parsestream.upb.c
blob: 2d342061f29c6653106dbb3944cee7929b70dd60 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

#include "main.c"

#include <stdlib.h>
#include "upb/bytestream.h"
#include "upb/def.h"
#include "upb/pb/decoder.h"
#include "upb/pb/glue.h"

static char *input_str;
static size_t input_len;
static upb_msgdef *def;
static upb_decoder decoder;
static upb_stringsrc stringsrc;

static upb_sflow_t startsubmsg(void *_m, upb_value fval) {
  (void)_m;
  (void)fval;
  return UPB_CONTINUE_WITH(NULL);
}

static upb_flow_t value(void *closure, upb_value fval, upb_value val) {
  (void)closure;
  (void)fval;
  (void)val;
  return UPB_CONTINUE;
}

static bool initialize()
{
  // Initialize upb state, decode descriptor.
  upb_status status = UPB_STATUS_INIT;
  upb_symtab *s = upb_symtab_new();
  upb_read_descriptorfile(s, MESSAGE_DESCRIPTOR_FILE, &status);
  if(!upb_ok(&status)) {
    fprintf(stderr, "Error reading descriptor: %s\n",
            upb_status_getstr(&status));
    return false;
  }

  def = upb_dyncast_msgdef(upb_symtab_lookup(s, MESSAGE_NAME));
  if(!def) {
    fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME);
    return false;
  }
  upb_symtab_unref(s);

  // Read the message data itself.
  input_str = upb_readfile(MESSAGE_FILE, &input_len);
  if(input_str == NULL) {
    fprintf(stderr, "Error reading " MESSAGE_FILE "\n");
    return false;
  }

  upb_handlers *handlers = upb_handlers_new();
  if (!JIT) handlers->should_jit = false;
  // Cause all messages to be read, but do nothing when they are.
  upb_handlerset hset = {NULL, NULL, value, startsubmsg, NULL, NULL, NULL};
  upb_handlers_reghandlerset(handlers, def, &hset);
  upb_decoder_init(&decoder, handlers);
  upb_handlers_unref(handlers);
  upb_stringsrc_init(&stringsrc);
  return true;
}

static void cleanup()
{
  free(input_str);
  upb_def_unref(UPB_UPCAST(def));
  upb_decoder_uninit(&decoder);
  upb_stringsrc_uninit(&stringsrc);
}

static size_t run(int i)
{
  (void)i;
  upb_status status = UPB_STATUS_INIT;
  upb_stringsrc_reset(&stringsrc, input_str, input_len);
  upb_decoder_reset(&decoder, upb_stringsrc_bytesrc(&stringsrc),
                    0, UPB_NONDELIMITED, NULL);
  upb_decoder_decode(&decoder, &status);
  if(!upb_ok(&status)) goto err;
  return input_len;

err:
  fprintf(stderr, "Decode error: %s", upb_status_getstr(&status));
  return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback