Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun committed Dec 31, 2024
1 parent df56354 commit 52d0473
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions rpc/lazy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ import (
"sync/atomic"
)

// lazyDecodedJsonObject is a generic structure that supports lazy decoding of JSON objects.
// This approach avoids unnecessary JSON unmarshalling until the value is explicitly accessed,
// which can improve performance when the decoded value is not always needed.
type lazyDecodedJsonObject[T any] struct {
cachedBytes json.RawMessage
value atomic.Value
cachedBytes json.RawMessage // Holds the raw JSON bytes for deferred decoding.
value atomic.Value // Caches the decoded value for thread-safe access.
}

// MarshalJSON implements the json.Marshaler interface.
// It returns the cached JSON bytes, ensuring the object can be marshaled back to JSON
// without decoding the actual value.
func (obj *lazyDecodedJsonObject[T]) MarshalJSON() ([]byte, error) {
return obj.cachedBytes, nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
// It stores the raw JSON bytes for lazy decoding, which allows the object to defer expensive
// JSON unmarshalling until the value is explicitly accessed.
func (obj *lazyDecodedJsonObject[T]) UnmarshalJSON(b []byte) error {
obj.cachedBytes = b
return nil
}

// Load returns the lazily decoded value. If the value has not been decoded yet,
// it unmarshals the cached JSON bytes, caches the result, and returns it. Subsequent
// calls will return the cached value.
func (obj *lazyDecodedJsonObject[T]) Load() (T, error) {
if v, ok := obj.value.Load().(T); ok {
return v, nil
Expand Down

0 comments on commit 52d0473

Please sign in to comment.