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

Better hash function for strings #1047

Closed
eulerkochy opened this issue Jul 7, 2019 · 3 comments
Closed

Better hash function for strings #1047

eulerkochy opened this issue Jul 7, 2019 · 3 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@eulerkochy
Copy link
Contributor

eulerkochy commented Jul 7, 2019

The current implementation of hashing of strings is way too trivial.

pub fn (s string) hash() int {
	mut hash := int(0)
	for i := 0; i < s.len; i++ {
		// if key == 'Content-Type' {
		// println('$i) $hash')
		// }
		hash = hash * int(31) + int(s.str[i])
	}
	return hash
}

This warrants for a better and secure hash function.

@eulerkochy eulerkochy added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Jul 7, 2019
@alfclement
Copy link

Any proposal?

@eulerkochy
Copy link
Contributor Author

I would tend to go for a variant of MurMur Hash. Also, a new file hash.v can be made in this retrospect.

@medvednikov
Copy link
Member

This is the same hash function that's been used by Java for decades. It's battle tested, it's simple, it works:

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants