From 117f11fd62a7541704e600ab2a9821f3f9e91395 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 4 Jul 2019 07:20:40 -0700 Subject: sha256: recycle I/O buffer used in stream hash. * chksum.c (sha256_stream): Use iobuf_get and iobuf_put. * gc.c (gc): Do not mark the list of recycled buffers; just consider them to be garbage and clear the list, like we do with recycled conses via rcyc_empty. * stream.c (iobuf_free_list): New static variable. (iobuf_get, iobuf_put, iobuf_list_empty): New functions. * stream.h (iobuf_get, iobuf_put, iobuf_list_empty): Declared. --- chksum.c | 5 +++-- gc.c | 1 + stream.c | 27 +++++++++++++++++++++++++++ stream.h | 4 +++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/chksum.c b/chksum.c index 588bdf92..1863ba47 100644 --- a/chksum.c +++ b/chksum.c @@ -51,8 +51,8 @@ val sha256_stream(val stream, val nbytes) { SHA256_t s256; unsigned char *hash = chk_malloc(SHA256_DIGEST_LENGTH); - val bfsz = num_fast(BUFSIZ); - val buf = make_buf(bfsz, nil, nil); + val buf = iobuf_get(); + val bfsz = length_buf(buf); SHA256_init(&s256); if (null_or_missing_p(nbytes)) { @@ -88,6 +88,7 @@ val sha256_stream(val stream, val nbytes) } SHA256_final(&s256, hash); + iobuf_put(buf); return make_borrowed_buf(num_fast(SHA256_DIGEST_LENGTH), hash); } diff --git a/gc.c b/gc.c index 178e1cc6..4c8d9f6d 100644 --- a/gc.c +++ b/gc.c @@ -754,6 +754,7 @@ void gc(void) save_context(mc); gc_enabled = 0; rcyc_empty(); + iobuf_list_empty(); mark(&mc, &gc_stack_top); hash_process_weak(); prepare_finals(); diff --git a/stream.c b/stream.c index ec9a7f66..4cad323d 100644 --- a/stream.c +++ b/stream.c @@ -4605,6 +4605,33 @@ val make_byte_input_stream(val obj) } } +static val iobuf_free_list; + +val iobuf_get(void) +{ + val buf = iobuf_free_list; + + if (buf) { + val next = buf->b.len; + buf->b.len = buf->b.size; + iobuf_free_list = next; + return buf; + } else { + return make_buf(num_fast(BUFSIZ), nil, nil); + } +} + +void iobuf_put(val buf) +{ + buf->b.len = iobuf_free_list; + iobuf_free_list = buf; +} + +void iobuf_list_empty(void) +{ + iobuf_free_list = nil; +} + void stream_init(void) { prot1(&ap_regex); diff --git a/stream.h b/stream.h index 2887b305..bc83122c 100644 --- a/stream.h +++ b/stream.h @@ -240,5 +240,7 @@ val base_name(val path); val dir_name(val path); val path_cat(val dir_name, val base_name); val make_byte_input_stream(val obj); - +val iobuf_get(void); +void iobuf_put(val buf); +void iobuf_list_empty(void); void stream_init(void); -- cgit v1.2.3