Use make instead of append to reduce memory usage#23
Use make instead of append to reduce memory usage#23vincent-163 wants to merge 1 commit intovalyala:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #23 +/- ##
=========================================
+ Coverage 92.78% 92.8% +0.02%
=========================================
Files 8 8
Lines 970 973 +3
=========================================
+ Hits 900 903 +3
Misses 48 48
Partials 22 22
Continue to review full report at Codecov.
|
| c.vs = c.vs[:len(c.vs)+1] | ||
| } else { | ||
| c.vs = append(c.vs, Value{}) | ||
| if len(c.vs) == 0 { |
There was a problem hiding this comment.
append already does something similar underneath - see https://play.golang.org/p/wFzqBZvEXPl . If append works slower than this code, then a bug must be filled at https://github.com/golang/go/issues
Uh I didn't notice this line. I thought the patch is for performance optimization, not a memory optimization. Then it looks valid. Could you add a benchmark with |
|
In fact, this will also improve performance by avoiding the copy. The benchmarks are here: https://gist.github.com/hewenyang/91618e54342f65809f4cb3c9c3aa1240 |
|
@valyala can this be merged? |
Since the pointers to original values are always held, it doesn't make sense to use append() and keep the original values anyway.
I expect this change to reduce memory consumption by about 30% for large JSON values and avoid copying.