Merged common encode tag paths.

pull/13171/head
Joshua Haberman 4 years ago
parent 5d7dc718cc
commit 9b31e8fe12
  1. 21
      benchmark.py
  2. 36
      upb/encode.c

@ -7,7 +7,7 @@ import re
def Run(cmd): def Run(cmd):
subprocess.check_call(cmd, shell=True) subprocess.check_call(cmd, shell=True)
def RunAgainstBranch(branch, outfile, runs=12): def RunAgainstBranch(branch, outbase, runs=12):
tmpfile = "/tmp/bench-output.json" tmpfile = "/tmp/bench-output.json"
Run("rm -rf {}".format(tmpfile)) Run("rm -rf {}".format(tmpfile))
Run("git checkout {}".format(branch)) Run("git checkout {}".format(branch))
@ -15,10 +15,13 @@ def RunAgainstBranch(branch, outfile, runs=12):
Run("./bazel-bin/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={}".format(tmpfile, runs)) Run("./bazel-bin/benchmark --benchmark_out_format=json --benchmark_out={} --benchmark_repetitions={}".format(tmpfile, runs))
Run("bazel build -c opt --copt=-g :conformance_upb")
Run("cp -f bazel-bin/conformance_upb {}.bin".format(outbase))
with open(tmpfile) as f: with open(tmpfile) as f:
bench_json = json.load(f) bench_json = json.load(f)
with open(outfile, "w") as f: with open(outbase + ".txt", "w") as f:
for run in bench_json["benchmarks"]: for run in bench_json["benchmarks"]:
name = re.sub(r'^BM_', 'Benchmark', run["name"]) name = re.sub(r'^BM_', 'Benchmark', run["name"])
if name.endswith("_mean") or name.endswith("_median") or name.endswith("_stddev"): if name.endswith("_mean") or name.endswith("_median") or name.endswith("_stddev"):
@ -26,7 +29,17 @@ def RunAgainstBranch(branch, outfile, runs=12):
values = (name, run["iterations"], run["cpu_time"]) values = (name, run["iterations"], run["cpu_time"])
print("{} {} {} ns/op".format(*values), file=f) print("{} {} {} ns/op".format(*values), file=f)
RunAgainstBranch("master", "/tmp/old.txt") RunAgainstBranch("6e140c267cc9bf6a4ef89d3f9a842209d7537599", "/tmp/old")
RunAgainstBranch("decoder", "/tmp/new.txt") RunAgainstBranch("encoder", "/tmp/new")
print()
print()
Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt") Run("~/go/bin/benchstat /tmp/old.txt /tmp/new.txt")
print()
print()
Run("objcopy --strip-debug /tmp/old.bin /tmp/old.bin.stripped")
Run("objcopy --strip-debug /tmp/new.bin /tmp/new.bin.stripped")
Run("~/code/bloaty/bloaty /tmp/new.bin.stripped -- /tmp/old.bin.stripped --debug-file=/tmp/old.bin --debug-file=/tmp/new.bin -d compileunits,symbols")

@ -154,15 +154,18 @@ static void encode_scalar(upb_encstate *e, const void *_field_mem,
const upb_msglayout *m, const upb_msglayout_field *f, const upb_msglayout *m, const upb_msglayout_field *f,
bool skip_zero_value) { bool skip_zero_value) {
const char *field_mem = _field_mem; const char *field_mem = _field_mem;
#define CASE(ctype, type, wire_type, encodeval) do { \ int wire_type;
ctype val = *(ctype*)field_mem; \
if (skip_zero_value && val == 0) { \ #define CASE(ctype, type, wtype, encodeval) \
return; \ { \
} \ ctype val = *(ctype *)field_mem; \
encode_ ## type(e, encodeval); \ if (skip_zero_value && val == 0) { \
encode_tag(e, f->number, wire_type); \ return; \
return; \ } \
} while(0) encode_##type(e, encodeval); \
wire_type = wtype; \
break; \
}
switch (f->descriptortype) { switch (f->descriptortype) {
case UPB_DESCRIPTOR_TYPE_DOUBLE: case UPB_DESCRIPTOR_TYPE_DOUBLE:
@ -197,8 +200,8 @@ static void encode_scalar(upb_encstate *e, const void *_field_mem,
} }
encode_bytes(e, view.data, view.size); encode_bytes(e, view.data, view.size);
encode_varint(e, view.size); encode_varint(e, view.size);
encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED); wire_type = UPB_WIRE_TYPE_DELIMITED;
return; break;
} }
case UPB_DESCRIPTOR_TYPE_GROUP: { case UPB_DESCRIPTOR_TYPE_GROUP: {
size_t size; size_t size;
@ -209,8 +212,8 @@ static void encode_scalar(upb_encstate *e, const void *_field_mem,
} }
encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP); encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
encode_message(e, submsg, subm, &size); encode_message(e, submsg, subm, &size);
encode_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP); wire_type = UPB_WIRE_TYPE_START_GROUP;
return; break;
} }
case UPB_DESCRIPTOR_TYPE_MESSAGE: { case UPB_DESCRIPTOR_TYPE_MESSAGE: {
size_t size; size_t size;
@ -221,12 +224,13 @@ static void encode_scalar(upb_encstate *e, const void *_field_mem,
} }
encode_message(e, submsg, subm, &size); encode_message(e, submsg, subm, &size);
encode_varint(e, size); encode_varint(e, size);
encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED); wire_type = UPB_WIRE_TYPE_DELIMITED;
return; break;
} }
} }
#undef CASE #undef CASE
UPB_UNREACHABLE();
encode_tag(e, f->number, wire_type);
} }
static void encode_array(upb_encstate *e, const char *field_mem, static void encode_array(upb_encstate *e, const char *field_mem,

Loading…
Cancel
Save