Skip to content

Commit

Permalink
Add fast readdelim to main buffering layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Leont committed Sep 20, 2021
1 parent 813ab4c commit 301acdc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions perlio.c
Expand Up @@ -1054,6 +1054,7 @@ PERLIO_FUNCS_DECL(PerlIO_remove) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
NULL,
};

PerlIO_list_t *
Expand Down Expand Up @@ -1857,6 +1858,7 @@ PERLIO_FUNCS_DECL(PerlIO_utf8) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
NULL,
};

PERLIO_FUNCS_DECL(PerlIO_byte) = {
Expand Down Expand Up @@ -1888,6 +1890,7 @@ PERLIO_FUNCS_DECL(PerlIO_byte) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
NULL,
};

PERLIO_FUNCS_DECL(PerlIO_raw) = {
Expand Down Expand Up @@ -1919,6 +1922,7 @@ PERLIO_FUNCS_DECL(PerlIO_raw) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
NULL,
};
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -2874,6 +2878,7 @@ PERLIO_FUNCS_DECL(PerlIO_unix) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
PerlIOBase_readdelim,
};

/*--------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -3747,6 +3752,7 @@ PERLIO_FUNCS_DECL(PerlIO_stdio) = {
NULL,
NULL,
#endif /* USE_STDIO_PTR */
PerlIOBase_readdelim,
};

/* Note that calls to PerlIO_exportFILE() are reversed using
Expand Down Expand Up @@ -4094,6 +4100,48 @@ PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count)
return 0;
}

/* this is a helper funcion that returns the buffer count, an tries to refill if it's empty */
STATIC SSize_t
fill_count(pTHX_ PerlIO* f) {
Size_t cnt = PerlIO_get_cnt(f);
if (!cnt && PerlIO_fill(f) == 0)
cnt = PerlIO_get_cnt(f);
return cnt;
}

SSize_t
PerlIOBuf_readdelim(pTHX_ PerlIO *f, STDCHAR *vbuf, Size_t count, STDCHAR delim)
{
SSize_t read = 0;
Size_t avail = fill_count(aTHX_ f);
STDCHAR* ptr = (STDCHAR*)PerlIO_get_ptr(f);
Size_t len = MIN(avail, count);
STDCHAR *found = (STDCHAR*)memchr(ptr, delim, len);
if (avail == 0)
return PerlIO_error(f) ? -1 : 0;
if (found) {
Size_t offered = found + 1 - ptr;
memcpy(vbuf + read, ptr, offered);
read += offered;
PerlIO_set_ptrcnt(f, found + 1, avail - offered);
}
else {
memcpy(vbuf + read, ptr, len);
read += len;
PerlIO_set_ptrcnt(f, ptr + len, avail - len);

if (count - read) {
SSize_t next;
next = PerlIO_readdelim(f, vbuf + read, count - read, delim);
if (next >= 0)
return read + next;
else
return next; /* XXX */
}
}
return read;
}

SSize_t
PerlIOBuf_unread(pTHX_ PerlIO *f, const void *vbuf, Size_t count)
{
Expand Down Expand Up @@ -4371,6 +4419,7 @@ PERLIO_FUNCS_DECL(PerlIO_perlio) = {
PerlIOBuf_get_ptr,
PerlIOBuf_get_cnt,
PerlIOBuf_set_ptrcnt,
PerlIOBuf_readdelim,
};

/*--------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -4494,6 +4543,7 @@ PERLIO_FUNCS_DECL(PerlIO_pending) = {
PerlIOBuf_get_ptr,
PerlIOBuf_get_cnt,
PerlIOPending_set_ptrcnt,
PerlIOBuf_readdelim,
};


Expand Down Expand Up @@ -4853,6 +4903,7 @@ PERLIO_FUNCS_DECL(PerlIO_crlf) = {
PerlIOBuf_get_ptr,
PerlIOCrlf_get_cnt,
PerlIOCrlf_set_ptrcnt,
PerlIOBuf_readdelim,
};

PerlIO *
Expand Down
1 change: 1 addition & 0 deletions perliol.h
Expand Up @@ -202,6 +202,7 @@ PERL_CALLCONV PerlIO * PerlIOBuf_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *
PERL_CALLCONV IV PerlIOBuf_popped(pTHX_ PerlIO *f);
PERL_CALLCONV IV PerlIOBuf_pushed(pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab);
PERL_CALLCONV SSize_t PerlIOBuf_read(pTHX_ PerlIO *f, void *vbuf, Size_t count);
PERL_CALLCONV SSize_t PerlIOBuf_readdelim(pTHX_ PerlIO *f, STDCHAR *vbuf, Size_t count, STDCHAR delim);
PERL_CALLCONV IV PerlIOBuf_seek(pTHX_ PerlIO *f, Off_t offset, int whence);
PERL_CALLCONV void PerlIOBuf_set_ptrcnt(pTHX_ PerlIO *f, STDCHAR * ptr, SSize_t cnt);
PERL_CALLCONV Off_t PerlIOBuf_tell(pTHX_ PerlIO *f);
Expand Down

0 comments on commit 301acdc

Please sign in to comment.