Faster method of accessing an array

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
liak
Registered User
Registered User
Posts: 195
Joined: Fri Oct 05, 2007 12:26 am

Faster method of accessing an array

Post by liak » Sun Feb 15, 2009 7:42 am

Dear all,

I have been trying to increase the efficiency of my MCU by optimizing my code. I have changed all my variables in situations that I can to type byte. Now, I am facing another bottleneck in this section.

I have a variable declared in a structured array:

Code: Select all

Structure TFile
   Data(250) As Byte
End Structure

Dim A as TFile
I noted that accessing an array is much much slower than accessing a variable directly, though I didn't look into the asm. In the same way also, I have noticed that accessing a variable by the following method is also significantly slower eventhough possible in SF:

Code: Select all

Dim B as byte
Dim C as bit

C = B.bits(Index)
In reference, I have seen the SDFileSystem module by Steven and noticed that he used indirect addressing method to access his array structure in the following segment, see sub ReadSector():

Code: Select all

 If SeekResponse($FE) = $FE Then         // Read start token
            Index = 0
            If pBuffer Then
               FSR0 = @Shared.CurrentSectorBuffer
               Repeat                            // Read data block
#if SD_SPI = SW                                  // Software SPI version - use SendByte
                  POSTINC0 = ReceiveByte()
#elseif SD_SPI = MSSP                            // MSSP version - use inline routine to optimise speed
                  SSPIF = 0 
                  SSPBuffer = $FF 
                  Repeat 
                     ClrWDT 
                  Until SSPIF = 1 
                  POSTINC0 = SSPBuffer
#endif
                  Inc(Index)
               Until Index = $200
            Else
Questions:
1. I am not sure if we can use this sort of indirect addressing on my array variable, or will it speed it up.

2. Any issues to expect if we use FSR directly? I have noticed that Steven had a thread with David on this earlier.

3. Can anyone enlighten me more on the specific function of FSR and its related registers: POSTINC, POSTDEC, INDIF and PREINC. I am not very sure if I have understood them well enough, eventhough I have read the datasheet a few times.


Thanks

Regards,
Liak

Post Reply