mirror of
				https://github.com/eledio-devices/thirdparty-littlefs.git
				synced 2025-10-31 00:32:38 +01:00 
			
		
		
		
	Modified robekras's optimization to avoid flush for all seeks in cache
The basic idea is simple, if we seek to a position in the currently loaded cache, don't flush the cache. Notably this ensures that seek is always as fast or faster than just reading the data. This is a bit tricky since we need to check that our new block and offset match the cache, fortunately we can skip the block check by reevaluating the block index for both the current and new positions. Note this only works whene reading, for writing we need to always flush the cache, or else we will lose the pending write data.
This commit is contained in:
		
							
								
								
									
										28
									
								
								lfs.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								lfs.c
									
									
									
									
									
								
							| @@ -3091,17 +3091,23 @@ static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file, | |||||||
|         return npos; |         return npos; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Get the difference between new and current file position |     // if we're only reading and our new offset is still in the file's cache | ||||||
|     // If new position is after the current file position |     // we can avoid flushing and needing to reread the data | ||||||
|     // If new position belongs to the currently cached data |     if ( | ||||||
|     // Set new file position, | #ifndef LFS_READONLY | ||||||
|     // and update also the block related position |         !(file->flags & LFS_F_WRITING) | ||||||
|     int offset = npos - file->pos; | #else | ||||||
|     if (offset > 0) { |         true | ||||||
|         if ((file->off + offset) < lfs->cfg->block_size) { | #endif | ||||||
|             file->pos  = npos; |             ) { | ||||||
|             file->off += offset; |         int oindex = lfs_ctz_index(lfs, &(lfs_off_t){file->pos}); | ||||||
|  |         lfs_off_t noff = npos; | ||||||
|  |         int nindex = lfs_ctz_index(lfs, &noff); | ||||||
|  |         if (oindex == nindex | ||||||
|  |                 && noff >= file->cache.off | ||||||
|  |                 && noff < file->cache.off + file->cache.size) { | ||||||
|  |             file->pos = npos; | ||||||
|  |             file->off = noff; | ||||||
|             return npos; |             return npos; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user