Skip to content

ytypes, util: avoid eager pretty-printing in debug-only unmarshal logs - #1127

Open
eoswald wants to merge 1 commit into
openconfig:masterfrom
eoswald:debug-print-lazy
Open

eoswald wants to merge 1 commit into
openconfig:masterfrom
eoswald:debug-print-lazy

Conversation

@eoswald

@eoswald eoswald commented Nov 19, 2026 •

Copy link
Copy Markdown

TLDR: Stop pretty-printing whole subtrees for three debug-only log calls on the unmarshal path when debugging is off. On
a 100-interface × 10-subinterface Unmarshal, time drops by ~61%, bytes allocated by ~79% and allocations by ~44%.

Problem

Three util.DbgPrint calls on the unmarshal path built their argument with pretty.Sprint(...) before DbgPrint checked it
s debugLibrary flag, which is off by default. So every unmarshalled container, list and map entry pretty-printed its whole
subtree, and the result was thrown away:

File Log call
ytypes/container.go "container after unmarshal"
ytypes/list.go "list after unmarshal"
util/reflect.go InsertIntoMap

Because each container prints its whole subtree, and nested containers print again at each level, the wasted work grows with
tree depth as well as size.

Change

  • Pass util.ValueStrDebug(...) instead, as the neighbouring debug calls already do. It returns "<not calculated>" unless
    debugging is enabled, and is otherwise ValueStr.
  • Remove the now-unused github.com/kylelemons/godebug/pretty imports from all three files.
  • Add BenchmarkUnmarshal in ytypes/schema_tests/validate_test.go: 100 interfaces × 10 subinterfaces of exampleoc (each
    with a description and one IPv4 address), emitted as RFC 7951 JSON and unmarshalled into a fresh oc.Device on each iteration.

With debugging disabled, behaviour is unchanged. With debugging enabled, these three lines print ValueStr output instead of
pretty.Sprint output.

Benchmarks (master before vs. this PR after)

BenchmarkUnmarshal was added to master (a1f7f9f) unchanged for the "before" run. Before and after runs were alternated
so machine noise affects both sides equally, then compared with benchstat:

B='-run=^$ -bench=^BenchmarkUnmarshal$ -benchmem -benchtime=1s'
for i in 1 2 3 4 5 6; do
  (cd before/ytypes/schema_tests && go test $B -count=1 .) >> before.txt
  (cd after/ytypes/schema_tests  && go test $B -count=1 .) >> after.txt
done
benchstat before.txt after.txt
goos: linux
goarch: amd64
pkg: github.com/openconfig/ygot/ytypes/schema_tests
cpu: Intel Xeon Processor (Cascadelake)
            │   before    │                after                │
            │   sec/op    │    sec/op     vs base               │
Unmarshal-4   502.4m ± 25%   196.0m ± 31%  -60.98% (p=0.002 n=6)

            │   before    │                after                │
            │    B/op     │     B/op      vs base               │
Unmarshal-4  246.24Mi ± 0%   50.77Mi ± 0%  -79.38% (p=0.002 n=6)

            │   before    │               after                │
            │  allocs/op  │  allocs/op   vs base               │
Unmarshal-4    3.154M ± 0%   1.766M ± 0%  -43.99% (p=0.002 n=6)

How to read it:

  • B/op and allocs/op are deterministic (± 0% across all 12 runs), so the 79% and 44% drops are exact, not noise. They match the removed work: pretty.Sprint allocates a string for every subtree it prints.
  • sec/op is noisy (± 25–31%; shared 4-vCPU VM). The effect is still much larger than the spread: the slowest "after"
    run is well below the fastest "before" run.
  • p=0.002 is the smallest p-value benchstat's Mann-Whitney U test can report with n=6 per side, meaning every "after" sample beat every "before" sample.

Go benchmarks don't fail on a slowdown, so BenchmarkUnmarshal is evidence for this change and a tool for future comparisons, not a regression gate.

Three util.DbgPrint calls on the unmarshal path built their argument
with pretty.Sprint before DbgPrint checked its off-by-default
debugLibrary flag, so every unmarshalled container, list and map entry
pretty-printed its whole subtree even with debugging disabled:

  - ytypes/container.go: "container after unmarshal"
  - ytypes/list.go: "list after unmarshal"
  - util/reflect.go: InsertIntoMap

Pass util.ValueStrDebug(...) instead, as the neighbouring debug calls
already do; it returns "<not calculated>" unless debugging is enabled
and is otherwise ValueStr. Remove the now-unused pretty imports.

Add BenchmarkUnmarshal in ytypes/schema_tests (100 interfaces x 10
subinterfaces of exampleoc, emitted as RFC7951 JSON).
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 88.775%. remained the same — eoswald:debug-print-lazy into openconfig:master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants