Speed difference between CoCreateInstance and new operator
-
Hi guys, I have COM DLL Servers creating a lot of COM objects with CoCreateInstance and it's quite slow. I wondered if replacing the CoCreateInstance(CLSID_Foo) by a new CComObject < CFoo > would speed up things ? Or to be clearer, how slower is CoCreateInstance compared to new operator ? thanks !
You simply cannot use
new
to createCOM
objects (COM
objects lifetime has its own rules :rolleyes: ). If you need to create multiple objects, then you may obtain a speed enhancement using directly theIClassFactory
interface, see the remarks section of this page [^].If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Alexandre GRANVAUD wrote:
I wondered if replacing the CoCreateInstance(CLSID_Foo) by a new CComObject < CFoo > would speed up things ?
No. Since in both ways a COM object is created. Using the "new operator", as you call it, will eventually call
::CoCreateInstance()
, so there's no difference."It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknowncalling new CComObject < CThing > calls CoCoreateInstance???? i'm sure it doesnt !!
-
You simply cannot use
new
to createCOM
objects (COM
objects lifetime has its own rules :rolleyes: ). If you need to create multiple objects, then you may obtain a speed enhancement using directly theIClassFactory
interface, see the remarks section of this page [^].If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]yes you can in the same DLL, i do it everyday : in inprocess it's ok to do so ;) new CComObject < CThing > then doing a Queryinterface or a AddRef on it and your object is ok
-
calling new CComObject < CThing > calls CoCoreateInstance???? i'm sure it doesnt !!
Alexandre GRANVAUD wrote:
calling new CComObject < CThing > calls CoCoreateInstance???? i'm sure it doesnt !!
No, of course; allocating memory for a
CComObject
doesn't call::CoCreateInstance()
. But in order to create the server you'd have to callCComObject::CreateInstance()
. Otherwise the server won't be created and your comparison wouldn't make sense at all. :suss:"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Alexandre GRANVAUD wrote:
calling new CComObject < CThing > calls CoCoreateInstance???? i'm sure it doesnt !!
No, of course; allocating memory for a
CComObject
doesn't call::CoCreateInstance()
. But in order to create the server you'd have to callCComObject::CreateInstance()
. Otherwise the server won't be created and your comparison wouldn't make sense at all. :suss:"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownwhy do you need to call it ? as i can see, the CCOmObject constructor call FinalConstruct, so what extra initialization do we need ?
-
yes you can in the same DLL, i do it everyday : in inprocess it's ok to do so ;) new CComObject < CThing > then doing a Queryinterface or a AddRef on it and your object is ok
And what is the rationale behind that (other than revealing your bad practices :rolleyes:)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
And what is the rationale behind that (other than revealing your bad practices :rolleyes:)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]speed is always a good practice ;) having a bad practice is ok if you know you have (i do ;)) and know the consequences ;)
-
speed is always a good practice ;) having a bad practice is ok if you know you have (i do ;)) and know the consequences ;)
Alexandre GRANVAUD wrote:
speed is always a good practice
Before the first road accident ;P --Carlo The Motor Biker. (Anyway Carlo The Developer suspects it is more coding lazyness than speed improvement... :rolleyes:). BTW breaking the
COM
object creation mechanism is really a bad practice and even if you're stuck with it, please don't tell other people to do it. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Alexandre GRANVAUD wrote:
speed is always a good practice
Before the first road accident ;P --Carlo The Motor Biker. (Anyway Carlo The Developer suspects it is more coding lazyness than speed improvement... :rolleyes:). BTW breaking the
COM
object creation mechanism is really a bad practice and even if you're stuck with it, please don't tell other people to do it. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]lol i agree for the speed ;) (but can't slowdown) i don't break the com creation mechanism : the CreateInstance in a ClassFactory does exactly the same : a new CComObject ! ;)
-
lol i agree for the speed ;) (but can't slowdown) i don't break the com creation mechanism : the CreateInstance in a ClassFactory does exactly the same : a new CComObject ! ;)
Alexandre GRANVAUD wrote:
i don't break the com creation mechanism : the CreateInstance in a ClassFactory does exactly the same : a new CComObject !
Avoiding such call you're breaking it by definition. Of course you'll see the practical implications of such a wicked act just on 'strange' servers (i.e. servers that don't "do exactly the same", such a behaviour is allowed by
COM
). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]