Changeset 20667af in mainline


Ignore:
Timestamp:
2020-11-21T19:56:45Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
68698ba
Parents:
06d0c81
git-author:
Jiri Svoboda <jiri@…> (2020-11-21 19:25:27)
git-committer:
Jiri Svoboda <jiri@…> (2020-11-21 19:56:45)
Message:

Remember current position in libriff and optimize sequential reading

TPF files currently contain a lot of chunks and each chunk means
a ftell/fseek. libc's ftell/fseek flush the I/O buffer, leading
to a lot of unnecessary IPC. This all leads to reading typeface to
take way too long.

We remember the current file position
in a RIFF reader and avoid ftell and also avoid fseek at the end
of a chunk, if possible (if we need to advance just a little bit,
we read instead of seek).

Location:
uspace/lib/riff
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/riff/include/types/riff/chunk.h

    r06d0c81 r20667af  
    5454typedef struct {
    5555        FILE *f;
     56        /** Current file position */
     57        long pos;
    5658} riffr_t;
    5759
  • uspace/lib/riff/src/chunk.c

    r06d0c81 r20667af  
    213213        }
    214214
     215        rr->pos = 0;
     216
    215217        rr->f = fopen(fname, "rb");
    216218        if (rr->f == NULL) {
     
    302304{
    303305        errno_t rc;
    304         long pos;
    305 
    306         pos = ftell(parent->riffr->f);
    307         if (pos < 0) {
    308                 rc = EIO;
    309                 goto error;
    310         }
    311306
    312307        rchunk->riffr = parent->riffr;
    313         rchunk->ckstart = pos + 8;
     308        rchunk->ckstart = parent->riffr->pos + 8;
    314309        rc = riff_read_uint32(parent, &rchunk->ckid);
    315310        if (rc != EOK)
     
    402397errno_t riff_rchunk_seek(riff_rchunk_t *rchunk, long offset, int whence)
    403398{
    404         long pos;
    405399        long dest;
    406400        int rv;
     
    414408                break;
    415409        case SEEK_CUR:
    416                 pos = ftell(rchunk->riffr->f);
    417                 if (pos < 0)
    418                         return EIO;
    419                 dest = pos + offset;
     410                dest = rchunk->riffr->pos + offset;
    420411                break;
    421412        default:
     
    432423                return EIO;
    433424
     425        rchunk->riffr->pos = dest;
    434426        return EOK;
    435427}
     
    482474{
    483475        long ckend;
     476        uint8_t byte;
     477        size_t nread;
     478        errno_t rc;
    484479
    485480        ckend = riff_rchunk_get_ndpos(rchunk);
    486         if (fseek(rchunk->riffr->f, ckend, SEEK_SET) < 0)
    487                 return EIO;
     481        if (rchunk->riffr->pos < ckend &&
     482            ckend <= rchunk->riffr->pos + 512) {
     483                /* (Buffered) reading is faster than seeking */
     484                while (rchunk->riffr->pos < ckend) {
     485                        rc = riff_read(rchunk, &byte, sizeof(byte), &nread);
     486                        if (rc != EOK)
     487                                return rc;
     488
     489                        if (nread != sizeof(byte))
     490                                return EIO;
     491                }
     492
     493        } else if (rchunk->riffr->pos != ckend) {
     494                /* Need to seek (backwards or too far) */
     495                if (fseek(rchunk->riffr->f, ckend, SEEK_SET) < 0)
     496                        return EIO;
     497                rchunk->riffr->pos = ckend;
     498        }
    488499
    489500        return EOK;
     
    511522        long toread;
    512523
    513         pos = ftell(rchunk->riffr->f);
    514         if (pos < 0)
    515                 return EIO;
     524        pos = rchunk->riffr->pos;
    516525
    517526        ckend = riff_rchunk_get_end(rchunk);
     
    529538                return EIO;
    530539
     540        rchunk->riffr->pos += *nread;
    531541        return EOK;
    532542}
Note: See TracChangeset for help on using the changeset viewer.