Closed n10v closed 7 years ago
Hi @bogem. Thanks for the contribution. Using a map complicates it now a little bit. First we need to protect against racy conditions. For example if you call the same print function, they will exists and they both will try to add it. Second if you protect it via locks I think it''ll add another bottleneck. So this time we'll going to have impact on the speed rather than memory. What do you think?
Locks work not so slow. But of course we should run benchmarks. I will do it a little bit later
So, I did benchmarks.
No cache:
BenchmarkPrintNoCache 200000 8981 ns/op 120 B/op 10 allocs/op
BenchmarkStringNoCache 1000000 1375 ns/op 200 B/op 15 allocs/op
Cache without locks:
BenchmarkPrintCacheNoLocks 200000 6417 ns/op 120 B/op 10 allocs/op
BenchmarkStringCacheNoLocks 1000000 1295 ns/op 152 B/op 13 allocs/op
Cache with mutex locks:
BenchmarkPrintCacheLock 200000 6436 ns/op 120 B/op 10 allocs/op
BenchmarkStringCacheLocks 1000000 1303 ns/op 152 B/op 13 allocs/op
As you see, there is no significant difference by using the mutex locks. I update this PR and you can prove it by yourself.
Done
Thanks @bogem 👍
Thank you for this awesome package! Wait one more performance PR ;)
printColor and printString create new Color object on every call. If user calls these functions more than one time, Color objects with the same Attribute are created. That consumes memory. I offer to cache already created objects and reuse them.
And this is strange, what these two functions are between print and string functions, so I place them in a top of these functions.