Idea for speeding up digitalRead, digitalWrite, etc...

Hi all,

As you know, there are times when you need all the speed you can get and the digitalRead/Write functions are slow because they do "pin number to port" and "pin number to bit" translation, testing for timers on pins, etc... ugh slow as molasses.

I have been thinking about adding an option to the "wiring_digital" library code which (by calling a function) would enable or disable a buffer to hold port translation data in SRAM.

The idea would be something like:

digitalFastmode (ON); or digitalFastmode (OFF);

When turned on, an appropriate amount of buffer would be malloc'd and then all the port and pin translations would be run and placed in the buffer and a "fastmode flag" would be set.

After that, digitalRead and digitalWrite would use fast "pre-looked-up" port and bit numbers rather than needing to grab them all from PROGMEM each time.

To turn off the mode would simply involve doing a "free()" of the buffer and clearing the "fastmode" flag.

This is, of course, trading ram for speed when the user desires (or needs) to do so and doesn't want to bother figuring out "PORT", "PIN", "DDR" and "_BV()" stuff.

My questions are:

  • Is this something users would want/like/need?
  • Has this already been done? (no need to re-invent the wheel).

Input will be appreciated. Thanks!

-- Roger

There exist several fast digital write implementation, there is even one that optimizes compile time if the pin number is fixed (IIRC it was created by Paul Stoffregen)

I wrote a FastShiftIn Class that optimizes the pin "math" - Arduino/libraries/FastShiftIn at master · RobTillaart/Arduino · GitHub -
related discussion here - FastShiftOut with Print interface (experimental) - Libraries - Arduino Forum -

Wrt your idea, I think everyone would have the optimized version always on as there is no reason to use the slow one.

robtillaart:
There exist several fast digital write implementation, there is even one that optimizes compile time if the pin number is fixed (IIRC it was created by Paul Stoffregen)

I wrote a FastShiftIn Class that optimizes the pin "math" - Arduino/libraries/FastShiftIn at master · RobTillaart/Arduino · GitHub -
related discussion here - FastShiftOut with Print interface (experimental) - Libraries - Arduino Forum -

Wrt your idea, I think everyone would have the optimized version always on as there is no reason to use the slow one.

The optimized version would use ram to hold pre-read port and pin data to be available for instant use (as opposed to having to look it up by reading PROGMEM each time a digitalWrite or digitalRead is used).

So there would be a reason for a person to use one over the other. If you need all the RAM you can get, you turn off fast mode and return (free()) that buffer to the pool.

If you need speed and have enough ram, you malloc a block of ram to the lookup table. You get faster performance, but less ram.

It all depends of the person's application, how much ram they have and how much they need.