Skip to content

Commit

Permalink
remove a couple uses of ntuple to help the compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 18, 2018
1 parent 22b32d5 commit 333d444
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 11 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,17 @@ unitrange_last(start::T, stop::T) where {T} =
convert(T,start-oneunit(stop-start)))

if isdefined(Main, :Base)
getindex(t::Tuple, r::AbstractUnitRange{<:Real}) =
(o = first(r) - 1; ntuple(n -> t[o + n], length(r)))
function getindex(t::Tuple, r::AbstractUnitRange{<:Real})
n = length(r)
n == 0 && return ()
a = Vector{eltype(t)}(undef, n)
o = first(r) - 1
for i = 1:n
el = t[o + i]
@inbounds a[i] = el
end
(a...,)
end
end

"""
Expand Down
7 changes: 5 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ function resolve(g::GlobalRef; force::Bool=false)
return g
end

_fieldnames(@nospecialize t) = isdefined(t, :names) ? t.names : t.name.names

"""
fieldname(x::DataType, i::Integer)
Expand All @@ -130,7 +132,7 @@ julia> fieldname(Rational, 2)
```
"""
function fieldname(t::DataType, i::Integer)
names = isdefined(t, :names) ? t.names : t.name.names
names = _fieldnames(t)
n_fields = length(names)
field_label = n_fields == 1 ? "field" : "fields"
i > n_fields && throw(ArgumentError("Cannot access field $i since type $t only has $n_fields $field_label."))
Expand All @@ -153,7 +155,8 @@ julia> fieldnames(Rational)
(:num, :den)
```
"""
fieldnames(t::DataType) = ntuple(i -> fieldname(t, i), fieldcount(t))
fieldnames(t::DataType) = (fieldcount(t); # error check to make sure type is specific enough
(_fieldnames(t)...,))
fieldnames(t::UnionAll) = fieldnames(unwrap_unionall(t))
fieldnames(t::Type{<:Tuple}) = ntuple(identity, fieldcount(t))

Expand Down

0 comments on commit 333d444

Please sign in to comment.