Skip to content

Commit e455c9c

Browse files
committed
fixup! Optimize ESP8266 miner
1 parent 938a8d4 commit e455c9c

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:
@@ -796,12 +797,12 @@ void loop() {
796797
br_sha1_init(&sha1_ctx_base);
797798
br_sha1_update(&sha1_ctx_base, job.last_block_hash.c_str(), job.last_block_hash.length());
798799

799-
float start_time = micros();
800+
unsigned long start_time = micros();
800801
max_micros_elapsed(start_time, 0);
801802

802803
String result = "";
803804
if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW);
804-
for (Counter<8> counter; counter < difficulty; ++counter) {
805+
for (Counter<10> counter; counter < difficulty; ++counter) {
805806
// Difficulty loop
806807
sha1_ctx = sha1_ctx_base;
807808

0 commit comments

Comments
 (0)