From dc4401be1e4c3f2011311feab972ca4edd8e5451 Mon Sep 17 00:00:00 2001 From: zhangjuntao <369090720@qq.com> Date: Thu, 24 Feb 2022 11:26:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- raw_memory_test.go | 4 ++-- span_pool_test.go | 28 ---------------------------- xmm.go | 19 +------------------ xmm_test.go | 31 ------------------------------- 4 files changed, 3 insertions(+), 79 deletions(-) diff --git a/raw_memory_test.go b/raw_memory_test.go index 3275ec0..98a1128 100644 --- a/raw_memory_test.go +++ b/raw_memory_test.go @@ -84,8 +84,8 @@ func Test_Syscall6(t *testing.T) { } func Test_LineAlloc(t *testing.T) { - userSize, num := unsafe.Sizeof(User{}), 1000000000 - fmt.Println(heapRawMemoryBytes*8, unsafe.Sizeof(User{})*1000000000) + userSize, num := unsafe.Sizeof(User{}), 10000 + fmt.Println(heapRawMemoryBytes*8, unsafe.Sizeof(User{})*uintptr(num)) size := int(round(uintptr(heapRawMemoryBytes*8), 4096)) addr := uintptr(sysReserve(size)) // 必须预留的多才可以 arena linearAlloc学习这个 prot, flags, fd, offset, length := syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_ANON|syscall.MAP_FIXED|syscall.MAP_PRIVATE, -1, 0, size diff --git a/span_pool_test.go b/span_pool_test.go index 474c7fe..2ebd239 100644 --- a/span_pool_test.go +++ b/span_pool_test.go @@ -86,34 +86,6 @@ func TestSpanLock(t *testing.T) { } } -func Test_SanPool_Alloc(t *testing.T) { - f := &Factory{} - mm, err := f.CreateConcurrentHashMapMemory(0.6, 1) - if err != nil { - panic(err) - } - key, val := "key", "val" - uSize := unsafe.Sizeof(User{}) - t1 := time.Now() - us := make([]*User, 1000000) - - for i := 0; i < 1000000; i++ { - entryPtr, err := mm.Alloc(uSize) - if err != nil { - panic(err) - } - user := (*User)(entryPtr) - f1(user, key, val) - us[i] = user - } - fmt.Println(time.Now().Sub(t1), len(us)) - for _, u := range us { - if u.Addr != "key" { - panic(u) - } - } -} - func Test_SanPool_Find(t *testing.T) { h, err := newXHeap() if err != nil { diff --git a/xmm.go b/xmm.go index 67fa261..992557d 100644 --- a/xmm.go +++ b/xmm.go @@ -142,6 +142,7 @@ type Factory struct { sp *xSpanPool } +// CreateMemory spanFact为负载因子,当span内存超过这个百分比阈值,就会扩容 func (s *Factory) CreateMemory(spanFact float32) (XMemory, error) { if spanFact <= 0 { return nil, NilError @@ -159,24 +160,6 @@ func (s *Factory) CreateMemory(spanFact float32) (XMemory, error) { return &mm{sp: sp, sa: sa, h: h}, nil } -//NewXConcurrentHashMapSpanPool -func (s *Factory) CreateConcurrentHashMapMemory(spanFact float32, pageNumCoefficient uint8) (XMemory, error) { - if spanFact <= 0 { - return nil, NilError - } - h, err := newXHeap() - if err != nil { - return nil, err - } - sp, err := newXConcurrentHashMapSpanPool(h, spanFact, pageNumCoefficient) - if err != nil { - return nil, err - } - sa := newXStringAllocator(sp) - s.sp = sp - return &mm{sp: sp, sa: sa, h: h}, nil -} - func (s *Factory) PrintStatus() { for index, u := range s.sp.inuse { if u < 100 { diff --git a/xmm_test.go b/xmm_test.go index f01e6b6..3c7a636 100644 --- a/xmm_test.go +++ b/xmm_test.go @@ -402,37 +402,6 @@ func Test_NewUser(t *testing.T) { fmt.Println(time.Now().Sub(t1)) } -func Test_FromInAddr(t *testing.T) { - f := &Factory{} - mm, err := f.CreateConcurrentHashMapMemory(0.6, 1) - if err != nil { - t.Fatal(err) - } - key, val := "key", "val" - uSize := unsafe.Sizeof(User{}) - t1 := time.Now() - for i := 0; i < 10000000; i++ { - entryPtr, err := mm.Alloc(uSize + uintptr(len(key)) + uintptr(len(val))) - if err != nil { - t.Fatal(err) - } - _ = (*User)(entryPtr) - /* - keyValPtrs, err := mm.FromInAddr(uintptr(entryPtr)+uSize, key, val) - if err != nil { - t.Fatal(err) - } - user.Addr = *(keyValPtrs[0]) - user.Desc = *(keyValPtrs[1]) - */ - /* - user.Addr = key - user.Desc = val - */ - } - fmt.Println(time.Now().Sub(t1)) -} - func TestMm_Alloc(t *testing.T) { f := &Factory{} mm, err := f.CreateMemory(0.6) From 088d6934cb63e51fbfb3098933f5ff2a4e3a4fc2 Mon Sep 17 00:00:00 2001 From: zhangjuntao <369090720@qq.com> Date: Thu, 24 Feb 2022 12:00:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- span_pool_test.go | 28 ++++++++++++++++++++++++++++ xmm_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/span_pool_test.go b/span_pool_test.go index 2ebd239..ed4f1e3 100644 --- a/span_pool_test.go +++ b/span_pool_test.go @@ -86,6 +86,34 @@ func TestSpanLock(t *testing.T) { } } +func Test_SanPool_Alloc(t *testing.T) { + f := &Factory{} + mm, err := f.CreateMemory(0.6) + if err != nil { + panic(err) + } + key, val := "key", "val" + uSize := unsafe.Sizeof(User{}) + t1 := time.Now() + us := make([]*User, 1000000) + + for i := 0; i < 1000000; i++ { + entryPtr, err := mm.Alloc(uSize) + if err != nil { + panic(err) + } + user := (*User)(entryPtr) + f1(user, key, val) + us[i] = user + } + fmt.Println(time.Now().Sub(t1), len(us)) + for _, u := range us { + if u.Addr != "key" { + panic(u) + } + } +} + func Test_SanPool_Find(t *testing.T) { h, err := newXHeap() if err != nil { diff --git a/xmm_test.go b/xmm_test.go index 3c7a636..42dd323 100644 --- a/xmm_test.go +++ b/xmm_test.go @@ -402,6 +402,37 @@ func Test_NewUser(t *testing.T) { fmt.Println(time.Now().Sub(t1)) } +func Test_FromInAddr(t *testing.T) { + f := &Factory{} + mm, err := f.CreateMemory(0.6) + if err != nil { + t.Fatal(err) + } + key, val := "key", "val" + uSize := unsafe.Sizeof(User{}) + t1 := time.Now() + for i := 0; i < 10000000; i++ { + entryPtr, err := mm.Alloc(uSize + uintptr(len(key)) + uintptr(len(val))) + if err != nil { + t.Fatal(err) + } + _ = (*User)(entryPtr) + /* + keyValPtrs, err := mm.FromInAddr(uintptr(entryPtr)+uSize, key, val) + if err != nil { + t.Fatal(err) + } + user.Addr = *(keyValPtrs[0]) + user.Desc = *(keyValPtrs[1]) + */ + /* + user.Addr = key + user.Desc = val + */ + } + fmt.Println(time.Now().Sub(t1)) +} + func TestMm_Alloc(t *testing.T) { f := &Factory{} mm, err := f.CreateMemory(0.6)