ev3dev / ev3dev-lang

(deprecated) language bindings for ev3dev sensors, motors, LEDs, etc.
GNU General Public License v2.0
56 stars 39 forks source link

Added LRU cache of fstream objects. #25

Closed dsharlet closed 9 years ago

dsharlet commented 9 years ago

Hi ev3dev, first of all, thanks for putting together an awesome set of tools. This is exactly what I was hoping to find!

This PR implements a small cache of file handles for the C++ language bindings. This is discussed in issue #12. This is important to me because I'm trying to build some pretty stiff controllers. In this situation, the lower the latency (and higher the sample rate) the better.

I think that the cache should hopefully alleviate the concerns mentioned in discussion on #12 regarding caching file handles consuming too many OS resources. The global LRU cache guarantees that there will only be < FILE_CACHE_SIZE file handles open at once. I think that this PR should basically make the cases that care about performance faster (reading/writing a few attributes very frequently in a tight loop) while keeping similar performance/resource consumption for other programs.

I benchmarked a program that reads the same attribute repeatedly in a tight loop:

Uncached:

real    0m16.823s
user    0m5.850s
sys     0m9.750s

Cached:

real    0m2.842s
user    0m1.610s
sys     0m1.100s