From 9f24b2f398b7da6b95fbe3642d716575db5d8700 Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期五, 03 二月 2023 14:18:15 +0800
Subject: [PATCH] 1.减少bhomebus的内存拷贝

---
 api/bhsgo/bhome_node.go |   26 ++++++++++++++++++++++++--
 1 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/api/bhsgo/bhome_node.go b/api/bhsgo/bhome_node.go
index 7fcb43c..a295673 100644
--- a/api/bhsgo/bhome_node.go
+++ b/api/bhsgo/bhome_node.go
@@ -8,10 +8,27 @@
 import "C"
 
 import (
+	"reflect"
 	"unsafe"
 
 	bh "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
 )
+
+func unsafeGetBytes(s string) []byte {
+	const MaxInt32 = 1<<31 - 1
+	return (*[MaxInt32]byte)(unsafe.Pointer((*reflect.StringHeader)(
+		unsafe.Pointer(&s)).Data))[: len(s)&MaxInt32 : len(s)&MaxInt32]
+}
+
+func ByteSlice2String(bs []byte) string {
+	return *(*string)(unsafe.Pointer(&bs))
+}
+
+/*func unsafeGetBytes(s string) []byte {
+    return (*[0x7fff0000]byte)(unsafe.Pointer(
+        (*reflect.StringHeader)(unsafe.Pointer(&s)).Data),
+    )[:len(s):len(s)]
+}*/
 
 func getPtr(n *[]byte) unsafe.Pointer {
 	if len(*n) > 0 {
@@ -150,8 +167,13 @@
 	defer C.BHFree(cpid, cpid_len)
 	defer C.BHFree(creply, creply_len)
 	if r {
-		*proc_id = string(C.GoBytes(cpid, cpid_len))
-		pub.Unmarshal(C.GoBytes(creply, creply_len))
+		//*proc_id = string(C.GoBytes(cpid, cpid_len))
+		//pub.Unmarshal(C.GoBytes(creply, creply_len))
+
+		//const MaxInt32 = 1<<32 - 1
+		const maxLen = 0x7fffffff
+		*proc_id = ByteSlice2String((*[maxLen]byte)(unsafe.Pointer(cpid))[:cpid_len:cpid_len])
+		pub.Unmarshal((*[maxLen]byte)(unsafe.Pointer(creply))[:creply_len:creply_len])
 	}
 
 	return r

--
Gitblit v1.8.0