Skip to content

Commit 5bb1053

Browse files
authored
Ensure string-hashing is defined before it gets used (#36411)
An analysis of invalidations during bootstrap revealed that `log_record_id` calls `hash` on strings, and in principle it is used before the specialized methods are defined. Since the new methods change the value of the hash is expected to change, this could cause subtle bugs. It seems safer to define the hash methods at around the first time they could conceivably be defined.
1 parent 3bbb582 commit 5bb1053

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

base/hashing.jl

+10
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@ else
7575
hash(x::Expr, h::UInt) = hash(x.args, hash(x.head, h + 0x96d26dc6))
7676
hash(x::QuoteNode, h::UInt) = hash(x.value, h + 0x469d72af)
7777
end
78+
79+
## hashing strings ##
80+
81+
const memhash = UInt === UInt64 ? :memhash_seed : :memhash32_seed
82+
const memhash_seed = UInt === UInt64 ? 0x71e729fd56419c81 : 0x56419c81
83+
84+
function hash(s::String, h::UInt)
85+
h += memhash_seed
86+
ccall(memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, sizeof(s), h % UInt32) + h
87+
end

base/hashing2.jl

-11
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,3 @@ end
230230
## hashing Float16s ##
231231

232232
hash(x::Float16, h::UInt) = hash(Float64(x), h)
233-
234-
## hashing strings ##
235-
236-
const memhash = UInt === UInt64 ? :memhash_seed : :memhash32_seed
237-
const memhash_seed = UInt === UInt64 ? 0x71e729fd56419c81 : 0x56419c81
238-
239-
function hash(s::Union{String,SubString{String}}, h::UInt)
240-
h += memhash_seed
241-
ccall(memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, sizeof(s), h % UInt32) + h
242-
end
243-
hash(s::AbstractString, h::UInt) = hash(String(s), h)

base/strings/basic.jl

+4
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ cmp(a::Symbol, b::Symbol) = Int(sign(ccall(:strcmp, Int32, (Cstring, Cstring), a
347347

348348
isless(a::Symbol, b::Symbol) = cmp(a, b) < 0
349349

350+
# hashing
351+
352+
hash(s::AbstractString, h::UInt) = hash(String(s), h)
353+
350354
## character index arithmetic ##
351355

352356
"""

base/strings/substring.jl

+5
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ end
122122
pointer(x::SubString{String}) = pointer(x.string) + x.offset
123123
pointer(x::SubString{String}, i::Integer) = pointer(x.string) + x.offset + (i-1)
124124

125+
function hash(s::SubString{String}, h::UInt)
126+
h += memhash_seed
127+
ccall(memhash, UInt, (Ptr{UInt8}, Csize_t, UInt32), s, sizeof(s), h % UInt32) + h
128+
end
129+
125130
"""
126131
reverse(s::AbstractString) -> AbstractString
127132

0 commit comments

Comments
 (0)