performance
This commit is contained in:
+9
-5
@@ -53,6 +53,7 @@ type BlockState struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var BlocksCache = make(map[string][]*Block)
|
var BlocksCache = make(map[string][]*Block)
|
||||||
|
var BlocksCacheIdMap = make(map[string]map[int32]*Block)
|
||||||
|
|
||||||
func BlocksForVersion(v string) (ret []*Block) {
|
func BlocksForVersion(v string) (ret []*Block) {
|
||||||
var ok bool
|
var ok bool
|
||||||
@@ -66,13 +67,16 @@ func BlocksForVersion(v string) (ret []*Block) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LookupBlockByStateId(version string, stateId int32) (block *Block, ok bool) {
|
func LookupBlockByStateId(version string, stateId int32) (block *Block, ok bool) {
|
||||||
for _, b := range BlocksForVersion(version) {
|
idmap, found := BlocksCacheIdMap[version]
|
||||||
if b.MinStateId <= stateId && stateId <= b.MaxStateId {
|
if !found {
|
||||||
block = b
|
idmap = make(map[int32]*Block)
|
||||||
ok = true
|
for _, b := range BlocksForVersion(version) {
|
||||||
return
|
for i := range b.MaxStateId - b.MinStateId + 1 {
|
||||||
|
idmap[b.MinStateId+i] = b
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
block, ok = idmap[stateId]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LoopsPerSecond = 100 // i dunno seems reasonable
|
LoopsPerSecond = 10000 // this is just so the cpu doesn't burn itself in an unlimited infinite loop
|
||||||
TimePerLoop = time.Second / LoopsPerSecond
|
TimePerLoop = time.Second / LoopsPerSecond
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/admin-else/strom/data"
|
"github.com/admin-else/strom/data"
|
||||||
"github.com/admin-else/strom/level"
|
"github.com/admin-else/strom/level"
|
||||||
@@ -42,10 +43,9 @@ type Region struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Region) GetBlockAt(x, y, z int32) (name string, properties map[string]string, err error) {
|
func (r *Region) GetBlockAt(x, y, z int32) (name string, properties map[string]string, err error) {
|
||||||
uintSlice := make([]uint64, len(r.BlockStates))
|
|
||||||
for i, v := range r.BlockStates {
|
// this is probably fine
|
||||||
uintSlice[i] = uint64(v)
|
uintSlice := unsafe.Slice((*uint64)(unsafe.Pointer(unsafe.SliceData(r.BlockStates))), len(r.BlockStates))
|
||||||
}
|
|
||||||
if r.Size.X < 0 {
|
if r.Size.X < 0 {
|
||||||
r.Size.X *= -1
|
r.Size.X *= -1
|
||||||
}
|
}
|
||||||
@@ -56,7 +56,7 @@ func (r *Region) GetBlockAt(x, y, z int32) (name string, properties map[string]s
|
|||||||
r.Size.Z *= -1
|
r.Size.Z *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
if x >= r.Size.X || y >= r.Size.Y || z >= r.Size.Z {
|
if x >= r.Size.X || y >= r.Size.Y || z >= r.Size.Z || x < 0 || y < 0 || z < 0 {
|
||||||
err = OutOfBoundsErr
|
err = OutOfBoundsErr
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -2,6 +2,7 @@ package proto
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -221,7 +222,9 @@ func (c *Conn) OnTick(_ event.Tick) (err error) {
|
|||||||
if c.receiveRoutine {
|
if c.receiveRoutine {
|
||||||
select {
|
select {
|
||||||
case packet = <-c.packetCh:
|
case packet = <-c.packetCh:
|
||||||
slog.Debug("receive", "actor", c.Actor, "packet", fmt.Sprintf("%#v", packet))
|
if slog.Default().Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
slog.Debug("receive", "actor", c.Actor, "packet", fmt.Sprintf("%#v", packet))
|
||||||
|
}
|
||||||
err = c.Loop.Fire(packet)
|
err = c.Loop.Fire(packet)
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package util
|
||||||
|
|
||||||
|
import "math/rand/v2"
|
||||||
|
|
||||||
|
func ShuffleSlice[T any](s []T) (ret []T) {
|
||||||
|
rand.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] })
|
||||||
|
return s
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user