optimization - How do I efficiently structure a golang program for optimum garbage collector runs? -


optimizing code better results in golang gc seems more of rather important thing time-optimized gc runs. told how accomplishes in run "depends on pattern of heap memory usage.", i'm not sure means/entails perspective of programmer in language. or not can controlled?

i have read through recent book "the go programming language" brian w. kernighan, there nothing topic in it. , information on topic on internet years ago, don't apply.

some things include:

  • making sure pointers/objects ever stored/remembered need be
  • allocating objects capacities of expected or sane
  • not duplicating data
  • when able, using streaming data through functions instead of putting data big heap front.

i bit annoyed fact strings , byte arrays recreated when converting between 1 or other (due strings being immutable). when going 1 other, , safe operation, recast pointers other type using unsafe.

are of these practices worth gc run faster , clear more? there else do?

if simple matter of list of do's , don'ts write program optimize memory usage.

the first step write correct, well-designed, maintainable, , easy-to-read code.

next, using go's testing package, benchmark critical functions. example. real case,

benchmarkoriginal      30000     44349 ns/op       52792 b/op      569 allocs/op 

use go's profile tool. read source , executable code see going on.

implement strategies, such single underlying array , full slice expressions, reduce gc memory allocations , cpu time. run final benchmark.

benchmarkoptimized    100000     13198 ns/op       32992 b/op        3 allocs/op 

in case, 569 allocations of elements of triangular array reduced 3 allocations, 99% reduction in allocations corresponding 70% reduction in cpu time. there's lot less garbage collector (gc) too.

of course, made code harder read (more complex , more obscure) , harder maintain.

don't on optimize. have nothing better do? best optimization buying bigger computer.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -