From 333d44431bbcad2f3e6f09072750ff88a67c1354 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Mon, 18 Jun 2018 13:01:40 -0400 Subject: [PATCH] remove a couple uses of `ntuple` to help the compiler --- base/range.jl | 13 +++++++++++-- base/reflection.jl | 7 +++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/base/range.jl b/base/range.jl index dbf0f407e61cf..86e5d9d6482bb 100644 --- a/base/range.jl +++ b/base/range.jl @@ -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 """ diff --git a/base/reflection.jl b/base/reflection.jl index 12d7cbcd46540..c9891bcc573b0 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -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) @@ -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.")) @@ -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))