Skip to content

File i/o performance problems #5

Description

@mihalicyn

Let's see following code:

-- file have size 34406 bytes
f = io.open("/root/dev/libs_port.patch", 'r')

function fsop()
    f:seek('set', 0)
    s = f:read('all')
end

starttime = os.clock()
for i=1,18000
do
    fsop()
end
endtime = (os.clock() - starttime)

print(s:len())
f:close()

print("os.clock() diff " .. endtime)

Results

9000 fsop ~650 timediff kern
        ~0.18 timediff user
4500 fsop ~320 kernel
        ~0.1 user
18000 fsop ~1280 kernel
            ~0.37 user

As we can see scalability factor is two and times ratio is approximately two.

After that I made some experiments...
/usr/src/external/mit/lua/dist/src/liolib.c
original code

static void read_all (lua_State *L, FILE *f) {
  size_t nr;
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  do {  /* read file in chunks of LUAL_BUFFERSIZE bytes */
    char *p = luaL_prepbuffer(&b);
    nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
    luaL_addsize(&b, nr);
  } while (nr == LUAL_BUFFERSIZE);
  luaL_pushresult(&b);  /* close buffer */
}

change to:

static void read_all (lua_State *L, FILE *f) {
  size_t nr;
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  int fsize = 34406;
  do {  /* read file in chunks of LUAL_BUFFERSIZE bytes */
    char *p = luaL_prepbuffer(&b);
    //nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, f);
    fsize -= LUAL_BUFFERSIZE;
    nr = LUAL_BUFFERSIZE;
    if (fsize < 0)
        nr = fsize + LUAL_BUFFERSIZE;        

    luaL_addsize(&b, nr);
  } while (nr == LUAL_BUFFERSIZE);
  luaL_pushresult(&b);  /* close buffer */
}

and times changes to ~1000 for 18k, ~500 for 9k...
Therefore, performance problems caused by memory allocator not by dofilereadv used in fread implementation.

I'm using code from that repo with NetBSD 7.1 generic kernel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions