From 89589c71c1eaa51a9f2d1c75b12ae47668ffc662 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 30 Jan 2025 06:34:48 -0800 Subject: get-csv: speed up with unsafe get-char. I'm seeing about a 6% improvement in get-csv from this. * stream.c (us_get_char, us_unget_char): New static functions, which assume all arguments have correct type. (get_csv): If we use source_opt, validate that it's a stream with class_check. Use us_get_char and use_unget_char. --- stream.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/stream.c b/stream.c index 20e5c933..1403f1b2 100644 --- a/stream.c +++ b/stream.c @@ -3086,6 +3086,12 @@ val get_char(val stream_in) return ops->get_char(stream); } +static val us_get_char(val stream) +{ + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); + return ops->get_char(stream); +} + val get_byte(val stream_in) { val self = lit("get-byte"); @@ -3114,6 +3120,12 @@ val unget_char(val ch, val stream_in) return ops->unget_char(stream, ch); } +static val us_unget_char(val ch, val stream) +{ + struct strm_ops *ops = coerce(struct strm_ops *, stream->co.ops); + return ops->unget_char(stream, ch); +} + val unget_byte(val byte, val stream_in) { val self = lit("unget-byte"); @@ -5424,24 +5436,25 @@ val make_byte_input_stream(val obj) val get_csv(val source_opt) { + val self = lit("get-csv"); val source = if3(missingp(source_opt), std_input, if3(stringp(source_opt), make_string_input_stream(source_opt), - source_opt)); + (class_check(self, source_opt, stream_cls), source_opt))); val record = nil, field = nil; enum { init, rfield, qfield, quot } state = init; int done = 0; while (!done) { - val ch = get_char(source); + val ch = us_get_char(source); if (ch == chr('\r')) { - val ch2 = get_char(source); + val ch2 = us_get_char(source); if (ch2 == chr('\n')) ch = ch2; else if (ch2) - unget_char(ch2, source); + us_unget_char(ch2, source); } else if (ch == nil) { if (state == init) return nil; -- cgit v1.2.3