README 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. FarmHash, a family of hash functions.
  2. Version 1.1
  3. Introduction
  4. ============
  5. A general overview of hash functions and their use is available in the file
  6. Understanding_Hash_Functions in this directory. It may be helpful to read it
  7. before using FarmHash.
  8. FarmHash provides hash functions for strings and other data. The functions
  9. mix the input bits thoroughly but are not suitable for cryptography. See
  10. "Hash Quality," below, for details on how FarmHash was tested and so on.
  11. We provide reference implementations in C++, with a friendly MIT license.
  12. All members of the FarmHash family were designed with heavy reliance on
  13. previous work by Jyrki Alakuijala, Austin Appleby, Bob Jenkins, and others.
  14. Recommended Usage
  15. =================
  16. Our belief is that the typical hash function is mostly used for in-memory hash
  17. tables and similar. That use case allows hash functions that differ on
  18. different platforms, and that change from time to time. For this, I recommend
  19. using wrapper functions in a .h file with comments such as, "may change from
  20. time to time, may differ on different platforms, and may change depending on
  21. NDEBUG."
  22. Some projects may also require a forever-fixed, portable hash function. Again
  23. we recommend using wrapper functions in a .h, but in this case the comments on
  24. them would be very different.
  25. We have provided a sample of these wrapper functions in src/farmhash.h. Our
  26. hope is that most people will need nothing more than src/farmhash.h and
  27. src/farmhash.cc. Those two files are a usable and relatively portable library.
  28. (One portability snag: if your compiler doesn't have __builtin_expect then
  29. you may need to define FARMHASH_NO_BUILTIN_EXPECT.) For those that prefer
  30. using a configure script (perhaps because they want to "make install" later),
  31. FarmHash has one, but for many people it's best to ignore it.
  32. Note that the wrapper functions such as Hash() in src/farmhash.h can select
  33. one of several hash functions. The selection is done at compile time, based
  34. on your machine architecture (e.g., sizeof(size_t)) and the availability of
  35. vector instructions (e.g., SSE4.1).
  36. To get the best performance from FarmHash, one will need to think a bit about
  37. when to use compiler flags that allow vector instructions and such: -maes,
  38. -msse4.2, -mavx, etc., or their equivalents for other compilers. Those are
  39. the g++ flags that make g++ emit more types of machine instructions than it
  40. otherwise would. For example, if you are confident that you will only be
  41. using FarmHash on systems with SSE4.2 and/or AES, you may communicate that to
  42. the compiler as explained in src/farmhash.cc. If not, use -maes, -mavx, etc.,
  43. when you can, and the appropriate choices will be made by via conditional
  44. compilation in src/farmhash.cc.
  45. It may be beneficial to try -O3 or other compiler flags as well. I also have
  46. found feedback-directed optimization (FDO) to improve the speed of FarmHash.
  47. The "configure" script: creating config.h
  48. =========================================
  49. We provide reference implementations of several FarmHash functions, written in
  50. C++. The build system is based on autoconf. It defaults the C++ compiler
  51. flags to "-g -O2", which may or may not be best.
  52. If you are planning to use the configure script, I generally recommend
  53. trying this first, unless you know that your system lacks AVX and/or AESNI:
  54. ./configure CXXFLAGS="-g -mavx -maes -O3"
  55. make all check
  56. If that fails, you can retry with -mavx and/or -maes removed, or with -mavx replaced by
  57. -msse4.1 or -msse4.2.
  58. Please see below for thoughts on cross-platform testing, if that is a concern.
  59. Finally, if you want to install a library, you may use
  60. make install
  61. Some useful flags for configure include:
  62. --enable-optional-builtin-expect: This causes __builtin_expect to be optional.
  63. If you don't use this flag, the assumption is that FarmHash will be compiled
  64. with compilers that provide __builtin_expect. In practice, some FarmHash
  65. variants may be slightly faster if __builtin_expect is available, but it
  66. isn't very important and affects speed only.
  67. Further Details
  68. ===============
  69. The above instructions will produce a single source-level library that
  70. includes multiple hash functions. It will use conditional compilation, and
  71. perhaps GCC's multiversioning, to select among the functions. In addition,
  72. "make all check" will create an object file using your chosen compiler, and
  73. test it. The object file won't necessarily contain all the code that would be
  74. used if you were to compile the code on other platforms. The downside of this
  75. is obvious: the paths not tested may not actually work if and when you try
  76. them. The FarmHash developers try hard to prevent such problems; please let
  77. us know if you find bugs.
  78. To aid your cross-platform testing, for each relevant platform you may
  79. compile your program that uses farmhash.cc with the preprocessor flag
  80. FARMHASHSELFTEST equal to 1. This causes a FarmHash self test to run
  81. at program startup; the self test writes output to stdout and then
  82. calls std::exit(). You can see this in action by running "make check":
  83. see src/farm-test.cc for details.
  84. There's also a trivial workaround to force particular functions to be used:
  85. modify the wrapper functions in hash.h. You can prevent choices being made via
  86. conditional compilation or multiversioning by choosing FarmHash variants with
  87. names like farmhashaa::Hash32, farmhashab::Hash64, etc.: those compute the same
  88. hash function regardless of conditional compilation, multiversioning, or
  89. endianness. Consult their comments and ifdefs to learn their requirements: for
  90. example, they are not all guaranteed to work on all platforms.
  91. Known Issues
  92. ============
  93. 1) FarmHash was developed with little-endian architectures in mind. It should
  94. work on big-endian too, but less work has gone into optimizing for those
  95. platforms. To make FarmHash work properly on big-endian platforms you may
  96. need to modify the wrapper .h file and/or your compiler flags to arrange for
  97. FARMHASH_BIG_ENDIAN to be defined, though there is logic that tries to figure
  98. it out automatically.
  99. 2) FarmHash's implementation is fairly complex.
  100. 3) The techniques described in dev/INSTRUCTIONS to let hash function
  101. developers regenerate src/*.cc from dev/* are hacky and not so portable.
  102. Hash Quality
  103. ============
  104. We like to test hash functions with SMHasher, among other things.
  105. SMHasher isn't perfect, but it seems to find almost any significant flaw.
  106. SMHasher is available at http://code.google.com/p/smhasher/
  107. SMHasher is designed to pass a 32-bit seed to the hash functions it tests.
  108. For our functions that accept a seed, we use the given seed directly (padded
  109. with zeroes as needed); for our functions that don't accept a seed, we hash
  110. the concatenation of the given seed and the input string.
  111. Some minor flaws in 32-bit and 64-bit functions are harmless, as we
  112. expect the primary use of these functions will be in hash tables. We
  113. may have gone slightly overboard in trying to please SMHasher and other
  114. similar tests, but we don't want anyone to choose a different hash function
  115. because of some minor issue reported by a quality test.
  116. If your setup is similar enough to mine, it's easy to use SMHasher and other
  117. tools yourself via the "builder" in the dev directory. See dev/INSTRUCTIONS.
  118. (Improvements to that directory are a relatively low priority, and code
  119. there is never going to be as portable as the other parts of FarmHash.)
  120. For more information
  121. ====================
  122. http://code.google.com/p/farmhash/
  123. farmhash-discuss@googlegroups.com
  124. Please feel free to send us comments, questions, bug reports, or patches.