diff --git a/packages/datastructure/lru_cache.go b/packages/datastructure/lru_cache.go index 469e54a961fd78abab92d86a85d8b9d008741e9a..fe15c424694b307b01e8a122864537e0057f74af 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 d8cb89fe010d8571331a2f4dbc0e31b328cbf275..e5955c32c3d34045b0764b83d2566611ee72cce7 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 f885fd6af1db7afc74c682c577b18b54f533d028..1c98156dcda57532bb11a5a3c61de591ddc015f4 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 3c9c6208eb00a25c4e182bfec12c5ad10c50f01c..977592c4c77afa16c7793b56b979e5fa1713aaa5 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 b86188108c5cb715637b1553b2924972b7741447..daf4a2342ed5b586e806134927756e8e32f1a568 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 6bd97c8827ce824ca64a63a227680359712b769c..0000000000000000000000000000000000000000 --- 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 56e8a99697d960216ec5aecdad241f9e08b372d8..0000000000000000000000000000000000000000 --- 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 d28a5c06f411c754399117d161cc0e7585cc5a19..0000000000000000000000000000000000000000 --- 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 eff0dfa925be10b501c1d795e1278d3730c676b4..16433e336c482a0c6386d34a9d5fbfc917ca9b21 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 9b17659979683f708879955c0454d8f573826cb9..56babd44a2fcfc67027ec4f465d3f685d8037cc7 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 e1503396505c720a0dc9fe9d05fe72d53d7e3e63..fd4461aa1e3cac8e3619652f1731df050f206b68 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 d8c33fc7ae4760a7e6a5bc70b9d5fa85250758b0..bd55c6e67e0cfd8eb22cc3a02d75fc142b4ac93c 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 01bbe222b903da9fac9b19145e713024c4f01390..aeb4c915affcddfbbae05a7c6594edccb96db7bf 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 07dc28f5738ab7e57548ed88ff5b3889a0ebbe06..e1032ed635b284814109bd0f1a4bcf86f3208635 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" )