SizeTest01.cpp 3.0 KB

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