Bladeren bron

Speed up profile encode/decode

Preallocate arrays to avoid repeated growth during profile encode/decode
Raul Silvera 8 jaren geleden
bovenliggende
commit
4d59d2f44f
2 gewijzigde bestanden met toevoegingen van 17 en 9 verwijderingen
  1. 11
    7
      profile/encode.go
  2. 6
    2
      profile/proto.go

+ 11
- 7
profile/encode.go Bestand weergeven

@@ -70,9 +70,9 @@ func (p *Profile) preEncode() {
70 70
 				)
71 71
 			}
72 72
 		}
73
-		s.locationIDX = nil
74
-		for _, l := range s.Location {
75
-			s.locationIDX = append(s.locationIDX, l.ID)
73
+		s.locationIDX = make([]uint64, len(s.Location))
74
+		for i, loc := range s.Location {
75
+			s.locationIDX[i] = loc.ID
76 76
 		}
77 77
 	}
78 78
 
@@ -177,9 +177,13 @@ var profileDecoder = []decoder{
177 177
 	// repeated Location location = 4
178 178
 	func(b *buffer, m message) error {
179 179
 		x := new(Location)
180
+		x.Line = make([]Line, 0, 8) // Pre-allocate Line buffer
180 181
 		pp := m.(*Profile)
181 182
 		pp.Location = append(pp.Location, x)
182
-		return decodeMessage(b, x)
183
+		err := decodeMessage(b, x)
184
+		var tmp []Line
185
+		x.Line = append(tmp, x.Line...) // Shrink to allocated size
186
+		return err
183 187
 	},
184 188
 	// repeated Function function = 5
185 189
 	func(b *buffer, m message) error {
@@ -282,9 +286,9 @@ func (p *Profile) postDecode() error {
282 286
 		if len(numLabels) > 0 {
283 287
 			s.NumLabel = numLabels
284 288
 		}
285
-		s.Location = nil
286
-		for _, lid := range s.locationIDX {
287
-			s.Location = append(s.Location, locations[lid])
289
+		s.Location = make([]*Location, len(s.locationIDX))
290
+		for i, lid := range s.locationIDX {
291
+			s.Location[i] = locations[lid]
288 292
 		}
289 293
 		s.locationIDX = nil
290 294
 	}

+ 6
- 2
profile/proto.go Bestand weergeven

@@ -290,6 +290,7 @@ func decodeInt64s(b *buffer, x *[]int64) error {
290 290
 	if b.typ == 2 {
291 291
 		// Packed encoding
292 292
 		data := b.data
293
+		tmp := make([]int64, 0, len(data)) // Maximally sized
293 294
 		for len(data) > 0 {
294 295
 			var u uint64
295 296
 			var err error
@@ -297,8 +298,9 @@ func decodeInt64s(b *buffer, x *[]int64) error {
297 298
 			if u, data, err = decodeVarint(data); err != nil {
298 299
 				return err
299 300
 			}
300
-			*x = append(*x, int64(u))
301
+			tmp = append(tmp, int64(u))
301 302
 		}
303
+		*x = append(*x, tmp...)
302 304
 		return nil
303 305
 	}
304 306
 	var i int64
@@ -321,6 +323,7 @@ func decodeUint64s(b *buffer, x *[]uint64) error {
321 323
 	if b.typ == 2 {
322 324
 		data := b.data
323 325
 		// Packed encoding
326
+		tmp := make([]uint64, 0, len(data)) // Maximally sized
324 327
 		for len(data) > 0 {
325 328
 			var u uint64
326 329
 			var err error
@@ -328,8 +331,9 @@ func decodeUint64s(b *buffer, x *[]uint64) error {
328 331
 			if u, data, err = decodeVarint(data); err != nil {
329 332
 				return err
330 333
 			}
331
-			*x = append(*x, u)
334
+			tmp = append(tmp, u)
332 335
 		}
336
+		*x = append(*x, tmp...)
333 337
 		return nil
334 338
 	}
335 339
 	var u uint64