Increase system performance by maximizing your cache

Unix Insider |  Networking



  • The standard I/O buffer

    Simple text filters in Unix process data one character at a time using
    the putchar and getchar macros,
    printf, and the related stdio.h routines. To
    avoid a system call for every read or write of one character,
    stdio uses a buffer to cache the data for each file. The
    buffer size is 1 kilobyte, so for every 1024 calls of
    getchar, a read system call of 1 kilobyte will occur; for
    every eight system calls, a filesystem block will be paged in from
    disk. If your application is reading and writing data in blocks of 1
    kilobyte or more, there is no point using the stdio
    library, you can save time by using the open/read/write
    calls instead of fopen/fread/fwrite. Conversely, if you
    are using open/read/write for a few bytes at a time you are generating
    a lot of unnecessary system calls and stdio would be
    faster.



  • Read, write, and memory mapping

    When you read data, you must first allocate a buffer, then read into
    that buffer. The data is copied out of a page in the in-memory page
    cache to your buffer, so there are two copies of the data in memory.
    This wastes memory and wastes the time it takes to do the copy. The
    alternative is to use mmap to map the page directly into
    your address space. Data accesses then occur directly to the page in
    the in-memory page cache, with no copying and no wasted space.

  • Join us:
    Facebook

    Twitter

    Pinterest

    Tumblr

    LinkedIn

    Google+

    Answers - Powered by ITworld

    ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

    Join us:
    Facebook

    Twitter

    Pinterest

    Tumblr

    LinkedIn

    Google+

    Ask a Question