1 | // Test01.cpp
|
---|
2 |
|
---|
3 | #include "nsIDOMNode.h"
|
---|
4 | #include "nsCOMPtr.h"
|
---|
5 | #include "nsString.h"
|
---|
6 |
|
---|
7 | NS_DEF_PTR(nsIDOMNode);
|
---|
8 |
|
---|
9 | /*
|
---|
10 | This test file compares the generated code size of similar functions between raw
|
---|
11 | COM interface pointers (|AddRef|ing and |Release|ing by hand) and |nsCOMPtr|s.
|
---|
12 |
|
---|
13 | Function size results were determined by examining dissassembly of the generated code.
|
---|
14 | mXXX is the size of the generated code on the Macintosh. wXXX is the size on Windows.
|
---|
15 | For these tests, all reasonable optimizations were enabled and exceptions were
|
---|
16 | disabled (just as we build for release).
|
---|
17 |
|
---|
18 | The tests in this file explore only the simplest functionality: assigning a pointer
|
---|
19 | to be reference counted into a [raw, nsCOMPtr] object; ensuring that it is
|
---|
20 | |AddRef|ed and |Release|d appropriately; calling through the pointer to a function
|
---|
21 | supplied by the underlying COM interface.
|
---|
22 |
|
---|
23 | Windows:
|
---|
24 | raw_optimized 31 bytes
|
---|
25 | raw, nsCOMPtr* 34
|
---|
26 | nsCOMPtr_optimized* 38
|
---|
27 | nsCOMPtr_optimized 42
|
---|
28 | nsCOMPtr 46
|
---|
29 |
|
---|
30 | Macintosh:
|
---|
31 | raw_optimized, nsCOMPtr_optimized 112 bytes (1.0000)
|
---|
32 | nsCOMPtr 120 (1.0714) i.e., 7.14% bigger than raw_optimized et al
|
---|
33 | raw 140 (1.2500)
|
---|
34 |
|
---|
35 | The overall difference in size between Windows and Macintosh is caused by the
|
---|
36 | the PowerPC RISC architecture where every instruction is 4 bytes.
|
---|
37 |
|
---|
38 | On Macintosh, nsCOMPtr generates out-of-line destructors which are
|
---|
39 | not referenced, and which can be stripped by the linker.
|
---|
40 | */
|
---|
41 |
|
---|
42 | void
|
---|
43 | Test01_raw( nsIDOMNode* aDOMNode, nsString* aResult )
|
---|
44 | // m140, w34
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | This test is designed to be more like a typical large function where,
|
---|
48 | because you are working with several resources, you don't just return when
|
---|
49 | one of them is |NULL|. Similarly: |Test01_nsCOMPtr00|, and |Test01_nsIPtr00|.
|
---|
50 | */
|
---|
51 |
|
---|
52 | nsIDOMNode* node = aDOMNode;
|
---|
53 | NS_IF_ADDREF(node);
|
---|
54 |
|
---|
55 | if ( node )
|
---|
56 | node->GetNodeName(*aResult);
|
---|
57 |
|
---|
58 | NS_IF_RELEASE(node);
|
---|
59 | }
|
---|
60 |
|
---|
61 | void
|
---|
62 | Test01_raw_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
|
---|
63 | // m112, w31
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | This test simulates smaller functions where you _do_ just return
|
---|
67 | |NULL| at the first sign of trouble. Similarly: |Test01_nsCOMPtr01|,
|
---|
68 | and |Test01_nsIPtr01|.
|
---|
69 | */
|
---|
70 |
|
---|
71 | /*
|
---|
72 | This test produces smaller code that |Test01_raw| because it avoids
|
---|
73 | the three tests: |NS_IF_...|, and |if ( node )|.
|
---|
74 | */
|
---|
75 |
|
---|
76 | // -- the following code is assumed, but is commented out so we compare only
|
---|
77 | // the relevent generated code
|
---|
78 |
|
---|
79 | // if ( !aDOMNode )
|
---|
80 | // return;
|
---|
81 |
|
---|
82 | nsIDOMNode* node = aDOMNode;
|
---|
83 | NS_ADDREF(node);
|
---|
84 | node->GetNodeName(*aResult);
|
---|
85 | NS_RELEASE(node);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void
|
---|
89 | Test01_nsCOMPtr( nsIDOMNode* aDOMNode, nsString* aResult )
|
---|
90 | // m120, w46/34
|
---|
91 | {
|
---|
92 | nsCOMPtr<nsIDOMNode> node = aDOMNode;
|
---|
93 |
|
---|
94 | if ( node )
|
---|
95 | node->GetNodeName(*aResult);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void
|
---|
99 | Test01_nsCOMPtr_optimized( nsIDOMNode* aDOMNode, nsString* aResult )
|
---|
100 | // m112, w42/38
|
---|
101 | {
|
---|
102 | // if ( !aDOMNode )
|
---|
103 | // return;
|
---|
104 |
|
---|
105 | nsCOMPtr<nsIDOMNode> node = aDOMNode;
|
---|
106 | node->GetNodeName(*aResult);
|
---|
107 | }
|
---|