6

Dispatch creates hash tables only for lists of length larger than 3. Is there any way to force MMA to create hash tables even for lists of length smaller than 4?

Table[Head@Dispatch@Table[Rule[a[i], i], {i, n}], {n, 5}]
(* {List, List, List, Dispatch, Dispatch} *)
Hector
  • 6,428
  • 15
  • 34
  • 3
    Is there any reason you would want a hash table for such small lists of rules? There won't be any performance advantage for such small rule lists. – Leonid Shifrin Aug 07 '13 at 16:30
  • I want to write a function to update a dispatch table using the first element of the dispatch table (which is the original set of rules). Such a function would take a performance hit if it was to evaluate whether the head is Dispatch or List. – Hector Aug 07 '13 at 17:01
  • 1
    Updating dispatch table built with Dispatch does not seem a good idea to me, if you need to do this often, since it will have to redo the dispatch table at every update, and the complexity of such an update will be generally linear with respect to the length of the list of rules it currently has. With respect to this performance hit, the one of pattern-matching on List or Dispatch is a minor one. If you want frequent updates, then you will likely be better off with DownValues or System`Utilities`HashTable. If the updates will not be frequent, this extra overhead should not matter. – Leonid Shifrin Aug 07 '13 at 18:49
  • You are right in asserting that the extra overhead is rather minor. My tests show that the time used recreating the dispatch table is recovered with as little as 10 replacements. In fact, the longer the table the faster you recover the invested time. My problem is that I expect anywhere between 8 and 20 lookups between updates. That means I am working on the margins. My solution so far is to keep a different list that I update and I create the dispatch table from that other list. – Hector Aug 07 '13 at 19:08
  • 1
    Is there any particular reason why you prefer Dispatch to DownValues or System`Utilities`HashTable? I like Dispatch very much myself, but normally use it in cases when the updates are either not needed at all or very infrequent. The other two options provide efficient updates, but extracting all keys or all values can be comparatively slow. As long as you don't have to frequently extract full key set or value set, I would go with one of them instead of Dispatch, judging from your description. – Leonid Shifrin Aug 07 '13 at 19:15
  • I did not know about SystemUtilitiesHashTable. It does not seem to be documented. I do not use DownValues because it conflicts with ReadProtect (writing a package). – Hector Aug 07 '13 at 20:41
  • 1
    You can look here for some information on System`Utilities`HashTable. And I didn't follow your remark on ReadProtected. If you mean that you can't change the DownValues once you read-protect the symbol, this is not true. In general, if you want to prevent the users from accessing your implementation, this is a different matter, and I am sure that one can do this reasonably well, but you can't get an absolute protection (which is I think true also in general, not just for Mathematica). – Leonid Shifrin Aug 07 '13 at 21:28
  • Thank you for the link on HashTable. As for DownValues, it is true that you can read and change them when the symbols is ReadProtected. However, one cannot "return a query". – Hector Aug 07 '13 at 22:57
  • @Hector If you explain the problem with DownValues and ReadProtect it's possible we can provide you with a work-around. – Mr.Wizard Aug 08 '13 at 03:28

1 Answers1

3

In version 10 even a single Rule will produce a Dispatch object:

Dispatch[ a -> 1 ] // InputForm
Dispatch[{a -> 1}]
Dispatch[ a -> 1 ] // AtomQ
True

Association is also a hash structure and provides a comparably fast alternative to Dispatch, which an interface for adding, deleting and updating rules. In many cases Associations may be used like Dispatch tables and they can also be used like functions. For some performance points see:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371