Skip to content

Commit c8638dd

Browse files
authored
* Don't return a parsed value from parsecustom! since that is type unstable (#20)
* Don't attempt to grow the result buffer if we have enough capacity * v0.2.1
1 parent 2f23fbb commit c8638dd

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ChunkedCSV"
22
uuid = "c0d0730e-6432-44b2-a51e-6ec55e1c8b99"
33
authors = ["Tomáš Drvoštěp <[email protected]>"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
ChunkedBase = "a380dd43-0ebf-4429-88d6-6f06ea920732"

src/populate_result_buffer.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ end
3535
@inline function parsecustom!(::Type{customtypes}, row_bytes, pos, len, col_idx, cols, options, _type) where {customtypes}
3636
if @generated
3737
block = Expr(:block)
38+
pushfirst!(block.args, :(error(lazy"Unreachable: type not matched")))
3839
for i = 1:fieldcount(customtypes)
3940
T = fieldtype(customtypes, i)
4041
pushfirst!(block.args, quote
4142
if type === $T
4243
res = Parsers.xparse($T, row_bytes, pos, len, options)::Parsers.Result{$T}
4344
(val, tlen, code) = res.val, res.tlen, res.code
4445
unsafe_push!(cols[col_idx]::BufferedVector{$T}, val)
45-
return val, tlen, code
46+
return tlen, code
4647
end
4748
end)
4849
end
@@ -55,7 +56,7 @@ end
5556
res = Parsers.xparse(_type, row_bytes, pos, len, options)::Parsers.Result{_type}
5657
(val, tlen, code) = res.val, res.tlen, res.code
5758
unsafe_push!(cols[col_idx]::BufferedVector{_type}, val)
58-
return val, tlen, code
59+
return tlen, code
5960
end
6061
end
6162

@@ -74,7 +75,8 @@ function ChunkedBase.populate_result_buffer!(
7475
errored_idx = 1
7576
options = parsing_ctx.options
7677

77-
Base.ensureroom(result_buf, ceil(Int, length(newlines_segment) * 1.01))
78+
# If we need to grow the buffer, we add a bit of extra room to avoid having to reallocate too often
79+
capacity(result_buf) < (length(newlines_segment)-1) && Base.ensureroom(result_buf, ceil(Int, length(newlines_segment) * 1.01))
7880

7981
ignorerepeated = options.ignorerepeated::Bool
8082
ignoreemptyrows = options.ignoreemptylines::Bool
@@ -163,7 +165,7 @@ function ChunkedBase.populate_result_buffer!(
163165
(val, tlen, code) = res.val, res.tlen, res.code
164166
unsafe_push!(cols[col_idx]::BufferedVector{Parsers.PosLen31}, Parsers.PosLen31(prev_newline+val.pos, val.len, val.missingvalue, val.escapedvalue))
165167
else
166-
(val, tlen, code) = parsecustom!(CT, row_bytes, pos, len, col_idx, cols, options, schema[col_idx])
168+
(tlen, code) = parsecustom!(CT, row_bytes, pos, len, col_idx, cols, options, schema[col_idx])
167169
end
168170
if Parsers.sentinel(code)
169171
row_status |= RowStatus.MissingValues

src/result_buffer.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ function _push_buffers!(::Type{T}, out, i, n) where {T}
220220
end
221221

222222
Base.length(buf::TaskResultBuffer) = length(buf.row_statuses)
223+
capacity(buf::TaskResultBuffer) = length(buf.row_statuses.elements)
223224

224225
function Base.empty!(buf::TaskResultBuffer)
225226
foreach(empty!, buf.cols)

0 commit comments

Comments
 (0)