luaL_Stream with own internal buffer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

luaL_Stream with own internal buffer

Ranier Vilela-2
Hi,

Each file opened in lua, has an associated luaL_Stream structure?
If so, is it possible to think about moving the buffer (luaL_Buffer) to this structure?
Would it be possible, in this way, to use this buffer, instead of the local buffer?

diff --git a/lauxlib.h b/lauxlib.h
index 59fef6af..60a6e8e3 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -218,6 +218,7 @@ LUALIB_API char *(luaL_buffinitsize) (lua_State *L, luaL_Buffer *B, size_t sz);
 
 typedef struct luaL_Stream {
+  luaL_Buffer b; /* stream internal buffer */
   FILE *f;  /* stream (NULL for incompletely created streams) */
   lua_CFunction closef;  /* to close stream (NULL for closed streams) */
 } luaL_Stream;

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: luaL_Stream with own internal buffer

Roberto Ierusalimschy
> Each file opened in lua, has an associated luaL_Stream structure?
> If so, is it possible to think about moving the buffer (luaL_Buffer) to
> this structure?
> Would it be possible, in this way, to use this buffer, instead of the local
> buffer?

What would be the gains?

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: luaL_Stream with own internal buffer

Ranier Vilela-2
Em qua., 30 de set. de 2020 às 17:26, Roberto Ierusalimschy <[hidden email]> escreveu:
> Each file opened in lua, has an associated luaL_Stream structure?
> If so, is it possible to think about moving the buffer (luaL_Buffer) to
> this structure?
> Would it be possible, in this way, to use this buffer, instead of the local
> buffer?

What would be the gains?
More fine control over buffer used by file operations.

1. Matchs size buffer with luaL_buffinitsize, instead of using luaL_buffinit.
  fw = assert(io.open("mybigfile.dat", "rb"))
  assert(fw:setvbuf("full", 8192 * 2))

2. Bypass, internal buffer fread / fwrite operations.
luaL_buffinitsize (L,  &LS->b , 8192 * 2);
int res = setvbuf(f, &LS->b, _IOFBF,  8192 * 2);

if called setvbuf, with NULL, a new buffer (size sz) is allocated and used.
if called setvbuf, with a buffer already allocated, fread/fwrite operations use this buffer,
avoiding copies.

3. Makes it possible to reuse the buffer for other operations.

Disadvantages:

1. Buffer need allocated before operations, but, if user
uses setvbuf, this already occurs, but the memory is allocated in libc.

regards,
Ranier Vilela