Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do You Cache Properties in Local Variables?

Consider the class Foo.

public class Foo {
    private double size;

    public double getSize() {
        return this.size; // Always O(1)
    }
}

Foo has a property called size, which is frequently accessed, but never modified, by a given method. I've always cached a property in a variable whenever it is accessed more than once in any method, because "someone told me so" without giving it much thought. i.e.

public void test(Foo foo) {
    double size = foo.getSize(); // Cache it or not?
    // size will be referenced in several places later on.
}

Is this worth it, or an overkill?

If I don't cache it, are modern compilers smart enough to cache it themselves?

like image 492
Tom Tucker Avatar asked Jan 27 '14 21:01

Tom Tucker


People also ask

What are Cache properties?

Cache Type. Property Description. Objects. Number of Folders Cached—Number of folders to cache. Cache Folders For—Maximum time in seconds to cache folders (The limit for the delay between changes to a folder's contents and Explorer's display of the changes.)

What is local cache in Java?

A cache is an area of local memory that holds a copy of frequently accessed data that is otherwise expensive to get or compute. Examples of such data include a result of a query to a database, a disk file or a report. Lets look at creating and using a simple thread-safe Java in-memory cache.


2 Answers

A couple of factors (in no particular order) that I consider when deciding whether or not to store the value returned by a call to a "get() method":

  1. Performance of the get() method - Unless the API specifies, or unless the calling code is tightly coupled with the called method, there are no guarantees of the performance of the get() method. The code may be fine in testing now, but may get worse if the get() methods performace changes in the future or if testing does not reflect real-world conditions. (e.g. testing with only a thousand objects in a container when a real-world container might have ten million) Used in a for-loop, the get() method will be called before every iteration

  2. Readability - A variable can be given a specific and descriptive name, providing clarification of its use and/or meaning in a way that may not be clear from inline calls to the get() method. Don't underestimate the value of this to those reviewing and maintaining the code.

  3. Thread safety - Can the value returned by the get() method potentially change if another thread modifies the object while the calling method is doing its thing? Should such a change be reflected in the calling method's behavior?

Regarding the question of whether or not compilers will cache it themselves, I'm going to speculate and say that in most cases the answer has to be 'no'. The only way the compiler could safely do so would be if it could determine that the get() method would return the same value at every invocation. And this could only be guaranteed if the get() method itself was marked final and all it did was return a constant (i.e an object or primitive also marked 'final'). I'm not sure but I think this is probably not a scenario the compiler bothers with. The JIT compiler has more information and thus could have more flexibility but you have no guarantees that some method will get JIT'ed.

In conclusion, don't worry about what the compiler might do. Caching the return value of a get() method is probably the right thing to do most of the time, and will rarely (i.e almost never) be the wrong thing to do. Favor writing code that is readable and correct over code that is fast(est) and flashy.

like image 108
Scrubbie Avatar answered Sep 17 '22 19:09

Scrubbie


I don't know whether there is a "right" answer, but I would keep a local copy.

In your example, I can see that getSize() is trivial, but in real code, I don't always know whether it is trivial or not; and even if it is trivial today, I don't know that somebody won't come along and change the getSize() method to make it non-trivial sometime in the future.

like image 27
Solomon Slow Avatar answered Sep 20 '22 19:09

Solomon Slow