-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**tldr: this change preserves the existing APIs but makes the library faster and also able to recursively format structs and arrays.** Also a new API: `redact.ManualBuffer`, which gives ownership of swapping between safe and unsafe output to the caller. This is envisioned to be useful in CockroachDB to help implement `tree.FmtCtx`. Details: We are really looking at a major redesign. The previous design was attempting to preserve Go's own `fmt/print.go` as much as possible, and was "injecting" special formatting behavior by wrapping every printed value in a custom object whose type had a `Format()` method. The idea was to give “ownership” of adding redaction markers to each argument in turn. That worked for simple values but there was no good way to make it able to print structs and arrays recursively. This new implementation customizes the `print.go` code more heavily. In particular, the check for safe/unsafe types is now done at every level of the recursive print. It also makes the code use a `redact.ManualBuffer` as target during the print evaluation, which saves string copies (hence a general performance boost). The new supported cases can be understood by looking at the diffs on the files `markers_test.go` and `internal/rfmt/registry_test.go`. Note about the recursive display of struct values: the print logic uses reflection to print out structs. However, the Go semantics prevent a package from looking at non-exported fields in structs from other packages. This means that the print logic is unable to recognize the various safety interfaces for non-exported struct fields: `SafeValue`, `SafeFormatter`, `FormatError`, etc. These are thus considered unsafe regardless of the interfaces implemented. The safe type registry and the special types `RedactableString` and `RedactableBytes` are still recognized for non-exported structs though. Peformance improvement using the included `bench_test.go`: ``` name old time/op new time/op delta Redact-32 2.71µs ±22% 1.49µs ± 0% -45.25% (p=0.000 n=10+9) name old alloc/op new alloc/op delta Redact-32 360B ± 0% 248B ± 0% -31.11% (p=0.000 n=10+10) name old allocs/op new allocs/op delta Redact-32 12.0 ± 0% 5.0 ± 0% -58.33% (p=0.000 n=10+10) ```
- Loading branch information
Showing
36 changed files
with
2,961 additions
and
1,133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package redact | ||
|
||
import "testing" | ||
|
||
func BenchmarkRedact(b *testing.B) { | ||
x := struct { | ||
a int | ||
}{456} | ||
for i := 0; i < b.N; i++ { | ||
_ = Sprintf("hello %v %v %v", 123, x, Safe("world"), Unsafe("universe")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.