github-vet / github-vet/rangeloop-pointer-findings

ypujante/ray-tracing: scene.go; 61 LoC

Open
#7,532 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [ypujante/ray-tracing](https://www.github.com/ypujante/ray-tracing) at [scene.go](https://github.com/ypujante/ray-tracing/blob/e5613b8a19dd7abf9eaa61dfc5dba087f3b5b5cc/scene.go#L107-L167)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable rpp used in defer or goroutine at line 147

[Click here to see the code in its original context.](https://github.com/ypujante/ray-tracing/blob/e5613b8a19dd7abf9eaa61dfc5dba087f3b5b5cc/scene.go#L107-L167)

Click here to show the 61 line(s) of Go which triggered the analyzer.

```go
for _, rpp := range scene.raysPerPixel {

loopStart := time.Now()

// creates a channel which will be used to dispatch the line to process to each go routine
pixelsToProcess := make(chan []*pixel)

// asynchronously dispatch the lines to process
go func() {
for _, p := range lines {
pixelsToProcess <- p
}
// done... signal the end
close(pixelsToProcess)
}()

// create a wait group to wait until all goroutine completes
wg := sync.WaitGroup{}

// create parallelCount goroutines
for c := 0; c < parallelCount; c++ {
wg.Add(1)
go func() {
// due to high contention on global rand, each goroutine uses its own random number generator
// thus avoiding massive slowdown
rnd := rand.New(rand.NewSource(rand.Int63()))

// process a bunch of pixels (in this case a line)
for ps := range pixelsToProcess {

// redisplay the line without gamma correction => make it darker to be more visible
for _, p := range ps {
if p.raysPerPixel > 0 {
col := p.color.Scale(1.0 / float64(p.raysPerPixel))
pixels[p.k] = col.PixelValue()
}
}

// render every pixel in the line
for _, p := range ps {
pixels[p.k] = scene.render(rnd, p, rpp)
}
}
wg.Done()
}()
}

// wait for the pass to be completed
wg.Wait()

// compute stats for the pass
accumulatedRaysPerPixel += rpp

loopEnd := time.Now()

totalTimeSoFar := loopEnd.Sub(totalStart)
estimatedTotalTime := time.Duration(float64(totalTimeSoFar) * float64(totalRaysPerPixel) / float64(accumulatedRaysPerPixel))
erm := estimatedTotalTime - totalTimeSoFar

fmt.Printf("Processed %v rays per pixel in %v. Total %v in %v. ERM %v\n", rpp, time.Now().Sub(loopStart), accumulatedRaysPerPixel, totalTimeSoFar, erm)
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: e5613b8a19dd7abf9eaa61dfc5dba087f3b5b5cc

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.