From c5bad7b5adac2b99dc410e2b7ca217b8273fcd4f Mon Sep 17 00:00:00 2001 From: Wolfgang Welz <welzwo@gmail.com> Date: Sat, 4 Jan 2020 15:55:20 +0100 Subject: [PATCH] fix: use hive.go typeutils --- packages/datastructure/lru_cache.go | 2 +- packages/filter/byte_array_filter.go | 2 +- packages/model/approvers/approvers.go | 2 +- packages/model/bundle/bundle.go | 2 +- .../transactionmetadata.go | 2 +- packages/typeutils/typeutils.go | 10 -- packages/typeutils/unsafe.go | 30 ------ packages/typeutils/unsafe_test.go | 92 ------------------- plugins/gossip/gossip.go | 2 +- plugins/gossip/plugin.go | 2 +- plugins/tangle/approvers.go | 2 +- plugins/tangle/bundle.go | 2 +- plugins/tangle/transaction.go | 2 +- plugins/tangle/transaction_metadata.go | 2 +- 14 files changed, 11 insertions(+), 143 deletions(-) delete mode 100644 packages/typeutils/typeutils.go delete mode 100644 packages/typeutils/unsafe.go delete mode 100644 packages/typeutils/unsafe_test.go diff --git a/packages/datastructure/lru_cache.go b/packages/datastructure/lru_cache.go index 469e54a9..fe15c424 100644 --- a/packages/datastructure/lru_cache.go +++ b/packages/datastructure/lru_cache.go @@ -3,7 +3,7 @@ package datastructure import ( "sync" - "github.com/iotaledger/goshimmer/packages/typeutils" + "github.com/iotaledger/hive.go/typeutils" ) type lruCacheElement struct { diff --git a/packages/filter/byte_array_filter.go b/packages/filter/byte_array_filter.go index d8cb89fe..e5955c32 100644 --- a/packages/filter/byte_array_filter.go +++ b/packages/filter/byte_array_filter.go @@ -3,7 +3,7 @@ package filter import ( "sync" - "github.com/iotaledger/goshimmer/packages/typeutils" + "github.com/iotaledger/hive.go/typeutils" ) type ByteArrayFilter struct { diff --git a/packages/model/approvers/approvers.go b/packages/model/approvers/approvers.go index f885fd6a..1c98156d 100644 --- a/packages/model/approvers/approvers.go +++ b/packages/model/approvers/approvers.go @@ -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" ) diff --git a/packages/model/bundle/bundle.go b/packages/model/bundle/bundle.go index 3c9c6208..977592c4 100644 --- a/packages/model/bundle/bundle.go +++ b/packages/model/bundle/bundle.go @@ -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" ) diff --git a/packages/model/transactionmetadata/transactionmetadata.go b/packages/model/transactionmetadata/transactionmetadata.go index b8618810..daf4a234 100644 --- a/packages/model/transactionmetadata/transactionmetadata.go +++ b/packages/model/transactionmetadata/transactionmetadata.go @@ -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" ) diff --git a/packages/typeutils/typeutils.go b/packages/typeutils/typeutils.go deleted file mode 100644 index 6bd97c88..00000000 --- a/packages/typeutils/typeutils.go +++ /dev/null @@ -1,10 +0,0 @@ -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(¶m))[1] == 0 -} diff --git a/packages/typeutils/unsafe.go b/packages/typeutils/unsafe.go deleted file mode 100644 index 56e8a996..00000000 --- a/packages/typeutils/unsafe.go +++ /dev/null @@ -1,30 +0,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 -} diff --git a/packages/typeutils/unsafe_test.go b/packages/typeutils/unsafe_test.go deleted file mode 100644 index d28a5c06..00000000 --- a/packages/typeutils/unsafe_test.go +++ /dev/null @@ -1,92 +0,0 @@ -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) - } - } -} diff --git a/plugins/gossip/gossip.go b/plugins/gossip/gossip.go index eff0dfa9..16433e33 100644 --- a/plugins/gossip/gossip.go +++ b/plugins/gossip/gossip.go @@ -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 ( diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go index 9b176599..56babd44 100644 --- a/plugins/gossip/plugin.go +++ b/plugins/gossip/plugin.go @@ -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 ( diff --git a/plugins/tangle/approvers.go b/plugins/tangle/approvers.go index e1503396..fd4461aa 100644 --- a/plugins/tangle/approvers.go +++ b/plugins/tangle/approvers.go @@ -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" ) diff --git a/plugins/tangle/bundle.go b/plugins/tangle/bundle.go index d8c33fc7..bd55c6e6 100644 --- a/plugins/tangle/bundle.go +++ b/plugins/tangle/bundle.go @@ -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" ) diff --git a/plugins/tangle/transaction.go b/plugins/tangle/transaction.go index 01bbe222..aeb4c915 100644 --- a/plugins/tangle/transaction.go +++ b/plugins/tangle/transaction.go @@ -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" ) diff --git a/plugins/tangle/transaction_metadata.go b/plugins/tangle/transaction_metadata.go index 07dc28f5..e1032ed6 100644 --- a/plugins/tangle/transaction_metadata.go +++ b/plugins/tangle/transaction_metadata.go @@ -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" ) -- GitLab