Skip to content

Commit 3a876cb

Browse files
authored
Merge branch 'master' into kf/rmunionpenalties
2 parents 1c6bf3b + feb2988 commit 3a876cb

File tree

7 files changed

+68
-45
lines changed

7 files changed

+68
-45
lines changed

base/compiler/tfuncs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ add_tfunc(neg_float, 1, 1, math_tfunc, 1)
184184
add_tfunc(add_float, 2, 2, math_tfunc, 1)
185185
add_tfunc(sub_float, 2, 2, math_tfunc, 1)
186186
add_tfunc(mul_float, 2, 2, math_tfunc, 4)
187-
add_tfunc(div_float, 2, 2, math_tfunc, 20)
187+
add_tfunc(div_float, 2, 2, math_tfunc, 4)
188188
add_tfunc(fma_float, 3, 3, math_tfunc, 5)
189189
add_tfunc(muladd_float, 3, 3, math_tfunc, 5)
190190

@@ -193,7 +193,7 @@ add_tfunc(neg_float_fast, 1, 1, math_tfunc, 1)
193193
add_tfunc(add_float_fast, 2, 2, math_tfunc, 1)
194194
add_tfunc(sub_float_fast, 2, 2, math_tfunc, 1)
195195
add_tfunc(mul_float_fast, 2, 2, math_tfunc, 2)
196-
add_tfunc(div_float_fast, 2, 2, math_tfunc, 10)
196+
add_tfunc(div_float_fast, 2, 2, math_tfunc, 2)
197197

198198
# bitwise operators
199199
# -----------------

base/strings/util.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,11 @@ end
778778

779779
# note: leave str untyped here to make it easier for packages like StringViews to hook in
780780
function _replace_(str, pat_repl::NTuple{N, Pair}, count::Int) where N
781-
count == 0 && return str
781+
count == 0 && return String(str)
782782
e1, patterns, replaces, rs, notfound = _replace_init(str, pat_repl, count)
783783
if notfound
784784
foreach(_free_pat_replacer, patterns)
785-
return str
785+
return String(str)
786786
end
787787
out = IOBuffer(sizehint=floor(Int, 1.2sizeof(str)))
788788
return String(take!(_replace_finish(out, str, count, e1, patterns, replaces, rs)))

doc/src/manual/performance-tips.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,27 @@ julia> !isconcretetype(Array), !isabstracttype(Array), isstructtype(Array), !isc
358358
```
359359
In this case, it would be better to avoid declaring `MyType` with a field `a::Array` and instead declare the field as `a::Array{T,N}` or as `a::A`, where `{T,N}` or `A` are parameters of `MyType`.
360360

361+
The previous advice is especially useful when the fields of a struct are meant to be functions, or more generally callable objects.
362+
It is very tempting to define a struct as follows:
363+
364+
```julia
365+
struct MyCallableWrapper
366+
f::Function
367+
end
368+
```
369+
370+
But since `Function` is an abstract type, every call to `wrapper.f` will require dynamic dispatch, due to the type instability of accessing the field `f`.
371+
Instead, you should write something like:
372+
373+
```julia
374+
struct MyCallableWrapper{F}
375+
f::F
376+
end
377+
```
378+
379+
which has nearly identical behavior but will be much faster (because the type instability is eliminated).
380+
Note that we do not impose `F<:Function`: this means callable objects which do not subtype `Function` are also allowed for the field `f`.
381+
361382
### Avoid fields with abstract containers
362383

363384
The same best practices also work for container types:

src/gc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,6 @@ static void gc_sweep_pool(int sweep_full)
15421542
pg->nfree = (GC_PAGE_SZ - (last_p - gc_page_data(last_p - 1))) / p->osize;
15431543
pg->has_young = 1;
15441544
}
1545-
p->newpages = NULL;
15461545
}
15471546
jl_gc_pagemeta_t *pg = ptls2->page_metadata_lazily_freed;
15481547
while (pg != NULL) {
@@ -1564,6 +1563,10 @@ static void gc_sweep_pool(int sweep_full)
15641563
pg = pg2;
15651564
}
15661565
ptls2->page_metadata_allocd = allocd;
1566+
for (int i = 0; i < JL_GC_N_POOLS; i++) {
1567+
jl_gc_pool_t *p = &ptls2->heap.norm_pools[i];
1568+
p->newpages = NULL;
1569+
}
15671570
}
15681571
}
15691572

stdlib/LinearAlgebra/src/special.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ end
334334
const _SpecialArrays = Union{Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal}
335335
const _Symmetric_DenseArrays{T,A<:Matrix} = Symmetric{T,A}
336336
const _Hermitian_DenseArrays{T,A<:Matrix} = Hermitian{T,A}
337-
const _Triangular_DenseArrays{T,A<:Matrix} = AbstractTriangular{T,A}
337+
const _Triangular_DenseArrays{T,A<:Matrix} = UpperOrLowerTriangular{T,A}
338338
const _Annotated_DenseArrays = Union{_SpecialArrays, _Triangular_DenseArrays, _Symmetric_DenseArrays, _Hermitian_DenseArrays}
339339
const _Annotated_Typed_DenseArrays{T} = Union{_Triangular_DenseArrays{T}, _Symmetric_DenseArrays{T}, _Hermitian_DenseArrays{T}}
340340
const _DenseConcatGroup = Union{Number, Vector, Adjoint{<:Any,<:Vector}, Transpose{<:Any,<:Vector}, Matrix, _Annotated_DenseArrays}

stdlib/LinearAlgebra/src/triangular.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
## Triangular
44

55
# could be renamed to Triangular when that name has been fully deprecated
6-
abstract type AbstractTriangular{T,S<:AbstractMatrix} <: AbstractMatrix{T} end
6+
abstract type AbstractTriangular{T} <: AbstractMatrix{T} end
77

88
# First loop through all methods that don't need special care for upper/lower and unit diagonal
9-
for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular,
10-
:UnitUpperTriangular)
9+
for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, :UnitUpperTriangular)
1110
@eval begin
12-
struct $t{T,S<:AbstractMatrix{T}} <: AbstractTriangular{T,S}
11+
struct $t{T,S<:AbstractMatrix{T}} <: AbstractTriangular{T}
1312
data::S
1413

1514
function $t{T,S}(data) where {T,S<:AbstractMatrix{T}}
@@ -854,9 +853,9 @@ for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular, :UnitUpperTr
854853
end
855854
end
856855

857-
errorbounds(A::AbstractTriangular{T,<:AbstractMatrix}, X::AbstractVecOrMat{T}, B::AbstractVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
856+
errorbounds(A::AbstractTriangular{T}, X::AbstractVecOrMat{T}, B::AbstractVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
858857
error("not implemented yet! Please submit a pull request.")
859-
function errorbounds(A::AbstractTriangular{TA,<:AbstractMatrix}, X::AbstractVecOrMat{TX}, B::AbstractVecOrMat{TB}) where {TA<:Number,TX<:Number,TB<:Number}
858+
function errorbounds(A::AbstractTriangular{TA}, X::AbstractVecOrMat{TX}, B::AbstractVecOrMat{TB}) where {TA<:Number,TX<:Number,TB<:Number}
860859
TAXB = promote_type(TA, TB, TX, Float32)
861860
errorbounds(convert(AbstractMatrix{TAXB}, A), convert(AbstractArray{TAXB}, X), convert(AbstractArray{TAXB}, B))
862861
end

test/reduce.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -439,39 +439,39 @@ end
439439

440440
# any & all
441441

442-
@test @inferred any([]) == false
443-
@test @inferred any(Bool[]) == false
444-
@test @inferred any([true]) == true
445-
@test @inferred any([false, false]) == false
446-
@test @inferred any([false, true]) == true
447-
@test @inferred any([true, false]) == true
448-
@test @inferred any([true, true]) == true
449-
@test @inferred any([true, true, true]) == true
450-
@test @inferred any([true, false, true]) == true
451-
@test @inferred any([false, false, false]) == false
452-
453-
@test @inferred all([]) == true
454-
@test @inferred all(Bool[]) == true
455-
@test @inferred all([true]) == true
456-
@test @inferred all([false, false]) == false
457-
@test @inferred all([false, true]) == false
458-
@test @inferred all([true, false]) == false
459-
@test @inferred all([true, true]) == true
460-
@test @inferred all([true, true, true]) == true
461-
@test @inferred all([true, false, true]) == false
462-
@test @inferred all([false, false, false]) == false
463-
464-
@test @inferred any(x->x>0, []) == false
465-
@test @inferred any(x->x>0, Int[]) == false
466-
@test @inferred any(x->x>0, [-3]) == false
467-
@test @inferred any(x->x>0, [4]) == true
468-
@test @inferred any(x->x>0, [-3, 4, 5]) == true
469-
470-
@test @inferred all(x->x>0, []) == true
471-
@test @inferred all(x->x>0, Int[]) == true
472-
@test @inferred all(x->x>0, [-3]) == false
473-
@test @inferred all(x->x>0, [4]) == true
474-
@test @inferred all(x->x>0, [-3, 4, 5]) == false
442+
@test @inferred(Union{Missing,Bool}, any([])) == false
443+
@test @inferred(any(Bool[])) == false
444+
@test @inferred(any([true])) == true
445+
@test @inferred(any([false, false])) == false
446+
@test @inferred(any([false, true])) == true
447+
@test @inferred(any([true, false])) == true
448+
@test @inferred(any([true, true])) == true
449+
@test @inferred(any([true, true, true])) == true
450+
@test @inferred(any([true, false, true])) == true
451+
@test @inferred(any([false, false, false])) == false
452+
453+
@test @inferred(Union{Missing,Bool}, all([])) == true
454+
@test @inferred(all(Bool[])) == true
455+
@test @inferred(all([true])) == true
456+
@test @inferred(all([false, false])) == false
457+
@test @inferred(all([false, true])) == false
458+
@test @inferred(all([true, false])) == false
459+
@test @inferred(all([true, true])) == true
460+
@test @inferred(all([true, true, true])) == true
461+
@test @inferred(all([true, false, true])) == false
462+
@test @inferred(all([false, false, false])) == false
463+
464+
@test @inferred(Union{Missing,Bool}, any(x->x>0, [])) == false
465+
@test @inferred(any(x->x>0, Int[])) == false
466+
@test @inferred(any(x->x>0, [-3])) == false
467+
@test @inferred(any(x->x>0, [4])) == true
468+
@test @inferred(any(x->x>0, [-3, 4, 5])) == true
469+
470+
@test @inferred(Union{Missing,Bool}, all(x->x>0, [])) == true
471+
@test @inferred(all(x->x>0, Int[])) == true
472+
@test @inferred(all(x->x>0, [-3])) == false
473+
@test @inferred(all(x->x>0, [4])) == true
474+
@test @inferred(all(x->x>0, [-3, 4, 5])) == false
475475

476476
@test reduce((a, b) -> a .| b, fill(trues(5), 24)) == trues(5)
477477
@test reduce((a, b) -> a .| b, fill(falses(5), 24)) == falses(5)

0 commit comments

Comments
 (0)