caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Soegtrop, Michael" <michael.soegtrop@intel.com>
To: OCaML Mailing List <caml-list@inria.fr>
Subject: RE: [Caml-list] Specify the default hash function for a type
Date: Tue, 17 May 2016 13:03:06 +0000	[thread overview]
Message-ID: <0F7D3B1B3C4B894D824F5B822E3E5A172CEF34D8@IRSMSX102.ger.corp.intel.com> (raw)
In-Reply-To: <0F7D3B1B3C4B894D824F5B822E3E5A172CEF2F38@IRSMSX102.ger.corp.intel.com>

[-- Attachment #1: Type: text/plain, Size: 2951 bytes --]

Dear OCaml users,

to close this topic, here are some statistics for my use case with different hash functions. All hash methods I tried lead to identical statistics as random numbers. Simply using Hashtbl.hash on lists of hashes of substructures (Method 2 below) worked very well, is easy to write down and offers sufficient flexibility.

NItems   =  3277598
NBuckets = 16777216

        | Theoretical | Method1  |  Method2 |  Method3
--------+-------------+----------+----------+---------
Empty   |  13799905.0 | 13800062 | 13799864 | 13801452
Single  |   2695950.5 |  2695830 |  2696131 |  2693130
2 items |    263340.5 |   263101 |   263042 |   264318
3 items |     17148.7 |    17350 |    17368 |    17463
4 items |       837.5 |      849 |      779 |      822
5 items |        32.7 |       24 |       29 |       31
6 items |         1.1 |        0 |        3 |        3
--------+-------------+----------+----------+---------
Runtime |             |     119s |     122s |     110s

Note 1: the expected random deviation of the counts is sqrt(count).

Note 2: The speed differences between Method 1 and 2 are random – Method 2 should be faster.

Note 3: using Hashtbl.hash on the complete structure doesn't work for me for two reasons:
- parts of the structure should be ignored
- I have maps as substructures and require that logically equal maps result in the same hash

Theoretical:
collision numbers for a random process with same parameters (used exact Bernoulli statistics, not Poisson approximation)

Method1:
Hashtbl.hash used on structures recursively (create list of hash values for substructures and use Hashtbl.hash on list).
Maps (<10 elements) are converted to lists and the list is hashed with Hashtbl.hash.
Multiply each hash list entry with a distinct prime.
Use prime values for variants and prime multipliers for variant arguments.

Method2:
Hashtbl.hash used on structures recursively (create list of hash values for substructures and use Hashtbl.hash on list).
Maps (<10 elements) are converted to lists and the list is hashed with Hashtbl.hash.
Use prime values for variants and prime multipliers for variant arguments.

Method3:
( ( ( (v1 * p1 + v2) * p2) + v3 ) *p3 + v4) * p4
where vi are values and pi are distinct prime numbers. For lists I use 4 primes alternatingly and 4 different primes for empty cases

Btw.: I added a function “worst_bucketlist” to Hashtbl, which returns the keys of the largest bucket. This is useful for optimizing (or bug fixing) hash and equality functions – try to make a feature request for it.

Best regards,

Michael
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

[-- Attachment #2: Type: text/html, Size: 13039 bytes --]

      reply	other threads:[~2016-05-17 13:04 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-13  7:52 Soegtrop, Michael
2016-05-13  8:13 ` Ben Millwood
2016-05-13  8:40   ` Soegtrop, Michael
2016-05-13  9:06 ` Alain Frisch
2016-05-13  9:26   ` Soegtrop, Michael
2016-05-13 12:01     ` Gabriel Scherer
2016-05-13 12:23       ` Alain Frisch
2016-05-13 12:32       ` Soegtrop, Michael
2016-05-13 13:50   ` Pierre Chambart
2016-05-13 13:56     ` Alain Frisch
2016-05-13 16:17       ` Soegtrop, Michael
2016-05-13 18:57         ` Thomas Braibant
2016-05-13 22:45           ` Soegtrop, Michael
2016-05-14  8:41             ` Thomas Braibant
2016-05-14  9:06               ` Soegtrop, Michael
2016-05-17 13:03                 ` Soegtrop, Michael [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0F7D3B1B3C4B894D824F5B822E3E5A172CEF34D8@IRSMSX102.ger.corp.intel.com \
    --to=michael.soegtrop@intel.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).