From 21a8cb75b0a131efa7ab0cfd02d3669fb2e09c34 Mon Sep 17 00:00:00 2001 From: Lars Karlslund Date: Wed, 2 Nov 2022 15:48:41 +0100 Subject: [PATCH] Switched sort and search algorithm to external generic library --- go.mod | 2 ++ go.sum | 4 ++++ modules/engine/edgeconnplus.go | 26 ++++++++++++++++---------- modules/engine/object.go | 9 --------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index f06deaa..85a7fd4 100644 --- a/go.mod +++ b/go.mod @@ -123,8 +123,10 @@ require ( github.com/open-networks/go-msgraph v0.3.1 // indirect github.com/open2b/scriggo v0.56.1 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect + github.com/peterrk/slices v0.0.0-20220825100938-df6f4b667142 // indirect github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/tealeg/xlsx v1.0.5 // indirect + golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f // indirect golang.org/x/mod v0.6.0 // indirect golang.org/x/tools v0.2.0 // indirect gopkg.in/gcfg.v1 v1.2.3 // indirect diff --git a/go.sum b/go.sum index 8b15d7f..8c8f264 100644 --- a/go.sum +++ b/go.sum @@ -412,6 +412,8 @@ github.com/open2b/scriggo v0.56.1/go.mod h1:FJS0k7CaKq2sNlrqAGMwU4dCltYqC1c+Eak3 github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg= github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas= +github.com/peterrk/slices v0.0.0-20220825100938-df6f4b667142 h1:8SOzwY384RK5VRDYrMTEt5jAnf4Lodud6M6d3Zq7QJA= +github.com/peterrk/slices v0.0.0-20220825100938-df6f4b667142/go.mod h1:emTQwVvDK3Bt0uFxjHKlQ+T5eo2oBYbCsYH077YMtQ8= github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ= github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pierrec/lz4 v2.2.6+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= @@ -554,6 +556,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f h1:Al51T6tzvuh3oiwX11vex3QgJ2XTedFPGmbEVh8cdoc= +golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/modules/engine/edgeconnplus.go b/modules/engine/edgeconnplus.go index b168884..c488d67 100644 --- a/modules/engine/edgeconnplus.go +++ b/modules/engine/edgeconnplus.go @@ -1,12 +1,12 @@ package engine import ( - "sort" "sync" "sync/atomic" "unsafe" "github.com/lkarlslund/adalanche/modules/ui" + "github.com/peterrk/slices" ) type EdgeConnectionsPlus struct { @@ -29,9 +29,13 @@ type Connection struct { edges EdgeBitmap } -func init() { - // sorty.MaxGor = 1 -} +var ( + connectionSorter = slices.Order[Connection]{ + RefLess: func(a, b *Connection) bool { + return uintptr(unsafe.Pointer(a.target)) < uintptr(unsafe.Pointer(b.target)) + }, + } +) func (e *EdgeConnectionsPlus) init() { @@ -66,11 +70,12 @@ func (e *EdgeConnectionsPlus) search(wantedKey *Object) *Connection { if backing != nil { atomic.AddUint32(&backing.lookups, 1) - uintWantedKey := uintptr(unsafe.Pointer(wantedKey)) - n, found := sort.Find(int(backing.maxClean), func(i int) int { - foundKey := uintptr(unsafe.Pointer(backing.data[i].target)) - return int(uintWantedKey - foundKey) - }) + // uintWantedKey := uintptr(unsafe.Pointer(wantedKey)) + // n, found := sort.Find(int(backing.maxClean), func(i int) int { + // foundKey := uintptr(unsafe.Pointer(backing.data[i].target)) + // return int(uintWantedKey - foundKey) + // }) + n, found := connectionSorter.BinarySearch(backing.data[:backing.maxClean], Connection{target: wantedKey}) if found { return &backing.data[n] } @@ -336,7 +341,8 @@ func (e *EdgeConnectionsPlus) maintainBacking(requestedModification sizeModifier // Sort the new items insertedData := newData[insertStart:insertEnd] - sort.Sort(ConnectionSliceSorter(insertedData)) + connectionSorter.Sort(insertedData) + // sort.Sort(ConnectionSliceSorter(insertedData)) // sorty.Sort(len(insertedData), func(i, k, r, s int) bool { // if uintptr(unsafe.Pointer(insertedData[i].target)) < uintptr(unsafe.Pointer(insertedData[k].target)) { // if r != s { diff --git a/modules/engine/object.go b/modules/engine/object.go index 2c69fea..2e9c037 100644 --- a/modules/engine/object.go +++ b/modules/engine/object.go @@ -163,15 +163,6 @@ func (target *Object) AbsorbEx(source *Object, fast bool) { // fmt.Println("----------------------------------------") absorbCriticalSection.Lock() source.edges[Out].Range(func(outgoingTarget *Object, edges EdgeBitmap) bool { - if !outgoingTarget.IsValid() { - panic("This is bad") - } - if source == outgoingTarget { - panic("Pointing at myself") - } - - // fmt.Println(unsafe.Pointer(outgoingTarget)) - // Load edges from target, and merge with source edges target.edges[Out].setEdges(outgoingTarget, edges) source.edges[Out].del(outgoingTarget)