Skip to content

Commit 676496b

Browse files
code changes
1 parent 3c4eb74 commit 676496b

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

v2/arangodb/collection_documents_create_impl.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func newCollectionDocumentCreateResponseReader(array *connection.Array, options
130130
c := &collectionDocumentCreateResponseReader{array: array, options: options}
131131

132132
if c.options != nil {
133+
// Cache reflection types once during initialization for performance
134+
if c.options.OldObject != nil {
135+
c.oldType = reflect.TypeOf(c.options.OldObject).Elem()
136+
}
137+
if c.options.NewObject != nil {
138+
c.newType = reflect.TypeOf(c.options.NewObject).Elem()
139+
}
140+
133141
c.response.Old = newUnmarshalInto(c.options.OldObject)
134142
c.response.New = newUnmarshalInto(c.options.NewObject)
135143
}
@@ -156,6 +164,10 @@ type collectionDocumentCreateResponseReader struct {
156164
cachedErrors []error
157165
cached bool
158166
readIndex int // Track position in cache for Read() after Len()
167+
168+
// Performance: Cache reflection types to avoid repeated lookups
169+
oldType reflect.Type
170+
newType reflect.Type
159171
}
160172

161173
func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) {
@@ -177,13 +189,13 @@ func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreat
177189

178190
var meta CollectionDocumentCreateResponse
179191

180-
if c.options != nil {
181-
if c.options.OldObject != nil {
182-
meta.Old = reflect.New(reflect.TypeOf(c.options.OldObject).Elem()).Interface()
183-
}
184-
if c.options.NewObject != nil {
185-
meta.New = reflect.New(reflect.TypeOf(c.options.NewObject).Elem()).Interface()
186-
}
192+
// Create new instances for each document to avoid pointer reuse
193+
// Use cached types for performance
194+
if c.oldType != nil {
195+
meta.Old = reflect.New(c.oldType).Interface()
196+
}
197+
if c.newType != nil {
198+
meta.New = reflect.New(c.newType).Interface()
187199
}
188200

189201
c.response.DocumentMeta = &meta.DocumentMeta
@@ -202,6 +214,20 @@ func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreat
202214
return meta, meta.AsArangoError()
203215
}
204216

217+
// Copy data from the new instances back to the original option objects for backward compatibility
218+
if c.options != nil {
219+
if c.options.OldObject != nil && meta.Old != nil {
220+
oldValue := reflect.ValueOf(meta.Old).Elem()
221+
originalValue := reflect.ValueOf(c.options.OldObject).Elem()
222+
originalValue.Set(oldValue)
223+
}
224+
if c.options.NewObject != nil && meta.New != nil {
225+
newValue := reflect.ValueOf(meta.New).Elem()
226+
originalValue := reflect.ValueOf(c.options.NewObject).Elem()
227+
originalValue.Set(newValue)
228+
}
229+
}
230+
205231
return meta, nil
206232
}
207233

v2/arangodb/collection_documents_replace_impl.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func newCollectionDocumentReplaceResponseReader(array *connection.Array, options
130130
c := &collectionDocumentReplaceResponseReader{array: array, options: options}
131131

132132
if c.options != nil {
133+
// Cache reflection types once during initialization for performance
134+
if c.options.OldObject != nil {
135+
c.oldType = reflect.TypeOf(c.options.OldObject).Elem()
136+
}
137+
if c.options.NewObject != nil {
138+
c.newType = reflect.TypeOf(c.options.NewObject).Elem()
139+
}
140+
133141
c.response.Old = newUnmarshalInto(c.options.OldObject)
134142
c.response.New = newUnmarshalInto(c.options.NewObject)
135143
}
@@ -155,6 +163,10 @@ type collectionDocumentReplaceResponseReader struct {
155163
cachedErrors []error
156164
cached bool
157165
readIndex int // Track position in cache for Read() after Len()
166+
167+
// Performance: Cache reflection types to avoid repeated lookups
168+
oldType reflect.Type
169+
newType reflect.Type
158170
}
159171

160172
func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentReplaceResponse, error) {
@@ -176,13 +188,13 @@ func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentRepl
176188

177189
var meta CollectionDocumentReplaceResponse
178190

179-
if c.options != nil {
180-
if c.options.OldObject != nil {
181-
meta.Old = reflect.New(reflect.TypeOf(c.options.OldObject).Elem()).Interface()
182-
}
183-
if c.options.NewObject != nil {
184-
meta.New = reflect.New(reflect.TypeOf(c.options.NewObject).Elem()).Interface()
185-
}
191+
// Create new instances for each document to avoid pointer reuse
192+
// Use cached types for performance
193+
if c.oldType != nil {
194+
meta.Old = reflect.New(c.oldType).Interface()
195+
}
196+
if c.newType != nil {
197+
meta.New = reflect.New(c.newType).Interface()
186198
}
187199

188200
c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev
@@ -201,6 +213,20 @@ func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentRepl
201213
return meta, meta.AsArangoError()
202214
}
203215

216+
// Copy data from the new instances back to the original option objects for backward compatibility
217+
if c.options != nil {
218+
if c.options.OldObject != nil && meta.Old != nil {
219+
oldValue := reflect.ValueOf(meta.Old).Elem()
220+
originalValue := reflect.ValueOf(c.options.OldObject).Elem()
221+
originalValue.Set(oldValue)
222+
}
223+
if c.options.NewObject != nil && meta.New != nil {
224+
newValue := reflect.ValueOf(meta.New).Elem()
225+
originalValue := reflect.ValueOf(c.options.NewObject).Elem()
226+
originalValue.Set(newValue)
227+
}
228+
}
229+
204230
return meta, nil
205231
}
206232

v2/arangodb/collection_documents_update_impl.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func newCollectionDocumentUpdateResponseReader(array *connection.Array, options
130130
c := &collectionDocumentUpdateResponseReader{array: array, options: options}
131131

132132
if c.options != nil {
133+
// Cache reflection types once during initialization for performance
134+
if c.options.OldObject != nil {
135+
c.oldType = reflect.TypeOf(c.options.OldObject).Elem()
136+
}
137+
if c.options.NewObject != nil {
138+
c.newType = reflect.TypeOf(c.options.NewObject).Elem()
139+
}
140+
133141
c.response.Old = newUnmarshalInto(c.options.OldObject)
134142
c.response.New = newUnmarshalInto(c.options.NewObject)
135143
}
@@ -155,6 +163,10 @@ type collectionDocumentUpdateResponseReader struct {
155163
cachedErrors []error
156164
cached bool
157165
readIndex int // Track position in cache for Read() after Len()
166+
167+
// Performance: Cache reflection types to avoid repeated lookups
168+
oldType reflect.Type
169+
newType reflect.Type
158170
}
159171

160172
func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdateResponse, error) {
@@ -176,13 +188,13 @@ func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdat
176188

177189
var meta CollectionDocumentUpdateResponse
178190

179-
if c.options != nil {
180-
if c.options.OldObject != nil {
181-
meta.Old = reflect.New(reflect.TypeOf(c.options.OldObject).Elem()).Interface()
182-
}
183-
if c.options.NewObject != nil {
184-
meta.New = reflect.New(reflect.TypeOf(c.options.NewObject).Elem()).Interface()
185-
}
191+
// Create new instances for each document to avoid pointer reuse
192+
// Use cached types for performance
193+
if c.oldType != nil {
194+
meta.Old = reflect.New(c.oldType).Interface()
195+
}
196+
if c.newType != nil {
197+
meta.New = reflect.New(c.newType).Interface()
186198
}
187199

188200
c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev
@@ -201,6 +213,20 @@ func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdat
201213
return meta, meta.AsArangoError()
202214
}
203215

216+
// Copy data from the new instances back to the original option objects for backward compatibility
217+
if c.options != nil {
218+
if c.options.OldObject != nil && meta.Old != nil {
219+
oldValue := reflect.ValueOf(meta.Old).Elem()
220+
originalValue := reflect.ValueOf(c.options.OldObject).Elem()
221+
originalValue.Set(oldValue)
222+
}
223+
if c.options.NewObject != nil && meta.New != nil {
224+
newValue := reflect.ValueOf(meta.New).Elem()
225+
originalValue := reflect.ValueOf(c.options.NewObject).Elem()
226+
originalValue.Set(newValue)
227+
}
228+
}
229+
204230
return meta, nil
205231
}
206232

0 commit comments

Comments
 (0)