Skip to content

[DEV] Explore reusing buffers in FFT processing to improve performance. #135

@Picorims

Description

@Picorims

See for instance channel separation logic with the creation of a new vector for each channel.

Copilot suggestion (not tested nor verified, possibly wrong or hallucinated):

// Declare reusable channel_samples buffer outside the frame loop  
            static mut CHANNEL_SAMPLES: Option<Vec<Vec<f32>>> = None;  
            let channel_samples = unsafe {  
                CHANNEL_SAMPLES.get_or_insert_with(|| {  
                    let mut v = Vec::with_capacity(track_channels_count as usize);  
                    for _ in 0..track_channels_count {  
                        v.push(Vec::with_capacity(fft_size as usize));  
                    }  
                    v  
                })  
            };  
            // Now channel_samples is a mutable reference to the reusable buffer  
            let samples: &[f32] = &samples_cache  
                [start_index..(start_index + (fft_size as u64 * track_channels_count) as usize)];  
            // Isolate each channel's samples  
            // Reuse channel_samples buffers to avoid repeated allocations  
            if channel_samples.len() != track_channels_count as usize {  
                channel_samples.clear();  
                for _ in 0..track_channels_count {  
                    channel_samples.push(Vec::with_capacity(fft_size as usize));  
                }  
            } else {  
                for buf in &mut channel_samples {  
                    buf.clear();  
                }  
            }  
            for channel in 0..track_channels_count as usize {  
                // Fill each channel's buffer  
                channel_samples[channel].extend(  
                    samples  
                        .iter()  
                        .skip(channel)  
                        .step_by(track_channels_count as usize)  
                        .copied()  
                );  

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestrustPull requests that update rust codetaskall kind of task not being a feature or a bug

Projects

Status

Backlog

Relationships

None yet

Development

No branches or pull requests

Issue actions