Debian Codegraph @

README.md

raw
1 # go-clone: Clone any Go data structure deeply and thoroughly
2
3 [![Go](https://github.com/huandu/go-clone/workflows/Go/badge.svg)](https://github.com/huandu/go-clone/actions)
4 [![Go Doc](https://godoc.org/github.com/huandu/go-clone?status.svg)](https://pkg.go.dev/github.com/huandu/go-clone)
5 [![Go Report](https://goreportcard.com/badge/github.com/huandu/go-clone)](https://goreportcard.com/report/github.com/huandu/go-clone)
6 [![Coverage Status](https://coveralls.io/repos/github/huandu/go-clone/badge.svg?branch=master)](https://coveralls.io/github/huandu/go-clone?branch=master)
7
8 Package `clone` provides functions to deep clone any Go data. It also provides a wrapper to protect a pointer from any unexpected mutation.
9
10 For users who use Go 1.18+, it's recommended to import `github.com/huandu/go-clone/generic` for generic APIs and arena support.
11
12 `Clone`/`Slowly` can clone unexported fields and "no-copy" structs as well. Use this feature wisely.
13
14 ## Install
15
16 Use `go get` to install this package.
17
18 ```shell
19 go get github.com/huandu/go-clone
20 ```
21
22 ## Usage
23
24 ### `Clone` and `Slowly`
25
26 If we want to clone any Go value, use `Clone`.
27
28 ```go
29 t := &T{...}
30 v := clone.Clone(t).(*T)
31 reflect.DeepEqual(t, v) // true
32 ```
33
34 For the sake of performance, `Clone` doesn't deal with values containing pointer cycles.
35 If we need to clone such values, use `Slowly` instead.
36
37 ```go
38 type ListNode struct {
39 Data int
40 Next *ListNode
41 }
42 node1 := &ListNode{
43 Data: 1,
44 }
45 node2 := &ListNode{
46 Data: 2,
47 }
48 node3 := &ListNode{
49 Data: 3,
50 }
51 node1.Next = node2
52 node2.Next = node3
53 node3.Next = node1
54
55 // We must use `Slowly` to clone a circular linked list.
56 node := Slowly(node1).(*ListNode)
57
58 for i := 0; i < 10; i++ {
59 fmt.Println(node.Data)
60 node = node.Next
61 }
62 ```
63
64 ### Generic APIs
65
66 Starting from go1.18, Go started to support generic. With generic syntax, `Clone`/`Slowly` and other APIs can be called much cleaner like following.
67
68 ```go
69 import "github.com/huandu/go-clone/generic"
70
71 type MyType struct {
72 Foo string
73 }
74
75 original := &MyType{
76 Foo: "bar",
77 }
78
79 // The type of cloned is *MyType instead of interface{}.
80 cloned := Clone(original)
81 println(cloned.Foo) // Output: bar
82 ```
83
84 It's required to update minimal Go version to 1.18 to opt-in generic syntax. It may not be a wise choice to update this package's `go.mod` and drop so many old Go compilers for such syntax candy. Therefore, I decide to create a new standalone package `github.com/huandu/go-clone/generic` to provide APIs with generic syntax.
85
86 For new users who use Go 1.18+, the generic package is preferred and recommended.
87
88 ### Arena support
89
90 Starting from Go1.20, arena is introduced as a new way to allocate memory. It's quite useful to improve overall performance in special scenarios.
91 In order to clone a value with memory allocated from an arena, there are new methods `ArenaClone` and `ArenaCloneSlowly` available in `github.com/huandu/go-clone/generic`.
92
93 ```go
94 // ArenaClone recursively deep clones v to a new value in arena a.
95 // It works in the same way as Clone, except it allocates all memory from arena.
96 func ArenaClone[T any](a *arena.Arena, v T) (nv T)
97
98 // ArenaCloneSlowly recursively deep clones v to a new value in arena a.
99 // It works in the same way as Slowly, except it allocates all memory from arena.
100 func ArenaCloneSlowly[T any](a *arena.Arena, v T) (nv T)
101 ```
102
103 Due to limitations in arena API, memory of the internal data structure of `map` and `chan` is always allocated in heap by Go runtime ([see this issue](https://github.com/golang/go/issues/56230)).
104
105 **Warning**: Per [discussion in the arena proposal](https://github.com/golang/go/issues/51317), the arena package may be changed incompatibly or removed in future. All arena related APIs in this package will be changed accordingly.
106
107 ### Memory allocations and the `Allocator`
108
109 The `Allocator` is designed to allocate memory when cloning. It's also used to hold all customizations, e.g. custom clone functions, scalar types and opaque pointers, etc. There is a default allocator which allocates memory from heap. Almost all public APIs in this package use this default allocator to do their job.
110
111 We can control how to allocate memory by creating a new `Allocator` by `NewAllocator`. It enables us to take full control over memory allocation when cloning. See [Allocator sample code](https://pkg.go.dev/github.com/huandu/go-clone#example-Allocator) to understand how to customize an allocator.
112
113 Let's take a closer look at the `NewAllocator` function.
114
115 ```go
116 func NewAllocator(pool unsafe.Pointer, methods *AllocatorMethods) *Allocator
117 ```
118
119 - The first parameter `pool` is a pointer to a memory pool. It's used to allocate memory for cloning. It can be `nil` if we don't need a memory pool.
120 - The second parameter `methods` is a pointer to a struct which contains all methods to allocate memory. It can be `nil` if we don't need to customize memory allocation.
121 - The `Allocator` struct is allocated from the `methods.New` or the `methods.Parent` allocator or from heap.
122
123 The `Parent` in `AllocatorMethods` is used to indicate the parent of the new allocator. With this feature, we can orgnize allocators into a tree structure. All customizations, including custom clone functions, scalar types and opaque pointers, etc, are inherited from parent allocators.
124
125 There are some APIs designed for convenience.
126
127 - We can create dedicated allocators for heap or arena by calling `FromHeap()` or `FromArena(a *arena.Arena)`.
128 - We can call `MakeCloner(allocator)` to create a helper struct with `Clone` and `CloneSlowly` methods in which the type of in and out parameters is `interface{}`.
129
130 ### Mark struct type as scalar
131
132 Some struct types can be considered as scalar.
133
134 A well-known case is `time.Time`.
135 Although there is a pointer `loc *time.Location` inside `time.Time`, we always use `time.Time` by value in all methods.
136 When cloning `time.Time`, it should be OK to return a shadow copy.
137
138 Currently, following types are marked as scalar by default.
139
140 - `time.Time`
141 - `reflect.Value`
142
143 If there is any type defined in built-in package should be considered as scalar, please open new issue to let me know.
144 I will update the default.
145
146 If there is any custom type should be considered as scalar, call `MarkAsScalar` to mark it manually. See [MarkAsScalar sample code](https://pkg.go.dev/github.com/huandu/go-clone#example-MarkAsScalar) for more details.
147
148 ### Mark pointer type as opaque
149
150 Some pointer values are used as enumerable const values.
151
152 A well-known case is `elliptic.Curve`. In package `crypto/tls`, curve type of a certificate is checked by comparing values to pre-defined curve values, e.g. `elliptic.P521()`. In this case, the curve values, which are pointers or structs, cannot be cloned deeply.
153
154 Currently, following types are marked as scalar by default.
155
156 - `elliptic.Curve`, which is `*elliptic.CurveParam` or `elliptic.p256Curve`.
157 - `reflect.Type`, which is `*reflect.rtype` defined in `runtime`.
158
159 If there is any pointer type defined in built-in package should be considered as opaque, please open new issue to let me know.
160 I will update the default.
161
162 If there is any custom pointer type should be considered as opaque, call `MarkAsOpaquePointer` to mark it manually. See [MarkAsOpaquePointer sample code](https://pkg.go.dev/github.com/huandu/go-clone#example-MarkAsOpaquePointer) for more details.
163
164 ### Clone "no-copy" types defined in `sync` and `sync/atomic`
165
166 There are some "no-copy" types like `sync.Mutex`, `atomic.Value`, etc.
167 They cannot be cloned by copying all fields one by one, but we can alloc a new zero value and call methods to do proper initialization.
168
169 Currently, all "no-copy" types defined in `sync` and `sync/atomic` can be cloned properly using following strategies.
170
171 - `sync.Mutex`: Cloned value is a newly allocated zero mutex.
172 - `sync.RWMutex`: Cloned value is a newly allocated zero mutex.
173 - `sync.WaitGroup`: Cloned value is a newly allocated zero wait group.
174 - `sync.Cond`: Cloned value is a cond with a newly allocated zero lock.
175 - `sync.Pool`: Cloned value is an empty pool with the same `New` function.
176 - `sync.Map`: Cloned value is a sync map with cloned key/value pairs.
177 - `sync.Once`: Cloned value is a once type with the same done flag.
178 - `atomic.Value`/`atomic.Bool`/`atomic.Int32`/`atomic.Int64`/`atomic.Uint32`/`atomic.Uint64`/`atomic.Uintptr`: Cloned value is a new atomic value with the same value.
179
180 If there is any type defined in built-in package should be considered as "no-copy" types, please open new issue to let me know.
181 I will update the default.
182
183 ### Set custom clone functions
184
185 If default clone strategy doesn't work for a struct type, we can call `SetCustomFunc` to register a custom clone function.
186
187 ```go
188 SetCustomFunc(reflect.TypeOf(MyType{}), func(allocator *Allocator, old, new reflect.Value) {
189 // Customized logic to copy the old to the new.
190 // The old's type is MyType.
191 // The new is a zero value of MyType and new.CanAddr() always returns true.
192 })
193 ```
194
195 We can use `allocator` to clone any value or allocate new memory.
196 It's allowed to call `allocator.Clone` or `allocator.CloneSlowly` on `old` to clone its struct fields in depth without worrying about dead loop.
197
198 See [SetCustomFunc sample code](https://pkg.go.dev/github.com/huandu/go-clone#example-SetCustomFunc) for more details.
199
200 ### Clone `atomic.Pointer[T]`
201
202 As there is no way to predefine a custom clone function for generic type `atomic.Pointer[T]`, cloning such atomic type is not supported by default. If we want to support it, we need to register a custom clone function manually.
203
204 Suppose we instantiate `atomic.Pointer[T]` with type `MyType1` and `MyType2` in a project, and then we can register custom clone functions like following.
205
206 ```go
207 import "github.com/huandu/go-clone/generic"
208
209 func init() {
210 // Register all instantiated atomic.Pointer[T] types in this project.
211 clone.RegisterAtomicPointer[MyType1]()
212 clone.RegisterAtomicPointer[MyType2]()
213 }
214 ```
215
216 ### `Wrap`, `Unwrap` and `Undo`
217
218 Package `clone` provides `Wrap`/`Unwrap` functions to protect a pointer value from any unexpected mutation.
219 It's useful when we want to protect a variable which should be immutable by design,
220 e.g. global config, the value stored in context, the value sent to a chan, etc.
221
222 ```go
223 // Suppose we have a type T defined as following.
224 // type T struct {
225 // Foo int
226 // }
227 v := &T{
228 Foo: 123,
229 }
230 w := Wrap(v).(*T) // Wrap value to protect it.
231
232 // Use w freely. The type of w is the same as that of v.
233
234 // It's OK to modify w. The change will not affect v.
235 w.Foo = 456
236 fmt.Println(w.Foo) // 456
237 fmt.Println(v.Foo) // 123
238
239 // Once we need the original value stored in w, call `Unwrap`.
240 orig := Unwrap(w).(*T)
241 fmt.Println(orig == v) // true
242 fmt.Println(orig.Foo) // 123
243
244 // Or, we can simply undo any change made in w.
245 // Note that `Undo` is significantly slower than `Unwrap`, thus
246 // the latter is always preferred.
247 Undo(w)
248 fmt.Println(w.Foo) // 123
249 ```
250
251 ## Performance
252
253 Here is the performance data running on my dev machine.
254
255 ```text
256 go 1.20.1
257 goos: darwin
258 goarch: amd64
259 cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
260 BenchmarkSimpleClone-12 7164530 156.7 ns/op 24 B/op 1 allocs/op
261 BenchmarkComplexClone-12 628056 1871 ns/op 1488 B/op 21 allocs/op
262 BenchmarkUnwrap-12 15498139 78.02 ns/op 0 B/op 0 allocs/op
263 BenchmarkSimpleWrap-12 3882360 309.7 ns/op 72 B/op 2 allocs/op
264 BenchmarkComplexWrap-12 949654 1245 ns/op 736 B/op 15 allocs/op
265 ```
266
267 ## License
268
269 This package is licensed under MIT license. See LICENSE for details.