On Wed, 03 Feb 2016 09:29:30 -0500, Steven Schveighoffer wrote: > On 2/2/16 8:42 PM, Chris Wright wrote: >> On Tue, 02 Feb 2016 15:41:07 -0800, H. S. Teoh via Digitalmars-d wrote: >> >>> Furthermore, since const provides actual guarantees that the called >>> function isn't going to touch the data, this opens up optimization >>> opportunities for the compiler. >> >> const opens up optimizations at the call site, so it's useful. >> immutable is useful on top of const because it allows optimizations >> within the function. >> >> Want to memoize a function? If it takes const(char[]), you have to copy >> your input and later check the entire array to see if the parameters >> match a previous call. If it takes an immutable(char[]), you can >> compare pointers. > > This isn't exactly right. If I call a function with "hello", I'd want it > to memoize if the function is called with "hello" that resides > elsewhere.
In the general case, yes. It's much faster to look up a pointer/length pair in a hashtable than to look up a very long string, so if you expect long strings and many calls with the same addresses, you might have a fast cache by pointer. You might also have values that are automatically generated, and you're relatively certain that it will be rare to have duplicate values. Or you might have a complex data structure that's immutable and has an id and a revision. If you know for certain that id+revision is unique, you can memoize based on that, and immutable gives you some extra protection.
