Dulan
Dulan

Reputation: 362

Do programming languages with access modifiers perform any optimizations based on access level?

Straightforward from the question.

Which, if any, language compilers perform optimization or compilation quirks when access modifiers are present on a symbol, keyword, etc.?

As a bonus: Are there optimizations that language compilers without access modifiers can do that one with them cannot? (In other words, do access modifiers ever prevent optimizations for the sake of the programmer's control)

EDIT

To be precise, I'm specifically referring to keywords like public, private, and protected, or some similar construct.

Upvotes: 2

Views: 114

Answers (2)

ewernli
ewernli

Reputation: 38625

In OO languages, method invocations are dynamically dispatched. If the compiler/runtime can figure out that there exists only one implementation of the method, and that the method won't change, it can perform more aggressive optimization.

From that point of view, private methods should be easier to optimize. Similarly, methods that belong to a class that is sealed are also easier to optimize.

I say "easier" becaues modern compilers/runtimes optimize lots of things anyway, but they need to rely on guards to make sure that the optimization are "deoptimized" if some assumptions becomes invalid. See this blog post from Brian Goetz.

Upvotes: 1

Michael
Michael

Reputation: 5939

First of all, languages don't perform optimizations, compilers do. Therefore the answer (usually) depends on the compiler implementation and on the compiler's options that you specify.

Most C++ compilers perform a significant amount of optimization depending on the const keyword: they pre-compute everything that can be computed at compile time, etc.

For example, consider the code inside the function "fraction":

const double yet_another_pi = 3.14;
double denominator() {
    return sqrt( 2*yet_another_pi );
}
double fraction( double numerator )
{
    return numerator / denominator();
}

This code will result on most compilers in a single multiplication of numerator by the precomputed reciprocal of sqrt(2*3.14).

Without the const keyword the same code would result in multiplication by 2 and a square root inside the function "denominator" followed by division in the function "fraction".


There are many other examples of modifiers that improve optimization. Some of them (such as enforcing certain alignment) have platform dependent effects.


If you are asking whether the availability of access modifiers inhibit optimization the answer is "no".

If you are asking whether incorrect usage of access modifiers can inhibit optimization the answer is "yes": if you apply volatile keyword to every variable in your code performance will go to drain because volatile variables cannot be optimized in registers.


EDIT: based on the comments to this answer, the question may have been about private/protected/public keywords. I am not aware of any optimization related to these keywords, except for a related Java's keyword "final".

Upvotes: 0

Related Questions