README.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ijar: A tool for generating interface .jars from normal .jars
  2. =============================================================
  3. Alan Donovan, 26 May 2007.
  4. Rationale:
  5. In order to improve the speed of compilation of Java programs in
  6. Bazel, the output of build steps is cached.
  7. This works very nicely for C++ compilation: a compilation unit
  8. includes a .cc source file and typically dozens of header files.
  9. Header files change relatively infrequently, so the need for a
  10. rebuild is usually driven by a change in the .cc file. Even after
  11. syncing a slightly newer version of the tree and doing a rebuild,
  12. many hits in the cache are still observed.
  13. In Java, by contrast, a compilation unit involves a set of .java
  14. source files, plus a set of .jar files containing already-compiled
  15. JVM .class files. Class files serve a dual purpose: from the JVM's
  16. perspective, they are containers of executable code, but from the
  17. compiler's perspective, they are interface definitions. The problem
  18. here is that .jar files are very much more sensitive to change than
  19. C++ header files, so even a change that is insignificant to the
  20. compiler (such as the addition of a print statement to a method in a
  21. prerequisite class) will cause the jar to change, and any code that
  22. depends on this jar's interface will be recompiled unnecessarily.
  23. The purpose of ijar is to produce, from a .jar file, a much smaller,
  24. simpler .jar file containing only the parts that are significant for
  25. the purposes of compilation. In other words, an interface .jar
  26. file. By changing ones compilation dependencies to be the interface
  27. jar files, unnecessary recompilation is avoided when upstream
  28. changes don't affect the interface.
  29. Details:
  30. ijar is a tool that reads a .jar file and emits a .jar file
  31. containing only the parts that are relevant to Java compilation.
  32. For example, it throws away:
  33. - Files whose name does not end in ".class".
  34. - All executable method code.
  35. - All private methods and fields.
  36. - All constants and attributes except the minimal set necessary to
  37. describe the class interface.
  38. - All debugging information
  39. (LineNumberTable, SourceFile, LocalVariableTables attributes).
  40. It also sets to zero the file modification times in the index of the
  41. .jar file.
  42. Implementation:
  43. ijar is implemented in C++, and runs very quickly. For example
  44. (when optimized) it takes only 530ms to process a 42MB
  45. .jar file containing 5878 classes, resulting in an interface .jar
  46. file of only 11.4MB in size. For more usual .jar sizes of a few
  47. megabytes, a runtime of 50ms is typical.
  48. The implementation strategy is to mmap both the input jar and the
  49. newly-created _interface.jar, and to scan through the former and
  50. emit the latter in a single pass. There are a couple of locations
  51. where some kind of "backpatching" is required:
  52. - in the .zip file format, for each file, the size field precedes
  53. the data. We emit a zero but note its location, generate and emit
  54. the stripped classfile, then poke the correct size into the
  55. location.
  56. - for JVM .class files, the header (including the constant table)
  57. precedes the body, but cannot be emitted before it because it's
  58. not until we emit the body that we know which constants are
  59. referenced and which are garbage. So we emit the body into a
  60. temporary buffer, then emit the header to the output jar, followed
  61. by the contents of the temp buffer.
  62. Also note that the zip file format has unnecessary duplication of
  63. the index metadata: it has header+data for each file, then another
  64. set of (similar) headers at the end. Rather than save the metadata
  65. explicitly in some datastructure, we just record the addresses of
  66. the already-emitted zip metadata entries in the output file, and
  67. then read from there as necessary.
  68. Notes:
  69. This code has no dependency except on the STL and on zlib.
  70. Almost all of the getX/putX/ReadX/WriteX functions in the code
  71. advance their first argument pointer, which is passed by reference.
  72. It's tempting to discard package-private classes and class members.
  73. However, this would be incorrect because they are a necessary part
  74. of the package interface, as a Java package is often compiled in
  75. multiple stages. For example: in Bazel, both java tests and java
  76. code inhabit the same Java package but are compiled separately.
  77. Assumptions:
  78. We assume that jar files are uncompressed v1.0 zip files (created
  79. with 'jar c0f') with a zero general_purpose_bit_flag.
  80. We assume that javap/javac don't need the correct CRC checksums in
  81. the .jar file.
  82. We assume that it's better simply to abort in the face of unknown
  83. input than to risk leaving out something important from the output
  84. (although in the case of annotations, it should be safe to ignore
  85. ones we don't understand).
  86. TODO:
  87. Maybe: ensure a canonical sort order is used for every list (jar
  88. entries, class members, attributes, etc.) This isn't essential
  89. because we can assume the compiler is deterministic and the order in
  90. the source files changes little. Also, it would require two passes. :(
  91. Maybe: delete dynamically-allocated memory.
  92. Add (a lot) more tests. Include a test of idempotency.