Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster hash of number field elements #23402

Closed
videlec opened this issue Jul 11, 2017 · 42 comments
Closed

faster hash of number field elements #23402

videlec opened this issue Jul 11, 2017 · 42 comments

Comments

@videlec
Copy link
Contributor

videlec commented Jul 11, 2017

Hashing of nf element go through self.polynomial() which is damn slow!

The attached branch provides a quick hash version (that is more robust than the one for polynomial). A x10 speed up is seen on the following example (425 ms vs 40ms)

sage: K.<b> = NumberField(x^3 - 2)
sage: l = [i * b + j * b^2 for i in range(100) for j in range(100)]
sage: l.sort()
sage: %time s = set(l[i+1] - l[i] for i in range(len(l)-1))

Depends on #23388

CC: @bhutz

Component: number fields

Keywords: days88, IMA coding sprints

Author: Vincent Delecroix

Branch/Commit: 8d8a2ea

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/23402

@videlec videlec added this to the sage-8.0 milestone Jul 11, 2017
@videlec
Copy link
Contributor Author

videlec commented Jul 13, 2017

Author: Vincent Delecroix

@videlec
Copy link
Contributor Author

videlec commented Jul 13, 2017

Commit: 2873d21

@videlec
Copy link
Contributor Author

videlec commented Jul 13, 2017

New commits:

0146c8623388: faster floor for nf element
2873d2123402: faster hash for nf elements

@videlec

This comment has been minimized.

@videlec
Copy link
Contributor Author

videlec commented Jul 13, 2017

Branch: u/vdelecroix/23402

@videlec
Copy link
Contributor Author

videlec commented Jul 13, 2017

Dependencies: #23388

@videlec

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Jul 14, 2017

comment:3

Where did the two magic numbers come from? Also, why is one is hex and the other in decimal?

@videlec
Copy link
Contributor Author

videlec commented Jul 14, 2017

comment:4

These "magic numbers" are just used to randomize the bits and can be obtained via

sage: phi = (1 + sqrt(5))/2
sage: hex(int(2^32 / phi))
'0x9e3779b9'
sage: int(phi * 2^62)
7461864723258187525

The 7461864723258187525 was not chosen by me but comes from the hash value of rationals...

I can actually be more coherent and use the same value in both places.

@videlec
Copy link
Contributor Author

videlec commented Jul 14, 2017

comment:5

... bad idea (got stupid collisions)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2017

Changed commit from 2873d21 to 862184f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

862184f23402: faster hash for nf elements

@videlec
Copy link
Contributor Author

videlec commented Jul 14, 2017

comment:7

More coherent version in 8124ba5 where both values are:

  • 63 bits
  • documented
  • in decimal notation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2017

Changed commit from 862184f to 8124ba5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

8124ba523402: faster hash for nf elements

@tscrim
Copy link
Collaborator

tscrim commented Jul 14, 2017

comment:9

Okay, thanks. LGTM.

@tscrim
Copy link
Collaborator

tscrim commented Jul 14, 2017

Reviewer: Travis Scrimshaw

@videlec
Copy link
Contributor Author

videlec commented Jul 20, 2017

comment:10

Changing the hash is changing the order of display in sets... some doctests break.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 20, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

477805723402: fix doctests23402: fix doctests

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 20, 2017

Changed commit from 8124ba5 to 4778057

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 20, 2017

Changed commit from 4778057 to 9de6f18

@videlec
Copy link
Contributor Author

videlec commented Jul 27, 2017

comment:17

Indeed... 3 collisions in 32 bits! Probabilistically, we should not see any collision before a sample of size 216 = sqrt(232).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 27, 2017

Branch pushed to git repo; I updated commit sha1 and set ticket back to needs_review. New commits:

ab28ce823402: better magic numbers avoiding small collisions

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 27, 2017

Changed commit from 9de6f18 to ab28ce8

@videlec
Copy link
Contributor Author

videlec commented Jul 27, 2017

comment:19

needs review (no collision on the 18 first bits which is more than enough for 32 bits)

@tscrim
Copy link
Collaborator

tscrim commented Jul 27, 2017

comment:20

Doctest order failures due to the new hash. See patchbot report.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 23, 2017

Changed commit from ab28ce8 to 9d0329f

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 23, 2017

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

0361ee123402: faster hash for nf elements
4f2687123402: fix doctests
dd1406b23402: better magic numbers avoiding small collisions
9d0329f23402: more doctest fixes

@videlec
Copy link
Contributor Author

videlec commented Aug 23, 2017

comment:22

(rebased with a new commit to fix the doctests)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 24, 2017

Changed commit from 9d0329f to 8d8a2ea

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 24, 2017

Branch pushed to git repo; I updated commit sha1. New commits:

8d8a2ea23402: one more doctest fix

@videlec
Copy link
Contributor Author

videlec commented Aug 24, 2017

comment:24

The second doctest change in 8d8a2ea708 looks fishy. Actually, the version after my changes looks better!!

@videlec
Copy link
Contributor Author

videlec commented Aug 24, 2017

comment:25

@bhutz: Ben can you have a look at the doctest change in invariants_of_degree? The first change just a reordering. But the second one reproduced below looks like there was something very wrong before

             sage: S3 = MatrixGroup(SymmetricGroup(3))
             sage: chi = S3.character(S3.character_table()[0])
             sage: S3.invariants_of_degree(5, chi=chi)
-            [x*y + (2*izeta3^3 + 3*izeta3^2 + 8*izeta3 + 3)*x*z + (-2*izeta3^3 -
-            3*izeta3^2 - 8*izeta3 - 4)*y*z,
-             x^2 + (-2*izeta3^3 - 3*izeta3^2 - 8*izeta3 - 4)*y^2 + (2*izeta3^3 +
-            3*izeta3^2 + 8*izeta3 + 3)*z^2]
+            [x0^4*x1 - x0*x1^4 - x0^4*x2 + x1^4*x2 + x0*x2^4 - x1*x2^4,
+             x0^3*x1^2 - x0^2*x1^3 - x0^3*x2^2 + x1^3*x2^2 + x0^2*x2^3 - x1^2*x2^3]

@bhutz
Copy link

bhutz commented Aug 24, 2017

comment:26

Actually, it looks like you messed up the doc test in commit 4f26871 and are now changing it back in commit 8d8a2ea.

So I see nothing wrong here with the invariants code.

@videlec
Copy link
Contributor Author

videlec commented Aug 24, 2017

comment:27

Replying to @bhutz:

Actually, it looks like you messed up the doc test in commit 4f26871 and are now changing it back in commit 8d8a2ea.

So I see nothing wrong here with the invariants code.

My bad. Thanks for having a look!

@tscrim
Copy link
Collaborator

tscrim commented Aug 24, 2017

Changed keywords from none to days88, IMA coding sprints

@tscrim
Copy link
Collaborator

tscrim commented Aug 24, 2017

comment:28

Thanks.

LGTM.

@vbraun
Copy link
Member

vbraun commented Aug 29, 2017

Changed branch from u/vdelecroix/23402 to 8d8a2ea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants