Skip to content

Commit

Permalink
Performance improvements for PointVector, RayVector, SubObjectIterator
Browse files Browse the repository at this point in the history
Add missing eltype methods and add type assertions to their getindex methods
so that iteration over objects of these types has a chance of being typestable.

Also ensure that the return value of `size` is recognized as Tuple{Int}.

Silly microbenchmark, before:

    julia> v = PointVector{ZZRingElem}(matrix(ZZ, [1 2 3 ; ]));

    julia> @Btime prod(v)
      1.217 μs (21 allocations: 336 bytes)

After:

    julia> @Btime prod(v);
      288.449 ns (5 allocations: 80 bytes)
  • Loading branch information
fingolfin committed Feb 15, 2024
1 parent 6d3e523 commit 74fc037
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/PolyhedralGeometry/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ for (T, _t) in ((:PointVector, :point_vector), (:RayVector, :ray_vector))
end

Base.IndexStyle(::Type{<:$T}) = IndexLinear()
Base.getindex(po::$T, i::Base.Integer) = po.p[1, i]
Base.getindex(po::$T{U}, i::Base.Integer) where U = po.p[1, i]::U

function Base.setindex!(po::$T, val, i::Base.Integer)
@boundscheck 1 <= length(po) <= i
Expand All @@ -23,7 +23,7 @@ for (T, _t) in ((:PointVector, :point_vector), (:RayVector, :ray_vector))

Base.firstindex(::$T) = 1
Base.lastindex(iter::$T) = length(iter)
Base.size(po::$T) = (size(po.p, 2),)
Base.size(po::$T) = (size(po.p, 2)::Int,)

coefficient_field(po::$T) = base_ring(po.p)

Expand Down Expand Up @@ -237,7 +237,7 @@ Base.IndexStyle(::Type{<:SubObjectIterator}) = IndexLinear()

function Base.getindex(iter::SubObjectIterator{T}, i::Base.Integer) where T
@boundscheck 1 <= i && i <= iter.n
return iter.Acc(T, iter.Obj, i; iter.options...)
return iter.Acc(T, iter.Obj, i; iter.options...)::T
end

Base.firstindex(::SubObjectIterator) = 1
Expand Down

0 comments on commit 74fc037

Please sign in to comment.