Skip to content

commitment: replace trace bool with io.Writer for structured trace output#20316

Closed
awskii wants to merge 5 commits into
mainfrom
awskii/trace-writer
Closed

commitment: replace trace bool with io.Writer for structured trace output#20316
awskii wants to merge 5 commits into
mainfrom
awskii/trace-writer

Conversation

@awskii

@awskii awskii commented Apr 3, 2026

Copy link
Copy Markdown
Member

Summary

Replace the scattered hph.trace bool + fmt.Printf pattern with a single hph.traceW io.Writer field. Trace output is zero-cost when traceW == nil.

Changes

  • hph.trace boolhph.traceW io.Writer on HexPatriciaHashed
  • SetTrace(bool)SetTraceWriter(io.Writer) on Trie interface
  • All 69+ if hph.trace { fmt.Printf(...) } sites → if hph.traceW != nil { fmt.Fprintf(hph.traceW, ...) }
  • Unconditional [witness] fmt.Printf sites wrapped in traceW guard
  • HexConcurrentPatriciaHashed propagates SetTraceWriter to root + 16 mounts
  • commitment_context.go updated to use SetTraceWriter

Benefits

  • Configurable destination: stdout, file, bytes.Buffer for tests
  • Zero overhead when disabled (nil check vs bool — same cost, but arguments to fmt.Printf are not evaluated)
  • Testable: pass &bytes.Buffer{} to capture trace for assertions

Related

awskii added 5 commits April 3, 2026 16:37
…tput

Replace hph.trace bool with hph.traceW io.Writer across the commitment
package. nil = disabled (zero-cost check), non-nil = writes trace output
to the provided writer. Update Trie interface SetTrace -> SetTraceWriter,
convert all 78 trace sites, and update all callers.
All tasks done: replaced hph.trace bool with hph.traceW io.Writer
for structured trace output across the commitment package.
- Add syncWriter mutex wrapper in ConcurrentPatriciaHashed.SetTraceWriter
  to prevent data races when concurrent mount goroutines write to the
  same io.Writer (e.g. *bytes.Buffer from CommitmentCapture)
- Capture traceW in local var in foldMounted defer to prevent latent
  nil-dereference if writer is reset before deferred func runs
- Revert commented-out debug lines from fmt.Fprintf(hph.traceW,...) back
  to fmt.Printf to avoid nil-panic trap if uncommented without guard
- Reset captureBuf at start of ComputeCommitment to prevent stale data
  accumulation across consecutive calls
- Fix residual //hph.trace = true comment to //hph.SetTraceWriter(os.Stderr)
- Remove leftover plan.md from repo root (duplicate of docs/plans/completed/)
- Strengthen BufferCapturesOutput test: count [proc] lines, use precise
  fold pattern match
@awskii

awskii commented Jun 17, 2026

Copy link
Copy Markdown
Member Author

Superseded by #21859 — clean reimplementation on current main (this branch was ~800 commits behind and conflicting).

@awskii awskii closed this Jun 17, 2026
@awskii awskii deleted the awskii/trace-writer branch July 3, 2026 06:09
pull Bot pushed a commit to Dustin4444/erigon that referenced this pull request Jul 3, 2026
…ch#21859)

Reimplements erigontech#20316 on current `main` (that branch was ~800 commits
behind and conflicting); supersedes it. Replaces the commitment trie's
`trace`/`traceDomain`/`capture []string` + `fmt.Printf` tracing with a
single `traceW io.Writer`.

## Changes

- `Trie` interface:
`SetTrace`/`SetTraceDomain`/`SetCapture`/`GetCapture` collapse to
`SetTraceWriter(io.Writer)`; `nil` disables tracing, and the `traceW !=
nil` guard keeps disabled tracing free (no perf change). `SharedDomains`
no longer carries trace state. Callers pass `os.Stderr`, a file, or a
`bytes.Buffer` to capture.
- Covers every trie engine: `HexPatriciaHashed`,
`ParallelPatriciaHashed`, `StreamingCommitter`, and the deep-storage
fold.
- Parallel/streaming trace is attributable: concurrent workers share one
mutex-guarded writer, so whole lines never interleave. Deep-storage fold
workers prefix each line with the parent account address — grep one
address to follow that account's whole storage fold out of the
interleaved output; account and split workers prefix by nibble.
- `TrieContext.trace` also becomes an `io.Writer`, wired from the
commitment context, so branch reads and writes trace as `[SDC]` lines on
the same writer.

---------

Co-authored-by: Alexey Sharov <AskAlexSharov@gmail.com>
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.

1 participant