xpcom/tests/SizeTest01.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/SizeTest01.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +// Test01.cpp
     1.5 +
     1.6 +#include "nsIDOMNode.h"
     1.7 +#include "nsCOMPtr.h"
     1.8 +#include "nsString.h"
     1.9 +
    1.10 +NS_DEF_PTR(nsIDOMNode);
    1.11 +
    1.12 +	/*
    1.13 +		This test file compares the generated code size of similar functions between raw
    1.14 +                COM interface pointers (|AddRef|ing and |Release|ing by hand) and |nsCOMPtr|s.
    1.15 +
    1.16 +		Function size results were determined by examining dissassembly of the generated code.
    1.17 +		mXXX is the size of the generated code on the Macintosh.  wXXX is the size on Windows.
    1.18 +		For these tests, all reasonable optimizations were enabled and exceptions were
    1.19 +		disabled (just as we build for release).
    1.20 +
    1.21 +		The tests in this file explore only the simplest functionality: assigning a pointer
    1.22 +                to be reference counted into a [raw, nsCOMPtr] object; ensuring that it is
    1.23 +		|AddRef|ed and |Release|d appropriately; calling through the pointer to a function
    1.24 +		supplied by the underlying COM interface.
    1.25 +
    1.26 +		Windows:
    1.27 +			raw_optimized														 31 bytes
    1.28 +			raw, nsCOMPtr*													 34
    1.29 +			nsCOMPtr_optimized*											 38
    1.30 +			nsCOMPtr_optimized											 42
    1.31 +			nsCOMPtr																 46
    1.32 +
    1.33 +		Macintosh:
    1.34 +			raw_optimized, nsCOMPtr_optimized				112 bytes 	(1.0000)
    1.35 +			nsCOMPtr																120					(1.0714)	i.e., 7.14% bigger than raw_optimized et al
    1.36 +			raw																			140 				(1.2500)
    1.37 +
    1.38 +		The overall difference in size between Windows and Macintosh is caused by the
    1.39 +		the PowerPC RISC architecture where every instruction is 4 bytes.
    1.40 +
    1.41 +                On Macintosh, nsCOMPtr generates out-of-line destructors which are
    1.42 +		not referenced, and which can be stripped by the linker.
    1.43 +	*/
    1.44 +
    1.45 +void
    1.46 +Test01_raw( nsIDOMNode* aDOMNode, nsString* aResult )
    1.47 +		// m140, w34
    1.48 +	{
    1.49 +			/*
    1.50 +				This test is designed to be more like a typical large function where,
    1.51 +				because you are working with several resources, you don't just return when
    1.52 +				one of them is |nullptr|.  Similarly: |Test01_nsCOMPtr00|, and |Test01_nsIPtr00|.
    1.53 +			*/
    1.54 +
    1.55 +		nsIDOMNode* node = aDOMNode;
    1.56 +		NS_IF_ADDREF(node);
    1.57 +
    1.58 +		if ( node )
    1.59 +			node->GetNodeName(*aResult);
    1.60 +
    1.61 +		NS_IF_RELEASE(node);
    1.62 +	}
    1.63 +
    1.64 +void
    1.65 +Test01_raw_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
    1.66 +		// m112, w31
    1.67 +	{
    1.68 +			/*
    1.69 +				This test simulates smaller functions where you _do_ just return
    1.70 +				|nullptr| at the first sign of trouble.  Similarly: |Test01_nsCOMPtr01|,
    1.71 +				and |Test01_nsIPtr01|.
    1.72 +			*/
    1.73 +
    1.74 +			/*
    1.75 +				This test produces smaller code that |Test01_raw| because it avoids
    1.76 +				the three tests: |NS_IF_...|, and |if ( node )|.
    1.77 +			*/
    1.78 +
    1.79 +// -- the following code is assumed, but is commented out so we compare only
    1.80 +//		 the relevent generated code
    1.81 +
    1.82 +//		if ( !aDOMNode )
    1.83 +//			return;
    1.84 +
    1.85 +		nsIDOMNode* node = aDOMNode;
    1.86 +		NS_ADDREF(node);
    1.87 +		node->GetNodeName(*aResult);
    1.88 +		NS_RELEASE(node);
    1.89 +	}
    1.90 +
    1.91 +void
    1.92 +Test01_nsCOMPtr( nsIDOMNode* aDOMNode, nsString* aResult )
    1.93 +		// m120, w46/34
    1.94 +	{
    1.95 +		nsCOMPtr<nsIDOMNode> node = aDOMNode;
    1.96 +
    1.97 +		if ( node )
    1.98 +			node->GetNodeName(*aResult);
    1.99 +	}
   1.100 +
   1.101 +void
   1.102 +Test01_nsCOMPtr_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
   1.103 +		// m112, w42/38
   1.104 +	{
   1.105 +//		if ( !aDOMNode )
   1.106 +//			return;
   1.107 +
   1.108 +		nsCOMPtr<nsIDOMNode> node = aDOMNode;
   1.109 +		node->GetNodeName(*aResult);
   1.110 +	}

mercurial