Skip to content

Commit 714f193

Browse files
committed
Optimize ESP8266 miner
This commit changes how the sequence of strings representing consecutive integers is generated for the hasher. Instead of utilizing the Arduino String conversions, a Counter class is introduced, which can generate consecutive integers much faster. On my Wemos D1 mini this gives a hash rate increase from around 37 kH/s to around 53 kH/s, which is a 43% improvement.
1 parent 9e608f2 commit 714f193

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

ESP8266_Code/ESP8266_Code.ino

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,49 @@ unsigned long lwdTimeOutMillis = LWD_TIMEOUT;
427427
#define BLINK_CLIENT_CONNECT 3
428428
#define BLINK_RESET_DEVICE 5
429429

430+
template <unsigned int max_digits>
431+
class Counter {
432+
public:
433+
Counter() { reset(); }
434+
435+
void reset() {
436+
memset(buffer, '0', max_digits);
437+
buffer[max_digits] = '\0';
438+
val = 0;
439+
len = 1;
440+
}
441+
442+
Counter & operator++() {
443+
inc_string(max_digits - 1);
444+
++val;
445+
return *this;
446+
}
447+
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; }
451+
452+
protected:
453+
inline void inc_string(int pos) {
454+
if (pos < 0)
455+
return;
456+
457+
if (buffer[pos] < '9') {
458+
buffer[pos]++;
459+
} else {
460+
buffer[pos] = '0';
461+
inc_string(pos - 1);
462+
}
463+
464+
len = max(max_digits - pos, len);
465+
}
466+
467+
protected:
468+
char buffer[max_digits + 1];
469+
unsigned int val;
470+
size_t len;
471+
};
472+
430473
void SetupWifi() {
431474
Serial.println("Connecting to: " + String(SSID));
432475
WiFi.mode(WIFI_STA); // Setup ESP in client mode
@@ -664,8 +707,7 @@ void setup() {
664707
void loop() {
665708
br_sha1_context sha1_ctx, sha1_ctx_base;
666709
uint8_t hashArray[20];
667-
String duco_numeric_result_str;
668-
710+
669711
// 1 minute watchdog
670712
lwdtFeed();
671713

@@ -735,22 +777,21 @@ void loop() {
735777

736778
String result = "";
737779
if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW);
738-
for (unsigned int duco_numeric_result = 0; duco_numeric_result < difficulty; duco_numeric_result++) {
780+
for (Counter<8> counter; counter < difficulty; ++counter) {
739781
// Difficulty loop
740782
sha1_ctx = sha1_ctx_base;
741-
duco_numeric_result_str = String(duco_numeric_result);
742783

743-
br_sha1_update(&sha1_ctx, duco_numeric_result_str.c_str(), duco_numeric_result_str.length());
784+
br_sha1_update(&sha1_ctx, counter.c_str(), counter.strlen());
744785
br_sha1_out(&sha1_ctx, hashArray);
745786

746787
if (memcmp(expected_hash, hashArray, 20) == 0) {
747788
// If result is found
748789
if (LED_BLINKING) digitalWrite(LED_BUILTIN, HIGH);
749790
unsigned long elapsed_time = micros() - start_time;
750791
float elapsed_time_s = elapsed_time * .000001f;
751-
hashrate = duco_numeric_result / elapsed_time_s;
792+
hashrate = counter / elapsed_time_s;
752793
share_count++;
753-
client.print(String(duco_numeric_result)
794+
client.print(String(counter)
754795
+ ","
755796
+ String(hashrate)
756797
+ ","
@@ -767,7 +808,7 @@ void loop() {
767808
Serial.println(client_buffer
768809
+ " share #"
769810
+ String(share_count)
770-
+ " (" + String(duco_numeric_result) + ")"
811+
+ " (" + String(counter) + ")"
771812
+ " hashrate: "
772813
+ String(hashrate / 1000, 2)
773814
+ " kH/s ("

0 commit comments

Comments
 (0)