|
|
|
|
|
by rdc12
4144 days ago
|
|
"In fact, the very example given in the article shows that without the ACCESS_ONCE, the compiler moved the access outside of the loop---thereby ensuring that it's accessed just once, ironically!!! Whereas by using ACCESS_ONCE, we ensure that it's accessed as many times as the loop is iterated, not only once." But that is just a consequence of putting a call to ACCESS_ONCE inside a loop. But it is retaining the property that it is only read from memory once per iteration. It would be way more confusing if the programmer only wanted read once for the whole loop and still put the statement inside the loop. I do think that knowing that it is being called in a loop context, implies that it should only be accessed once per iteration |
|
ACCESS_ONCE to me strongly suggests that the value is accessed once and cached forever. E.g. ACCESS_ONCE(x) could expand to something like:
ANSI Common Lisp has load-time-value. You can use this anywhere: The load-time-value form is evaluated when the module is loaded and the value is stashed. Then whenever myfunc is called and that code is evaluated, the previously stashed value is retrieved; the (whatever) is not evaluated any more.