From: sgf Date: Wed, 26 Oct 2022 17:40:39 +0000 (+0300) Subject: chg(balancer): Use more pointers to avoid copying data. X-Git-Url: https://gitweb.sgf-dma.tk/?a=commitdiff_plain;h=d9285ec238e2c1cea4e262f3346f53b557f5bed0;p=go.git chg(balancer): Use more pointers to avoid copying data. --- diff --git a/concurrency-is-not-parallelism/balancer.go b/concurrency-is-not-parallelism/balancer.go index 2cae58c..d39d69e 100644 --- a/concurrency-is-not-parallelism/balancer.go +++ b/concurrency-is-not-parallelism/balancer.go @@ -18,12 +18,12 @@ func (r Request) String() string { return fmt.Sprintf("R %.2d", r.num) } -func workFn(r Request) int { +func workFn(r *Request) int { fmt.Printf("%v: computing (workFn).. (%p)\n", r, r.fn) return r.num } -func furtherProcess(r Request, result int) { +func furtherProcess(r *Request, result int) { fmt.Printf("%v: further processing result %v..\n", r, result) } @@ -36,13 +36,13 @@ func requester(work chan<- Request) { time.Sleep(time.Duration(t) * time.Second) req := &Request{num: reqNum, c: c} - fn := func () int { return workFn(*req) } + fn := func () int { return workFn(req) } req.fn = fn fmt.Printf("requester() '%v': sending\n", req) work <- *req result := <-c fmt.Printf("requester() '%v': received result %v\n", req, result) - furtherProcess(*req, result) + furtherProcess(req, result) reqNum++ } } @@ -59,7 +59,7 @@ type Worker struct { type Done struct { w *Worker - r Request + r *Request } func (w Worker) String() string { @@ -74,7 +74,7 @@ func (w *Worker) work(done chan<- *Done) { req := <- w.requests fmt.Printf("%v: start computing '%v'\n", w, req) req.c <- req.fn() - done <- &Done{w, req} + done <- &Done{w, &req} fmt.Printf("%v: finished '%v'\n", w, req) } } @@ -143,8 +143,8 @@ func (b *Balancer) dispatch(req Request) { } func (b *Balancer) completed(done *Done) { - done.w.pending-- fmt.Printf("completed() '%v': finished '%v'\n", done.w, done.r) + done.w.pending-- heap.Remove(&b.pool, done.w.index) heap.Push(&b.pool, done.w) }