Permalink
Browse files

don't make a copy when hashing substrings

  • Loading branch information...
1 parent c776e53 commit e6e0ce65d3ef9c0d8c4335a979ef3d5dc9c7d895 @stevengj stevengj committed Mar 5, 2014
Showing with 17 additions and 16 deletions.
  1. +0 −16 base/dict.jl
  2. +16 −0 base/string.jl
  3. +1 −0 test/hashing.jl
View
@@ -288,22 +288,6 @@ hash(a::AbstractArray{Bool}) = hash(bitpack(a))
hash(x::ANY) = object_id(x)
-if WORD_SIZE == 64
- hash(s::ByteString) =
- ccall(:memhash, Uint64, (Ptr{Void}, Int), s.data, length(s.data))
- hash(s::ByteString, seed::Union(Int,Uint)) =
- ccall(:memhash_seed, Uint64, (Ptr{Void}, Int, Uint32),
- s.data, length(s.data), uint32(seed))
-else
- hash(s::ByteString) =
- ccall(:memhash32, Uint32, (Ptr{Void}, Int), s.data, length(s.data))
- hash(s::ByteString, seed::Union(Int,Uint)) =
- ccall(:memhash32_seed, Uint32, (Ptr{Void}, Int, Uint32),
- s.data, length(s.data), uint32(seed))
-end
-
-hash(s::String) = hash(bytestring(s))
-
hash(x::Expr) = bitmix(hash(x.head),hash(x.args)+43)
View
@@ -1672,3 +1672,19 @@ pointer{T<:ByteString}(x::SubString{T}, i::Integer) = pointer(x.string.data) + x
pointer(x::Union(UTF16String,UTF32String), i::Integer) = pointer(x)+(i-1)*sizeof(eltype(x.data))
pointer{T<:Union(UTF16String,UTF32String)}(x::SubString{T}) = pointer(x.string.data) + x.offset*sizeof(eltype(x.data))
pointer{T<:Union(UTF16String,UTF32String)}(x::SubString{T}, i::Integer) = pointer(x.string.data) + (x.offset + (i-1))*sizeof(eltype(x.data))
+
+# string hashing:
+if WORD_SIZE == 64
+ hash{T<:ByteString}(s::Union(T,SubString{T})) =
+ ccall(:memhash, Uint64, (Ptr{Void}, Int), pointer(s), sizeof(s))
+ hash{T<:ByteString}(s::Union(T,SubString{T}), seed::Union(Int,Uint)) =
+ ccall(:memhash_seed, Uint64, (Ptr{Void}, Int, Uint32),
+ pointer(s), sizeof(s), uint32(seed))
+else
+ hash{T<:ByteString}(s::Union(T,SubString{T})) =
+ ccall(:memhash32, Uint32, (Ptr{Void}, Int), pointer(s), sizeof(s))
+ hash{T<:ByteString}(s::Union(T,SubString{T}), seed::Union(Int,Uint)) =
+ ccall(:memhash32_seed, Uint32, (Ptr{Void}, Int, Uint32),
+ pointer(s), sizeof(s), uint32(seed))
+end
+hash(s::String) = hash(bytestring(s))
View
@@ -30,6 +30,7 @@ for T=types, S=types, x=vals
end
@test hash(RopeString("1","2")) == hash("12")
+@test hash(SubString("--hello--",3,7)) == hash("hello")
@test hash(:(X.x)) == hash(:(X.x))
@test hash(:(X.x)) != hash(:(X.y))

0 comments on commit e6e0ce6

Please sign in to comment.