Skip to content

Commit

Permalink
implemented smart lock mechanism in All function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Koss committed Jan 27, 2024
1 parent e757bcd commit 8ae2ca9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
32 changes: 31 additions & 1 deletion internal/internalpipe/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internalpipe

import (
"sync"
"time"
)

const hugeLenStep = 1 << 15
Expand All @@ -20,6 +21,8 @@ func anySingleThread[T any](limit int, fn GeneratorFn[T]) *T {

// Any returns a pointer to a random element in the pipe or nil if none left.
func (p Pipe[T]) Any() *T {
const mutexUpdateCoef = 18

limit := p.limit()
if p.GoroutinesCnt == 1 {
return anySingleThread(limit, p.Fn)
Expand Down Expand Up @@ -66,16 +69,43 @@ func (p Pipe[T]) Any() *T {
rg = min(rg, limit)
}

for j := lf; j < rg; j++ {
var avgFnTime time.Duration
var avgUpdResSetTime time.Duration
resSetUpdCnt := int64(0)
beforeLastResSetUpd := 0

getResSet := func() bool {
start := time.Now()
mx.Lock()
rs := resSet
mx.Unlock()
avgUpdResSetTime = time.Duration(
(int64(time.Since(start)) + int64(avgUpdResSetTime)*(resSetUpdCnt)) / (resSetUpdCnt + 1),
)
resSetUpdCnt++
beforeLastResSetUpd = 0
return rs
}
rs := getResSet()
cnt := 0
for j := lf; j < rg; j++ {
beforeLastResSetUpd++
if j != lf &&
avgFnTime != 0 &&
int64(beforeLastResSetUpd) > (mutexUpdateCoef*int64(avgUpdResSetTime)/int64(avgFnTime)) {
rs = getResSet()
cnt++
}
if !rs {
start := time.Now()
obj, skipped := p.Fn(j)
if !skipped {
setObj(obj)
return
}
avgFnTime = time.Duration(
(int64(time.Since(start)) + int64(avgFnTime)*int64(j-lf)) / int64(j-lf+1),
)
}
}
}(i, i+step)
Expand Down
5 changes: 3 additions & 2 deletions perf/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/koss-null/funcfrog/pkg/pipe"
)

func fib(n int) int {
n = n % 91
func fib(_ int) int {
// about 100 operations to get 91th fib number
n := 100 // 100 iters
a, b := 0, 1
for i := 0; i < n; i++ {
a, b = b, a+b
Expand Down

0 comments on commit 8ae2ca9

Please sign in to comment.