瀏覽代碼

Merge pull request #25 from rauls5382/grow

Speed up profile encode/decode
Hyoun Kyu Cho 8 年之前
父節點
當前提交
14ce0c7a73
共有 2 個檔案被更改,包括 17 行新增9 行删除
  1. 11
    7
      profile/encode.go
  2. 6
    2
      profile/proto.go

+ 11
- 7
profile/encode.go 查看文件

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
 	// repeated Location location = 4
177
 	// repeated Location location = 4
178
 	func(b *buffer, m message) error {
178
 	func(b *buffer, m message) error {
179
 		x := new(Location)
179
 		x := new(Location)
180
+		x.Line = make([]Line, 0, 8) // Pre-allocate Line buffer
180
 		pp := m.(*Profile)
181
 		pp := m.(*Profile)
181
 		pp.Location = append(pp.Location, x)
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
 	// repeated Function function = 5
188
 	// repeated Function function = 5
185
 	func(b *buffer, m message) error {
189
 	func(b *buffer, m message) error {
282
 		if len(numLabels) > 0 {
286
 		if len(numLabels) > 0 {
283
 			s.NumLabel = numLabels
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
 		s.locationIDX = nil
293
 		s.locationIDX = nil
290
 	}
294
 	}

+ 6
- 2
profile/proto.go 查看文件

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