AudioStreamWAV::load_from_file: Do not load file into memory#115235
AudioStreamWAV::load_from_file: Do not load file into memory#115235DeeJayLSP wants to merge 1 commit intogodotengine:masterfrom
AudioStreamWAV::load_from_file: Do not load file into memory#115235Conversation
|
We discussed this PR briefly in the audio meeting. Makes sense to avoid copying into a buffer for no reason. Am I understanding it right that this is a performance optimization (reduced CPU load + RAM use)? If so, please provide benchmarks / profiles, as per our optimization guidelines. W were also wondering if there could have been any reason for |
I wouldn't say this is a performance optimization, but more like "changing to what it used to be" before #93831 in a non-breaking way. Mostly because I never understood why wasn't this done before.
The original code always used a direct |
What's the motivation to change it if not for performance purposes? Every change comes with some risk, and we especially shouldn't be changing code because we don't understand what it does.
That's a good indication that switching back is probably safe, but not a guarantee. cc @cherrythecool |
|
Compiled a build using For testing, I decided to import hundreds of WAV files(a total of 2.3GiB, the biggest WAV file having 88.7 MiB):
In the end, it trades memory usage for higher import times. Although if we enabled import options like downsampling to mono or decreasing the mix rate, memory usage would be much higher while import times would be closer to each other. I might do more benchmarks. |
sorry for not explaining back then, but if i recall correctly, i used |
|
After a few more benchmarks, I realized reading from file is significantly slower due to If reading directly from file is desired nonetheless I know two ways of speeding it up:
With |
I managed to get this one working. Instead of making it read frame by frame from a On my tests, there is little difference in time between loading from buffer and from file. Actually, from file seems to be slightly faster. |
|
Redid the benchmarks after the change, this time checking only the time comparison between from buffer and from file:
Skipped memory checks as I don't believe there would be a difference between the previous benchmark. But basically, the difference in time is negligible. |
7f709a5 to
26d6a15
Compare
Partly inspired by #96545 (this PR began as a split from that one before reducing its scope to the current state)
#93831 repurposed WAV importing code so it could be used to load WAV files.
It had a side effect though: all WAV loading, including importing, loads the entire file data into memory to read, even if you use
load_from_file:I did some changes to separate the load procedure from
load_from_buffer, soload_from_filecould read from aFileAccessagain. It's now something like this:The current code reads the file frame-by-frame, which is significantly slower when reading directly from file, so I borrowed a technique from
dr_wavwhere it does so in chunks of 4KiB, which maintains the current read time.By benchmarks below, this should minimize memory usage while keeping load times around the same.