Re: faster access private or public?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Tom Widmer (tom_usenet_at_hotmail.com)
Date: 09/27/04


Date: Mon, 27 Sep 2004 10:27:41 +0100

On Fri, 24 Sep 2004 19:21:11 +0200, "Sigurd Stenersen"
<sigurds@utvikling.com> wrote:

>Tom Widmer wrote:
>> Here's an example of the public vs private case:
>>
>> class Foo
>> {
>> int i; //private (*)
>> public:
>> Foo(int i): i(i){}
>> int getResult() const
>> {
>> //perform lengthy calculation relying only on i
>> return result;
>> }
>> };
>>
>> //...
>> Foo N(100000);
>> f(&N);
>> for (int i = 0; i != N.getResult(); ++i)
>> {
>> g(i);
>> }
>>
>> Can you see that if i is made public, N.getResult() cannot be hoisted
>> out of the loop as a constant, and instead the calculation will be
>> performed for every loop iteration? Hoisting of loop-invariant
>> expressions is a common compiler optimization.
>
>Actually, you can't take it out of the loop anyway. I've implemented f()
>and g() for you :
>
> Foo *OhFooey;
>
> void f(Foo *ey)
> {
> OhFooey = ey;
> }
>
> void g(int i)
> {
> *OhFooey = Foo(i);
> }

Whoops, Foo should have had:

private:
  Foo& operator=(Foo const& f); //unimplemented

But, thinking about it, that's still not enough, since g can legally
be written:

void g(int i)
{
   OhFooey->~Foo();
   new (OhFooey) Foo(10);
}

I can't think of a way out of that except to make the destructor
private, and make the original function a friend of Foo. But perhaps
you can see another way of breaking it?

>> But like I said in my original post, I don't know whether
>> public/private is ever used by compilers for optimization, but
>> theoretically it could be.
>
>Let's see an example of this situation, then.

Well, I think the above is now a theoretical example of this, but I
agree it isn't a real world one! Because of the C++ type system, there
are a lot of ways to change the value of private variables, and
closing them off isn't easy. Languages like Java have much more
"secure" type systems, and that gives optimizers an easier job IMHO.
Certainly, making variables private in Java is more likely to have a
real world benefit than in C++ - Java has neither assignment operators
nor explicit destruction and re-construction, so if no public function
allows mutation of a particular variable, the JIT compiler can
optimize based on that.

Tom



Relevant Pages

  • Re: derived class can not access base class protected member?
    ... void foo() ... Because foo() is defined as PRIVATE in the base class ... class derived: public base ...
    (microsoft.public.dotnet.languages.vc)
  • java - public interface - private menthods
    ... warning - I'm more familiar with C++ but I'm learning Java for new project. ... The intent is that methods may be overridden by clients but only the class that defines the interfaces are allowed to call them. ... void YConsumer(Y & y) ... private void FuncOnlyForX(); // javac saya illegal use of private ...
    (comp.lang.java.programmer)
  • Re: Why does this compile with VC++?
    ... A foo() ... void bar() ... private. ...
    (microsoft.public.vc.language)
  • Re: Java generics limitations?
    ... The underlying idea is known as the Curiously Recurring Template Idiom, first described, as far as I know, by James Coplien. ... private static final Foo instance = new Foo; ... Java generics are designed to express in the language certain constraints on types objects interact with. ...
    (comp.lang.java.programmer)
  • Re: Why does this compile with VC++?
    ... A foo() ... void bar() ... private. ...
    (microsoft.public.vc.language)