Repository navigation
Conversation
kyasbal
requested review from
K53,
RyuSA,
jyane,
kkuchima and
renamoo
as code owners
December 1, 2026 04:41
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors TypedDict to serialize write operations per key using reference-counted mutexes, allowing its zero value to be ready for use without calling NewTypedDict. It also updates Get to handle nil values and adds comprehensive unit tests. The reviewer suggested using var zero T instead of *new(T) as a more idiomatic way to obtain the zero value of a generic type in Go.
Comment on lines
85
to
98
| func Get[T any](dict *TypedDict[T], key string) (T, bool) { | ||
| value, ok := dict.container.Load(key) | ||
| if ok { | ||
| typed, ok := value.(T) | ||
| if !ok { | ||
| panic(fmt.Sprintf("expected dict value at %s is convertible to %T, but got %T.\nThis error rarely happens unless users forcibly casting the key types or a bug in KHI.\n Please report a bug. https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value)) | ||
| } | ||
| return typed, true | ||
| if !ok { | ||
| return *new(T), false | ||
| } | ||
| if value == nil { | ||
| return *new(T), true | ||
| } | ||
| typed, ok := value.(T) | ||
| if !ok { | ||
| panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value)) | ||
| } | ||
| return *new(T), false | ||
| return typed, true | ||
| } |
Contributor
There was a problem hiding this comment.
Using var zero T is the standard and more idiomatic way to obtain the zero value of a generic type in Go, rather than allocating/dereferencing with *new(T). This also simplifies the code and improves readability.
Suggested change
| func Get[T any](dict *TypedDict[T], key string) (T, bool) { | |
| value, ok := dict.container.Load(key) | |
| if ok { | |
| typed, ok := value.(T) | |
| if !ok { | |
| panic(fmt.Sprintf("expected dict value at %s is convertible to %T, but got %T.\nThis error rarely happens unless users forcibly casting the key types or a bug in KHI.\n Please report a bug. https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value)) | |
| } | |
| return typed, true | |
| if !ok { | |
| return *new(T), false | |
| } | |
| if value == nil { | |
| return *new(T), true | |
| } | |
| typed, ok := value.(T) | |
| if !ok { | |
| panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, *new(T), value)) | |
| } | |
| return *new(T), false | |
| return typed, true | |
| } | |
| func Get[T any](dict *TypedDict[T], key string) (T, bool) { | |
| var zero T | |
| value, ok := dict.container.Load(key) | |
| if !ok { | |
| return zero, false | |
| } | |
| if value == nil { | |
| return zero, true | |
| } | |
| typed, ok := value.(T) | |
| if !ok { | |
| panic(fmt.Sprintf("expected dict value at %s to be convertible to %T, but got %T.\nThis error indicates an internal type mismatch in KHI.\nPlease report a bug: https://github.com/GoogleCloudPlatform/khi/issues", key, zero, value)) | |
| } | |
| return typed, true | |
| } |
Go Quality Report (
|
| Metric | Result |
|---|---|
| Changed Scope | 1 prod file(s), 1 test file(s), 20 unit(s) |
| Patch Coverage | 97.3% (36/37 lines) |
| Mutation Score | 100.0% (15/15 killed, 0 survived) |
Quality Rule Violations
| # | Rule | Status | Value (Limit) | Location | Subject |
|---|---|---|---|---|---|
[Q1] |
test_redundancy |
ADVISORY |
1 (<= 0) |
pkg/common/typeddict/typeddict_test.go:550 |
TestTypedDict_KeyLockLifecycle |
[Q1] test_redundancy — pkg/common/typeddict/typeddict_test.go:550 (TestTypedDict_KeyLockLifecycle)
- Why: Subtest exercises an identical branch-polarity path with no unique mutant kills, or checks only err != nil on an already-covered path.
- Fix: Remove duplicate equivalence-class cases, or assert concrete output values so the case kills a unique mutant.
- Detail: 1 redundant subtest(s) subsumed by stronger tests
[SAME PATH]TestTypedDict_KeyLockLifecycle/allows_distinct_keys_to_proceed_concurrently_without_blocking(L550) dominated byTestTypedDict_KeyLockLifecycle/acquires_and_reclaims_keyLocks_across_mutations_and_missing-key_deletes(pkg/common/typeddict/typeddict_test.go:493)
Commit: 5869dc84bdd3 · Cloud Build Logs
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Aligns
pkg/common/typeddictwith KHI Go coding and testing conventions without changing exported APIs, while fixing two runtime bugs:lockers sync.Mapper-key mutex table with a reference-countedkeyLocksmap guarded bylockMu(lazily initialized so zero-value&TypedDict[T]{}remains ready to use), reclaiming idle per-key locks whenrefsdrops to0without holdinglockMuacrossgenFunc().nilinterface values inGet(returning(*new(T), true)before thevalue.(T)assertion) so storingnilintoTypedDict[error]/TypedDict[any]no longer panics while preserving typed-nil interface values.pkg/common/typeddict/typeddict.go.TestTypedDict_Concurrent(adding schedule-independent assertions andtesting.Short()scaling without skipping) and adds targeted tests for zero-value readiness, interfacenil/ typed-nil handling, per-key lock reclamation and panic cleanup, distinct-key concurrency, and boundary operations.Verification
make pre-push— PASS (0 mustfix, 0 advisory, 0 skipped)make qa-all-advisory CODEQUALITY_SIMILARITY=true— PASS (0 mustfix, 0 similarity advisories; 15/15 diff mutants killed)go test -race -count=1 -cover ./pkg/common/typeddict/... ./pkg/common/patternfinder/...— PASS (pkg/common/typeddict:97.9%statement coverage)Retained Advisories
typeddict_test.go:550(test_redundancyonTestTypedDict_KeyLockLifecycle/allows_distinct_keys_to_proceed_concurrently_without_blocking): intentionally retained because it is the sole deterministic causal-barrier test proving distinct keys do not block each other duringGetOrSetFunc(single-goroutine mutation reachability does not capture cross-key lock serialization).typeddict.go:94-96(defensive internal type-mismatch panic guard): intentionally retained unexercised via exported APIs.