-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_1.go
109 lines (89 loc) · 2.13 KB
/
test_1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"math"
"unsafe"
)
type SkipListNode struct {
//将value offset 与 value size 合并编码,方便实现CAS操作
//低31位存放offset,高31位存放size
// value offset: uint32(bits 0-31)
// value size : uint32(bits 32-63)
value uint64 //8
//尽量减少key的内存占用
keyOffset uint32 //不可变 //4
keySize uint16 //不可变 //2
//节点实际高度(指针个数)
height uint16 //2
//skiplist节点的next指针数组,默认初始化为MAXHEIGHT
tower [20]uint32 //4*20
}
func sizeOfInt64(x uint64) (n int) {
for {
n++
x >>= 7
if x == 0 {
break
}
}
return n
}
func test() {
OFFSETSIZE := int(unsafe.Sizeof(uint32(0)))
MAXNODESIZE := int(unsafe.Sizeof(SkipListNode{}))
//内存对齐
NODEALIGN := int(unsafe.Sizeof(uint64(0))) - 1
fmt.Printf("OFFSETSIZE: %d\n", OFFSETSIZE)
fmt.Printf("MAXNODESIZE: %d\n", MAXNODESIZE)
fmt.Printf("NODEALIGN: %d\n", NODEALIGN)
}
// 验证切片的深浅拷贝问题
func test_slice() {
var sl []int = make([]int, 5)
fmt.Println("sl=", sl)
s2 := sl[1:3]
s2[1] = 3
fmt.Println("s2=", s2)
fmt.Println("new sl=", sl)
}
//go:linkname FastRand runtime.fastrand
func FastRand() uint32
func test_random() {
heightIncrease := math.MaxUint32 / 3
fmt.Println("heightIncrease=", heightIncrease)
fmt.Println("rand=", FastRand())
}
// Entry 是一个示例结构体
type Entry struct {
Key string
Value int
}
// Item 接口定义
type Item interface {
Entry() *Entry
}
// MyItem 是实现了 Item 接口的具体类型
type MyItem struct {
Data Entry
}
// Entry 方法的实现
func (mi *MyItem) Entry() *Entry {
return &mi.Data
}
func TestInterface() {
// 创建 MyItem 对象
myItem := &MyItem{Data: Entry{Key: "example", Value: 42}}
// 通过 Item 接口调用 Entry 方法
entry := myItem.Entry()
// 打印 Entry 对象的内容
fmt.Println("Key:", entry.Key)
fmt.Println("Value:", entry.Value)
}
//find . -type f -name "*.go" -not -path "./vendor/*" -exec grep -v '^ *//' {} \; -exec grep -v '^ *$' {} \; | wc -l
func main() {
//test()
//fmt.Printf("%d", sizeOfInt64(903432))
//test_slice()
//test_random()
TestInterface()
}