[prev in list] [next in list] [prev in thread] [next in thread]
List: gcc
Subject: Singletons and optimisation
From: Carlo Wood <carlo () runaway ! xs4all ! nl>
Date: 1999-09-19 17:12:41
[Download RAW message or body]
Hi,
has been some time I mailed here :).
I am trying to figure out a way to optimise
Singleton::instance()->method();
where `method' is not a static method, but doesn't
use `this' for some reason.
Marking Singleton::instance() with __attribute__ ((const))
should help imho, but it doesn't.
Example:
class Singleton {
public:
static Singleton *instance(void) __attribute__ ((const));
int add(int i) const { return i + 16; }
private:
Singleton(void) { }
static Singleton *instance_;
};
Singleton *Singleton::instance_;
Singleton *Singleton::instance(void)
{
if (!instance_)
instance_ = new Singleton;
return instance_;
}
int main(void)
{
return Singleton::instance()->add(1);
}
Does NOT have to call Singleton::instance()
(which is marked as __attribute__ ((const))!)
and could return immedeately 17.
But instead egcs (g++ -O6 test.cc) produces:
main:
.LFB1:
pushl %ebp
.LCFI0:
movl %esp,%ebp
.LCFI1:
cmpl $0,_9Singleton.instance_ }
jne .L23 }
pushl $1 } Redundant code
.LCFI2: }
call __builtin_new }
movl %eax,_9Singleton.instance_ }
.L23:
movl $17,%eax
leave
ret
and hence is initializing `instance_' (0x804997c).
[ Note: Making `add' also static is not an option, `add' is actually
part of a base class that is also used for non-singletons and CAN
access non-static member functions since it depends on template
parameters. ]
--
Carlo Wood <carlo@runaway.xs4all.nl>
PS Please CC me when replying to the list!
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic