Back to index

lightning-sunbird  0.9+nobinonly
SizeTest01.cpp
Go to the documentation of this file.
00001 // Test01.cpp
00002 
00003 #include "nsIDOMNode.h"
00004 #include "nsCOMPtr.h"
00005 #include "nsString.h"
00006 
00007 NS_DEF_PTR(nsIDOMNode);
00008 
00009        /*
00010               This test file compares the generated code size of similar functions between raw
00011                 COM interface pointers (|AddRef|ing and |Release|ing by hand) and |nsCOMPtr|s.
00012 
00013               Function size results were determined by examining dissassembly of the generated code.
00014               mXXX is the size of the generated code on the Macintosh.  wXXX is the size on Windows.
00015               For these tests, all reasonable optimizations were enabled and exceptions were
00016               disabled (just as we build for release).
00017 
00018               The tests in this file explore only the simplest functionality: assigning a pointer
00019                 to be reference counted into a [raw, nsCOMPtr] object; ensuring that it is
00020               |AddRef|ed and |Release|d appropriately; calling through the pointer to a function
00021               supplied by the underlying COM interface.
00022 
00023               Windows:
00024                      raw_optimized                                                                                             31 bytes
00025                      raw, nsCOMPtr*                                                                                            34
00026                      nsCOMPtr_optimized*                                                                         38
00027                      nsCOMPtr_optimized                                                                          42
00028                      nsCOMPtr                                                                                                                46
00029 
00030               Macintosh:
00031                      raw_optimized, nsCOMPtr_optimized                       112 bytes     (1.0000)
00032                      nsCOMPtr                                                                                                               120                                (1.0714)      i.e., 7.14% bigger than raw_optimized et al
00033                      raw                                                                                                                                  140                         (1.2500)
00034 
00035               The overall difference in size between Windows and Macintosh is caused by the
00036               the PowerPC RISC architecture where every instruction is 4 bytes.
00037 
00038                 On Macintosh, nsCOMPtr generates out-of-line destructors which are
00039               not referenced, and which can be stripped by the linker.
00040        */
00041 
00042 void
00043 Test01_raw( nsIDOMNode* aDOMNode, nsString* aResult )
00044               // m140, w34
00045        {
00046                      /*
00047                             This test is designed to be more like a typical large function where,
00048                             because you are working with several resources, you don't just return when
00049                             one of them is |NULL|.  Similarly: |Test01_nsCOMPtr00|, and |Test01_nsIPtr00|.
00050                      */
00051 
00052               nsIDOMNode* node = aDOMNode;
00053               NS_IF_ADDREF(node);
00054 
00055               if ( node )
00056                      node->GetNodeName(*aResult);
00057 
00058               NS_IF_RELEASE(node);
00059        }
00060 
00061 void
00062 Test01_raw_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
00063               // m112, w31
00064        {
00065                      /*
00066                             This test simulates smaller functions where you _do_ just return
00067                             |NULL| at the first sign of trouble.  Similarly: |Test01_nsCOMPtr01|,
00068                             and |Test01_nsIPtr01|.
00069                      */
00070 
00071                      /*
00072                             This test produces smaller code that |Test01_raw| because it avoids
00073                             the three tests: |NS_IF_...|, and |if ( node )|.
00074                      */
00075 
00076 // -- the following code is assumed, but is commented out so we compare only
00077 //             the relevent generated code
00078 
00079 //            if ( !aDOMNode )
00080 //                   return;
00081 
00082               nsIDOMNode* node = aDOMNode;
00083               NS_ADDREF(node);
00084               node->GetNodeName(*aResult);
00085               NS_RELEASE(node);
00086        }
00087 
00088 void
00089 Test01_nsCOMPtr( nsIDOMNode* aDOMNode, nsString* aResult )
00090               // m120, w46/34
00091        {
00092               nsCOMPtr<nsIDOMNode> node = aDOMNode;
00093 
00094               if ( node )
00095                      node->GetNodeName(*aResult);
00096        }
00097 
00098 void
00099 Test01_nsCOMPtr_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
00100               // m112, w42/38
00101        {
00102 //            if ( !aDOMNode )
00103 //                   return;
00104 
00105               nsCOMPtr<nsIDOMNode> node = aDOMNode;
00106               node->GetNodeName(*aResult);
00107        }