Commit c686e4a
authored
Currently the `@nospecialize`-d `push!(::Vector{Any}, ...)` can only
take a single item and we will end up with runtime dispatch when we try
to call it with multiple items:
```julia
julia> code_typed(push!, (Vector{Any}, Any))
1-element Vector{Any}:
CodeInfo(
1 ─ $(Expr(:foreigncall, :(:jl_array_grow_end), Nothing, svec(Any, UInt64), 0, :(:ccall), Core.Argument(2), 0x0000000000000001, 0x0000000000000001))::Nothing
│ %2 = Base.arraylen(a)::Int64
│ Base.arrayset(true, a, item, %2)::Vector{Any}
└── return a
) => Vector{Any}
julia> code_typed(push!, (Vector{Any}, Any, Any))
1-element Vector{Any}:
CodeInfo(
1 ─ %1 = Base.append!(a, iter)::Vector{Any}
└── return %1
) => Vector{Any}
```
This commit adds a new specialization that it can take arbitrary-length
items. Our compiler should still be able to optimize the single-input
case as before via the dispatch mechanism.
```julia
julia> code_typed(push!, (Vector{Any}, Any))
1-element Vector{Any}:
CodeInfo(
1 ─ $(Expr(:foreigncall, :(:jl_array_grow_end), Nothing, svec(Any, UInt64), 0, :(:ccall), Core.Argument(2), 0x0000000000000001, 0x0000000000000001))::Nothing
│ %2 = Base.arraylen(a)::Int64
│ Base.arrayset(true, a, item, %2)::Vector{Any}
└── return a
) => Vector{Any}
julia> code_typed(push!, (Vector{Any}, Any, Any))
1-element Vector{Any}:
CodeInfo(
1 ─ %1 = Base.arraylen(a)::Int64
│ $(Expr(:foreigncall, :(:jl_array_grow_end), Nothing, svec(Any, UInt64), 0, :(:ccall), Core.Argument(2), 0x0000000000000002, 0x0000000000000002))::Nothing
└── goto JuliaLang#7 if not true
2 ┄ %4 = φ (#1 => 1, JuliaLang#6 => %14)::Int64
│ %5 = φ (#1 => 1, JuliaLang#6 => %15)::Int64
│ %6 = Base.getfield(x, %4, true)::Any
│ %7 = Base.add_int(%1, %4)::Int64
│ Base.arrayset(true, a, %6, %7)::Vector{Any}
│ %9 = (%5 === 2)::Bool
└── goto #4 if not %9
3 ─ goto JuliaLang#5
4 ─ %12 = Base.add_int(%5, 1)::Int64
└── goto JuliaLang#5
5 ┄ %14 = φ (#4 => %12)::Int64
│ %15 = φ (#4 => %12)::Int64
│ %16 = φ (#3 => true, #4 => false)::Bool
│ %17 = Base.not_int(%16)::Bool
└── goto JuliaLang#7 if not %17
6 ─ goto #2
7 ┄ return a
) => Vector{Any}
```
This commit also adds the equivalent implementations for `pushfirst!`.
1 parent f849517 commit c686e4a
2 files changed
+55
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1053 | 1053 | | |
1054 | 1054 | | |
1055 | 1055 | | |
1056 | | - | |
| 1056 | + | |
| 1057 | + | |
1057 | 1058 | | |
1058 | | - | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
1059 | 1069 | | |
1060 | 1070 | | |
1061 | 1071 | | |
| |||
1385 | 1395 | | |
1386 | 1396 | | |
1387 | 1397 | | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
1388 | 1414 | | |
1389 | 1415 | | |
1390 | 1416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1375 | 1375 | | |
1376 | 1376 | | |
1377 | 1377 | | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
| 1391 | + | |
| 1392 | + | |
| 1393 | + | |
| 1394 | + | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
0 commit comments