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 May 2, 2021
1 parent 6e631e6 commit b6f31f4
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 @@ -1863,6 +1864,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 @@ -1894,6 +1896,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 @@ -1925,6 +1928,7 @@ PERLIO_FUNCS_DECL(PerlIO_raw) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
NULL,
};
/*--------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -2880,6 +2884,7 @@ PERLIO_FUNCS_DECL(PerlIO_unix) = {
NULL, /* get_ptr */
NULL, /* get_cnt */
NULL, /* set_ptrcnt */
PerlIOBase_readdelim,
};

/*--------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -3753,6 +3758,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 @@ -4100,6 +4106,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 @@ -4377,6 +4425,7 @@ PERLIO_FUNCS_DECL(PerlIO_perlio) = {
PerlIOBuf_get_ptr,
PerlIOBuf_get_cnt,
PerlIOBuf_set_ptrcnt,
PerlIOBuf_readdelim,
};

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


Expand Down Expand Up @@ -4859,6 +4909,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 @@ -205,6 +205,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 b6f31f4

Please sign in to comment.