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

Use MurmurHash3 to allow for fast in-memory hashing with no conversion #26

Merged
merged 1 commit into from
Nov 2, 2020

Conversation

ScottPJones
Copy link
Member

No description provided.

Project.toml Outdated Show resolved Hide resolved
Base.firstindex(::ShortString) = 1
Base.isvalid(s::ShortString, i::Integer) = isvalid(String(s), i)
Base.iterate(s::ShortString) = iterate(String(s))
Base.iterate(s::ShortString, i::Integer) = iterate(String(s), i)
Base.lastindex(s::ShortString) = sizeof(s)
Base.ncodeunits(s::ShortString) = sizeof(s)

Base.display(s::ShortString) = display(String(s))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't show enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved that, to organize them a bit better, but I think you are right.

src/hash.jl Outdated

Base.hash(x::ShortString, h::UInt) = hash(String(x), h)
Base.hash(x::ShortString, h::UInt) =
last(mmhash128_a(sizeof(x), ntoh(x.size_content), h%UInt32)) + h
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
last(mmhash128_a(sizeof(x), ntoh(x.size_content), h%UInt32)) + h
last(mmhash128_a(sizeof(x), bswap(x.size_content), h%UInt32)) + h

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, what's the difference?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ntoh is short for network to host, so if host (the computer executing it) is little endian or big little endian it does it differently right? So bswap will always swap them? I am bit confused. here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bswap will always swap.
nthoh will not swap if julia is running on a big endian machine.
Only if it is on a little endian machine.
Julia only runs on little endian machines however.
Main advantage is that bswap says what it does without remembering network to host.

@oxinabox
Copy link
Member

Do you have benchmarks?

@ScottPJones ScottPJones force-pushed the spj/fasthash branch 2 times, most recently from 11a26a5 to 9f43169 Compare October 31, 2020 22:47
@ScottPJones
Copy link
Member Author

I'm not sure that I trust @benchmark on this, it is saying about 9.5ns for String, about 9ns for ASCIIStr, and 0.045ns for ShortString15.
I may have to figure some other way to benchmark it.

@oxinabox
Copy link
Member

oxinabox commented Nov 2, 2020

0.045ns for ShortString15.

Nice, it is constant-folding.
That's great.
ShortStrings constant-fold really well.

Base.hash(x::ShortString, h::UInt) = hash(String(x), h)
function Base.hash(x::ShortString, h::UInt)
h += Base.memhash_seed
last(mmhash128_a(sizeof(x), bswap(x.size_content), h%UInt32)) + h
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be:

Suggested change
last(mmhash128_a(sizeof(x), bswap(x.size_content), h%UInt32)) + h
last(mmhash128_a(ncodeunits(x), bswap(x.size_content), h%UInt32)) + h

to be more semantic

Comment on lines +21 to +26

# Warning: if a SubString is at the very end of a string, which is at the end of allocated
# memory, this can cause an access violation, by trying to access past the end
# (for example, reading a 1 byte substring at the end of a length 119 string, could go past
# the end)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a proper fix for this # #

@xiaodaigh xiaodaigh merged commit 2aa1f22 into JuliaString:master Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants