davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

Support option to avoid printing duplicate values #139

Open rogpeppe opened 2 years ago

rogpeppe commented 2 years ago

Currently if there are multiple references to a pointer, the entire contents of it are printed each time (unless it's a recursive reference).

This can result in unnecessarily large and hard to read output when an object is shared in many places.

In one recent example, the output was 28MB, but when I changed the code to avoid printing duplicates (by removing the delete(d.pointers, k) line), the output dropped to 280KB, a factor of 100x difference.

This is not necessarily something that would be a good idea to enable by default, because it would require using O(n) memory to keep track of all pointers seen so far, but ISTM that it would be nice to be able to enable de-duplication as an option, at any rate.