I have working code which I’m trying to reduce its memory use.
I have a primes
slice that I pass to a go function
to do parallel processing, which can be very large (up to 100s of millions of primes). It’s read only after its created, and not changed.
The go functions
eats up allot of memory compared to the Rust version of the same program, as it only passes primes
by reference vs value (I originally passed copies of the primes array until I figured out how to do it by reference, and memory use dropped significantly).
The Go
version memory use behaves like Rust
's before making the change so I’ve looked at the docs and tried to pass primes
by reference according to this doc:
Here’s a snippet of the original code.
var wg sync.WaitGroup
for i, r_hi := range rescousins {
wg.Add(1)
go func(i, r_hi int) {
defer wg.Done()
l, c := cousins_sieve(r_hi, kmin, kmax, kb, start_num, end_num, modpg, primes, resinvrs)
lastcousins[i] = l; ■■■■[i] = c
fmt.Printf("\r%d of %d cousinpairs done", (i + 1), pairscnt)
}(i, r_hi)
}
wg.Wait()
And here’s the modded version to pass primes
by reference.
refprimes := &primes
var wg sync.WaitGroup
for i, r_hi := range rescousins {
wg.Add(1)
go func(i, r_hi int) {
defer wg.Done()
l, c := cousins_sieve(r_hi, kmin, kmax, kb, start_num, end_num, modpg, *refprimes, resinvrs)
lastcousins[i] = l; ■■■■[i] = c
fmt.Printf("\r%d of %d cousinpairs done", (i + 1), pairscnt)
}(i, r_hi)
}
wg.Wait()
In both cases it seems primes
data is still being copied into each thread to use.
Can this be written so that isn’t the case, so primes
data can be shared and not have to be copied to use?