Skip to content

Commit 8535b8c

Browse files
authored
Merge pull request #41063 from JuliaLang/jb/statstructmethods
more efficient `==` and `hash` for `StatStruct`
2 parents e2d647e + 81dcc81 commit 8535b8c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

base/stat.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ struct StatStruct
4040
mtime :: Float64
4141
ctime :: Float64
4242
end
43-
const StatFieldTypes = Union{UInt,Int,Int64,Float64}
4443

45-
function Base.:(==)(x::StatStruct, y::StatStruct) # do not include `desc` in equality or hash
46-
for i = 2:nfields(x)
47-
xi = getfield(x, i)::StatFieldTypes
48-
xi === getfield(y, i) || return false
49-
end
50-
return true
44+
@eval function Base.:(==)(x::StatStruct, y::StatStruct) # do not include `desc` in equality or hash
45+
$(let ex = true
46+
for fld in fieldnames(StatStruct)[2:end]
47+
ex = :(getfield(x, $(QuoteNode(fld))) === getfield(y, $(QuoteNode(fld))) && $ex)
48+
end
49+
Expr(:return, ex)
50+
end)
5151
end
52-
function Base.hash(obj::StatStruct, h::UInt)
53-
for i = 2:nfields(obj)
54-
h = hash(getfield(obj, i)::StatFieldTypes, h)
55-
end
56-
return h
52+
@eval function Base.hash(obj::StatStruct, h::UInt)
53+
$(quote
54+
$(Any[:(h = hash(getfield(obj, $(QuoteNode(fld))), h)) for fld in fieldnames(StatStruct)[2:end]]...)
55+
return h
56+
end)
5757
end
5858

5959
StatStruct() = StatStruct("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

0 commit comments

Comments
 (0)