Skip to content
Snippets Groups Projects
Commit c5bad7b5 authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

fix: use hive.go typeutils

parent 81a60105
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ package datastructure
import (
"sync"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/typeutils"
)
type lruCacheElement struct {
......
......@@ -3,7 +3,7 @@ package filter
import (
"sync"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/typeutils"
)
type ByteArrayFilter struct {
......
......@@ -6,7 +6,7 @@ import (
"sync"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
......@@ -7,8 +7,8 @@ import (
"unsafe"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/bitmask"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
......@@ -5,8 +5,8 @@ import (
"time"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/bitmask"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
package typeutils
import (
"unsafe"
)
// Checks whether an interface is nil or has the value nil.
func IsInterfaceNil(param interface{}) bool {
return param == nil || (*[2]uintptr)(unsafe.Pointer(&param))[1] == 0
}
package typeutils
import (
"reflect"
"runtime"
"unsafe"
)
// Converts a slice of bytes into a string without performing a copy.
// NOTE: This is an unsafe operation and may lead to problems if the bytes
// passed as argument are changed while the string is used. No checking whether
// bytes are valid UTF-8 data is performed.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// Converts a string into a slice of bytes without performing a copy.
// NOTE: This is an unsafe operation and may lead to problems if the bytes are changed.
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
b := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}))
// ensure the underlying string doesn't get GC'ed before the assignment happens
runtime.KeepAlive(&s)
return b
}
package typeutils
import (
"bytes"
"strings"
"testing"
)
var testStrings = []string{
"",
" ",
"test",
"こんにちは、 世界",
strings.Repeat(" ", 10),
strings.Repeat(" ", 100),
strings.Repeat(" ", 10000),
strings.Repeat(" ", 1000000),
}
func TestBytesToString(t *testing.T) {
for _, expected := range testStrings {
arg := []byte(expected)
actual := BytesToString(arg)
if actual != expected {
t.Errorf("BytesToString(%q) = %q but expected %q", arg, actual, expected)
}
}
}
func TestStringToBytes(t *testing.T) {
for _, arg := range testStrings {
expected := []byte(arg)
actual := StringToBytes(arg)
if !bytes.Equal(actual, expected) {
t.Errorf("Bytes(%q) = %q but expected %q", arg, actual, expected)
}
}
}
func TestNil(t *testing.T) {
actual := BytesToString(nil)
expected := ""
if actual != expected {
t.Errorf("String(nil) = %q but expected %q", actual, expected)
}
}
func createTestBytes() [][]byte {
result := make([][]byte, len(testStrings))
for i, str := range testStrings {
result[i] = []byte(str)
}
return result
}
func BenchmarkNativeBytesToString(b *testing.B) {
testBytes := createTestBytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, bs := range testBytes {
_ = string(bs)
}
}
}
func BenchmarkUnsafeBytesToString(b *testing.B) {
testBytes := createTestBytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, bs := range testBytes {
_ = BytesToString(bs)
}
}
}
func BenchmarkNativeStringToBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, str := range testStrings {
_ = []byte(str)
}
}
}
func BenchmarkUnsafeStringToBytes(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, str := range testStrings {
_ = StringToBytes(str)
}
}
}
......@@ -10,11 +10,11 @@ import (
"github.com/iotaledger/goshimmer/packages/errors"
gp "github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/goshimmer/packages/gossip/server"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/goshimmer/plugins/autopeering/local"
"github.com/iotaledger/goshimmer/plugins/tangle"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/parameter"
"github.com/iotaledger/hive.go/typeutils"
)
var (
......
......@@ -5,12 +5,12 @@ import (
"github.com/iotaledger/goshimmer/packages/autopeering/selection"
"github.com/iotaledger/goshimmer/packages/gossip"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/goshimmer/plugins/tangle"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/typeutils"
)
const (
......
......@@ -5,8 +5,8 @@ import (
"github.com/iotaledger/goshimmer/packages/datastructure"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/model/approvers"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
......@@ -5,8 +5,8 @@ import (
"github.com/iotaledger/goshimmer/packages/datastructure"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/model/bundle"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
......@@ -5,8 +5,8 @@ import (
"github.com/iotaledger/goshimmer/packages/datastructure"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/model/value_transaction"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
......@@ -5,8 +5,8 @@ import (
"github.com/iotaledger/goshimmer/packages/datastructure"
"github.com/iotaledger/goshimmer/packages/errors"
"github.com/iotaledger/goshimmer/packages/model/transactionmetadata"
"github.com/iotaledger/goshimmer/packages/typeutils"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/typeutils"
"github.com/iotaledger/iota.go/trinary"
)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment