Use fs_read instead of fread

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
lerabot
Insane DCEmu
Insane DCEmu
Posts: 134
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Nov 01, 2015 8:25 pm
Has thanked: 2 times
Been thanked: 19 times

Use fs_read instead of fread

Post by lerabot »

I was doing some testing lately after resintalling ym BBA dev setup and felt like the BBA transfer speed was much slower than I remembered.

I stumbled on this post by TapamN and this help the solution:
One issue you might run into is slow file access over ethernet. Using the C library stdio.h functions (fread, fwrite) can be much slower than using the KOS filesystem calls directly (fs_read, fs_write) when reading/writing large blocks. With stdio, you get something like tens of KB/sec, while with KOS you can get over 1 MB/sec. Stdio might be faster when preforming many very small operations. dcload-serial doesn't have this issue.
There are the timing for loading a 256x256 .dtex texture (131kb)

FREAD
BBA: 3300ms
Serial: 6100ms

FS_OPEN
BBA: 70ms
Serial: 600ms

I'll test with GDEmu and CD and update my result later.
These users thanked the author lerabot for the post (total 3):
|darc|T_chanIan Robinson
TapamN
DC Developer
DC Developer
Posts: 105
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 90 times

Re: Use fs_read instead of fread

Post by TapamN »

So, the reason BBA is so slow is because newlib breaks up every I/O operation into tiny chunks, and dcload-ip is kind of half baked and doesn't handle many small requests well.

I feel incredibly stupid for not thinking about this earlier, but you can use setvbuf to disable stdio's buffering on a stream, which causes newlib to use large, single calls to KOS's fs_read/write operations, getting rid of the BBA's per-request overhead.

You would do something like this:

Code: Select all

FILE *f = fopen("/pc/blahblah", "r");
if (f) {
        setvbuf(f, NULL, _IONBF, 0);  //This makes it fast
        int amtread = fread(f, 1, sizeof(buff), buff);
}
These users thanked the author TapamN for the post (total 4):
BB HoodIan Robinson|darc|GyroVorbis
User avatar
BB Hood
DC Developer
DC Developer
Posts: 189
Joined: Fri Mar 30, 2007 12:09 am
Has thanked: 41 times
Been thanked: 10 times

Re: Use fs_read instead of fread

Post by BB Hood »

Oh Snap I have to try that out!
User avatar
GyroVorbis
Elysian Shadows Developer
Elysian Shadows Developer
Posts: 1874
Joined: Mon Mar 22, 2004 4:55 pm
Location: #%^&*!!!11one Super Sonic
Has thanked: 80 times
Been thanked: 62 times
Contact:

Re: Use fs_read instead of fread

Post by GyroVorbis »

Thanks for this... currently digging into what we can do about making Newlib's default buffering behavior less... stupid. lol.
Post Reply