Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Unfortunately many programmers underestimate the speed at which computers can copy flat regions of memory these days and overestimate speed of code littered with pointers and indirection. Often using pointers to avoid copying a few bytes leads to worse performance.

In my experience a combination of pass-by-value with move semantics provides good code readability and almost optimal performance in most cases, so that's my default. Unless a profiler disagrees in specific cases, of course.



> copy flat regions

A map or pretty much any nontrivial data structure is not a flat region.

In any event, waiting to optimize until a profiler tells you to is a reasonable practice as long as you pay attention to scaling. It’s very easy to write, for example, a JSON parser that performs fine in small tests and has a nice small n^2 coefficient. And then someone throws in a bigger input than you tested and your game takes ten minutes to load.


> A map or pretty much any nontrivial data structure is not a flat region.

Not necessarily. A hashmap can be implemented in a way it stores both the keys and values in a flat memory region, as long as keys and values are fixed size, and such a structure is way more efficient that traditional array-of-pointers implementation, where fetching each key requires following a pointer and getting the value requires following another pointer.


The resulting code will still be asymptotically slow.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: