Browse Source

Allocate work struct once, on stack, rather than alloc+free for each getwork.

nfactor-troky
Jeff Garzik 14 years ago committed by Jeff Garzik
parent
commit
1032fec15b
  1. 2
      Makefile.am
  2. 50
      cpu-miner.c

2
Makefile.am

@ -1,5 +1,5 @@
INCLUDES = -pthread INCLUDES = -pthread -fno-strict-aliasing
bin_PROGRAMS = minerd bin_PROGRAMS = minerd

50
cpu-miner.c

@ -82,11 +82,12 @@ struct upload_buffer {
}; };
struct work { struct work {
unsigned char midstate[32];
unsigned char data[128]; unsigned char data[128];
unsigned char hash[32];
unsigned char hash1[64]; unsigned char hash1[64];
unsigned char midstate[32];
unsigned char target[32]; unsigned char target[32];
unsigned char hash[32];
}; };
static void databuf_free(struct data_buffer *db) static void databuf_free(struct data_buffer *db)
@ -141,8 +142,7 @@ static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
return len; return len;
} }
static json_t *json_rpc_call(const char *url, const char *userpass, static json_t *json_rpc_call(const char *url, const char *rpc_req)
const char *rpc_req)
{ {
CURL *curl; CURL *curl;
json_t *val; json_t *val;
@ -282,22 +282,8 @@ static bool jobj_binary(const json_t *obj, const char *key,
return true; return true;
} }
static void work_free(struct work *work) static bool work_decode(const json_t *val, struct work *work)
{
if (!work)
return;
free(work);
}
static struct work *work_decode(const json_t *val)
{ {
struct work *work;
work = calloc(1, sizeof(*work));
if (!work)
return NULL;
if (!jobj_binary(val, "midstate", if (!jobj_binary(val, "midstate",
work->midstate, sizeof(work->midstate))) { work->midstate, sizeof(work->midstate))) {
fprintf(stderr, "JSON inval midstate\n"); fprintf(stderr, "JSON inval midstate\n");
@ -319,11 +305,12 @@ static struct work *work_decode(const json_t *val)
goto err_out; goto err_out;
} }
return work; memset(work->hash, 0, sizeof(work->hash));
return true;
err_out: err_out:
work_free(work); return false;
return NULL;
} }
static void inc_stats(uint64_t n_hashes) static void inc_stats(uint64_t n_hashes)
@ -415,7 +402,7 @@ static void submit_work(struct work *work)
fprintf(stderr, "DBG: sending RPC call:\n%s", s); fprintf(stderr, "DBG: sending RPC call:\n%s", s);
/* issue JSON-RPC request */ /* issue JSON-RPC request */
val = json_rpc_call(rpc_url, userpass, s); val = json_rpc_call(rpc_url, s);
if (!val) { if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n"); fprintf(stderr, "submit_work json_rpc_call failed\n");
goto out; goto out;
@ -440,19 +427,20 @@ static void *miner_thread(void *dummy)
while (1) { while (1) {
json_t *val; json_t *val;
struct work *work; struct work work __attribute__((aligned(128)));
uint32_t nonce; uint32_t nonce;
bool rc;
/* obtain new work from bitcoin */ /* obtain new work from bitcoin */
val = json_rpc_call(rpc_url, userpass, rpc_req); val = json_rpc_call(rpc_url, rpc_req);
if (!val) { if (!val) {
fprintf(stderr, "json_rpc_call failed\n"); fprintf(stderr, "json_rpc_call failed\n");
return NULL; return NULL;
} }
/* decode result into work state struct */ /* decode result into work state struct */
work = work_decode(json_object_get(val, "result")); rc = work_decode(json_object_get(val, "result"), &work);
if (!work) { if (!rc) {
fprintf(stderr, "work decode failed\n"); fprintf(stderr, "work decode failed\n");
return NULL; return NULL;
} }
@ -460,14 +448,12 @@ static void *miner_thread(void *dummy)
json_decref(val); json_decref(val);
/* scan nonces for a proof-of-work hash */ /* scan nonces for a proof-of-work hash */
nonce = scanhash(work->midstate, work->data + 64, nonce = scanhash(work.midstate, work.data + 64,
work->hash1, work->hash); work.hash1, work.hash);
/* if nonce found, submit work */ /* if nonce found, submit work */
if (nonce) if (nonce)
submit_work(work); submit_work(&work);
work_free(work);
} }
return NULL; return NULL;

Loading…
Cancel
Save