Skip to content

Commit cb19c25

Browse files
committed
fixup! Optimize ESP8266 miner
1 parent 78ce242 commit cb19c25

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

ESP8266_Code/ESP8266_Code.ino

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -439,29 +439,30 @@ public:
439439
len = 1;
440440
}
441441

442-
Counter & operator++() {
443-
inc_string(max_digits - 1);
442+
inline Counter & operator++() {
443+
inc_string(buffer + max_digits - 1);
444444
++val;
445445
return *this;
446446
}
447447

448-
operator unsigned int () const { return val; }
449-
const char * c_str() const { return buffer + max_digits - len; }
450-
size_t strlen() const { return len; }
448+
inline operator unsigned int () const { return val; }
449+
inline const char * c_str() const { return buffer + max_digits - len; }
450+
inline size_t strlen() const { return len; }
451451

452452
protected:
453-
inline void inc_string(int pos) {
454-
if (pos < 0)
455-
return;
456-
457-
if (buffer[pos] < '9') {
458-
buffer[pos]++;
453+
inline void inc_string(char * c) {
454+
// In theory, the line below should be uncommented to avoid writing outside the buffer. In practice however,
455+
// with max_digits set to 10 or more, we can fit all possible unsigned 32-bit integers in the buffer. The
456+
// check is skipped to gain a small extra speed improvement.
457+
// if (c >= buffer) return;
458+
459+
if (*c < '9') {
460+
*c += 1;
459461
} else {
460-
buffer[pos] = '0';
461-
inc_string(pos - 1);
462+
*c = '0';
463+
inc_string(c - 1);
464+
len = max(max_digits - (c - buffer) + 1, len);
462465
}
463-
464-
len = max(max_digits - pos, len);
465466
}
466467

467468
protected:
@@ -772,12 +773,12 @@ void loop() {
772773
br_sha1_init(&sha1_ctx_base);
773774
br_sha1_update(&sha1_ctx_base, last_block_hash.c_str(), last_block_hash.length());
774775

775-
float start_time = micros();
776+
unsigned long start_time = micros();
776777
max_micros_elapsed(start_time, 0);
777778

778779
String result = "";
779780
if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW);
780-
for (Counter<8> counter; counter < difficulty; ++counter) {
781+
for (Counter<10> counter; counter < difficulty; ++counter) {
781782
// Difficulty loop
782783
sha1_ctx = sha1_ctx_base;
783784

0 commit comments

Comments
 (0)