Skip to content

Commit

Permalink
Memoize the soonest job.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Jun 12, 2008
1 parent 8b0e9bd commit 9856ada
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ make_conn(int fd, char start_state, tube use, tube watch)
c->type = 0;
c->cmd_read = 0;
c->pending_timeout = -1;
c->soonest_job = NULL;
c->in_job = c->out_job = NULL;
c->in_job_read = c->out_job_sent = 0;
c->prev = c->next = c; /* must be out of a linked list right now */
Expand Down Expand Up @@ -220,11 +221,15 @@ conn_insert(conn head, conn c)
job
soonest_job(conn c)
{
job j, soonest = NULL;
job j = NULL;
job soonest = c->soonest_job;

for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
if (soonest == NULL) {
for (j = c->reserved_jobs.next; j != &c->reserved_jobs; j = j->next) {
if (j->deadline <= (soonest ? : j)->deadline) soonest = j;
}
}
c->soonest_job = soonest;
return soonest;
}

Expand Down
3 changes: 3 additions & 0 deletions conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ struct conn {
/* A job to be read from the client. */
job in_job;

/* Memoization of the soonest job */
job soonest_job;

/* How many bytes of in_job->body have been read so far. If in_job is NULL
* while in_job_read is nonzero, we are in bit bucket mode and
* in_job_read's meaning is inverted -- then it counts the bytes that
Expand Down
9 changes: 9 additions & 0 deletions prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ reserve_job(conn c, job j)
conn_insert(&running, c);
j->state = JOB_STATE_RESERVED;
job_insert(&c->reserved_jobs, j);
if (c->soonest_job == NULL) {
c->soonest_job = j;
} else {
if (j->deadline < c->soonest_job->deadline) {
c->soonest_job = j;
}
}
return reply_job(c, j, MSG_RESERVED);
}

Expand Down Expand Up @@ -455,6 +462,7 @@ enqueue_reserved_jobs(conn c)
if (!r) bury_job(j);
global_stat.reserved_ct--;
j->tube->stat.reserved_ct--;
c->soonest_job = NULL;
if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
}
}
Expand Down Expand Up @@ -1019,6 +1027,7 @@ remove_this_reserved_job(conn c, job j)
global_stat.reserved_ct--;
j->tube->stat.reserved_ct--;
}
c->soonest_job = NULL;
if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
return j;
}
Expand Down

0 comments on commit 9856ada

Please sign in to comment.