Skip to content

Commit 02a5ae9

Browse files
In HardwareSerial, don't use int for buffer indices
The index attributes have been uint8_t for a while, so there is no point in using int for local variables. This should allow the compiler to generate slightly more efficient code, but (at least on gcc 4.8.2) it also confuses the register allocator, causing this change to increase code size by 2 bytes instead due to extra push/pop instructions (but this will probably change in the future if the compiler improves).
1 parent 6c7f786 commit 02a5ae9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Diff for: cores/arduino/HardwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ int HardwareSerial::read(void)
171171
return -1;
172172
} else {
173173
unsigned char c = _rx_buffer[_rx_buffer_tail];
174-
_rx_buffer_tail = (unsigned int)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
174+
_rx_buffer_tail = (uint8_t)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
175175
return c;
176176
}
177177
}
@@ -207,7 +207,7 @@ size_t HardwareSerial::write(uint8_t c)
207207
sbi(*_ucsra, TXC0);
208208
return 1;
209209
}
210-
int i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE;
210+
uint8_t i = (_tx_buffer_head + 1) % SERIAL_BUFFER_SIZE;
211211

212212
// If the output buffer is full, there's nothing for it other than to
213213
// wait for the interrupt handler to empty it a bit

0 commit comments

Comments
 (0)