Skip to content

Commit dddda07

Browse files
authored
Merge pull request #35618 from JuliaLang/jb/iosbufsize
change default ios buffer size to 32k
2 parents d192a73 + 1f0b4b0 commit dddda07

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/flisp/iostream.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,13 @@ value_t fl_ioreaduntil(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
352352
cv->len = n;
353353
if (dest.buf != data) {
354354
// outgrew initial space
355-
cv->data = dest.buf;
355+
size_t sz;
356+
cv->data = ios_take_buffer(&dest, &sz);
356357
cv_autorelease(fl_ctx, cv);
357358
}
358-
((char*)cv->data)[n] = '\0';
359+
else {
360+
((char*)cv->data)[n] = '\0';
361+
}
359362
if (n == 0 && ios_eof(src))
360363
return fl_ctx->FL_EOF;
361364
return str;

src/support/ios.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,12 @@ static char *_buf_realloc(ios_t *s, size_t sz)
198198

199199
if (s->ownbuf && s->buf != &s->local[0]) {
200200
// if we own the buffer we're free to resize it
201-
// always allocate 1 bigger in case user wants to add a NUL
202-
// terminator after taking over the buffer
203-
temp = (char*)LLT_REALLOC(s->buf, sz+1);
201+
temp = (char*)LLT_REALLOC(s->buf, sz);
204202
if (temp == NULL)
205203
return NULL;
206204
}
207205
else {
208-
temp = (char*)LLT_ALLOC(sz+1);
206+
temp = (char*)LLT_ALLOC(sz);
209207
if (temp == NULL)
210208
return NULL;
211209
s->ownbuf = 1;
@@ -691,22 +689,24 @@ char *ios_take_buffer(ios_t *s, size_t *psize)
691689

692690
ios_flush(s);
693691

694-
if (s->buf == &s->local[0]) {
692+
if (s->buf == &s->local[0] || s->buf == NULL || (!s->ownbuf && s->size == s->maxsize)) {
695693
buf = (char*)LLT_ALLOC((size_t)s->size + 1);
696694
if (buf == NULL)
697695
return NULL;
698696
if (s->size)
699697
memcpy(buf, s->buf, (size_t)s->size);
700698
}
699+
else if (s->size == s->maxsize) {
700+
buf = (char*)LLT_REALLOC(s->buf, (size_t)s->size + 1);
701+
if (buf == NULL)
702+
return NULL;
703+
}
701704
else {
702-
if (s->buf == NULL)
703-
buf = (char*)LLT_ALLOC((size_t)s->size + 1);
704-
else
705-
buf = s->buf;
705+
buf = s->buf;
706706
}
707707
buf[s->size] = '\0';
708708

709-
*psize = s->size+1; // buffer is actually 1 bigger for terminating NUL
709+
*psize = s->size + 1;
710710

711711
/* empty stream and reinitialize */
712712
_buf_init(s, s->bm);
@@ -1203,10 +1203,14 @@ int ios_vprintf(ios_t *s, const char *format, va_list args)
12031203
char *start = s->buf + s->bpos;
12041204
c = vsnprintf(start, avail, format, args);
12051205
if (c < 0) {
1206+
#if defined(_OS_WINDOWS_)
1207+
// on windows this can mean not enough space was available
1208+
#else
12061209
va_end(al);
12071210
return c;
1211+
#endif
12081212
}
1209-
if (c < avail) {
1213+
else if (c < avail) {
12101214
s->bpos += (size_t)c;
12111215
_write_update_pos(s);
12121216
// TODO: only works right if newline is at end

src/support/ios.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ typedef enum { bm_none=UV_HANDLE_TYPE_MAX+1, bm_line, bm_block, bm_mem } bufmode
2020
typedef enum { bst_none, bst_rd, bst_wr } bufstate_t;
2121

2222
#define IOS_INLSIZE 54
23-
#define IOS_BUFSIZE 131072
23+
#define IOS_BUFSIZE 32768
2424

2525
#ifdef _P64
2626
#define ON_P64(x) x
@@ -95,7 +95,7 @@ JL_DLLEXPORT int ios_eof_blocking(ios_t *s);
9595
JL_DLLEXPORT int ios_flush(ios_t *s);
9696
JL_DLLEXPORT int ios_close(ios_t *s) JL_NOTSAFEPOINT;
9797
JL_DLLEXPORT int ios_isopen(ios_t *s);
98-
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // release buffer to caller
98+
JL_DLLEXPORT char *ios_take_buffer(ios_t *s, size_t *psize); // nul terminate and release buffer to caller
9999
// set buffer space to use
100100
JL_DLLEXPORT int ios_setbuf(ios_t *s, char *buf, size_t size, int own) JL_NOTSAFEPOINT;
101101
JL_DLLEXPORT int ios_bufmode(ios_t *s, bufmode_t mode) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)