mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 18:50:27 +08:00
优化代码
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
type OpenFileCache struct {
|
type OpenFileCache struct {
|
||||||
poolMap map[string]*OpenFilePool // file path => Pool
|
poolMap map[string]*OpenFilePool // file path => Pool
|
||||||
poolList *linkedlist.List
|
poolList *linkedlist.List[*OpenFilePool]
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
|
|
||||||
locker sync.RWMutex
|
locker sync.RWMutex
|
||||||
@@ -33,7 +33,7 @@ func NewOpenFileCache(maxSize int) (*OpenFileCache, error) {
|
|||||||
var cache = &OpenFileCache{
|
var cache = &OpenFileCache{
|
||||||
maxSize: maxSize,
|
maxSize: maxSize,
|
||||||
poolMap: map[string]*OpenFilePool{},
|
poolMap: map[string]*OpenFilePool{},
|
||||||
poolList: linkedlist.NewList(),
|
poolList: linkedlist.NewList[*OpenFilePool](),
|
||||||
}
|
}
|
||||||
|
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
@@ -105,7 +105,7 @@ func (this *OpenFileCache) Put(filename string, file *OpenFile) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
var headPool = head.Value.(*OpenFilePool)
|
var headPool = head.Value
|
||||||
headFile, consumed := headPool.Get()
|
headFile, consumed := headPool.Get()
|
||||||
if consumed {
|
if consumed {
|
||||||
this.count--
|
this.count--
|
||||||
@@ -163,8 +163,8 @@ func (this *OpenFileCache) Debug() {
|
|||||||
goman.New(func() {
|
goman.New(func() {
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
logs.Println("==== " + types.String(this.count) + " ====")
|
logs.Println("==== " + types.String(this.count) + " ====")
|
||||||
this.poolList.Range(func(item *linkedlist.Item) (goNext bool) {
|
this.poolList.Range(func(item *linkedlist.Item[*OpenFilePool]) (goNext bool) {
|
||||||
logs.Println(filepath.Base(item.Value.(*OpenFilePool).Filename()), item.Value.(*OpenFilePool).Len())
|
logs.Println(filepath.Base(item.Value.Filename()), item.Value.Len())
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,5 +42,7 @@ func TestNewOpenFileCache_CloseAll(t *testing.T) {
|
|||||||
cache.Get("d.txt")
|
cache.Get("d.txt")
|
||||||
cache.CloseAll()
|
cache.CloseAll()
|
||||||
|
|
||||||
|
if testutils.IsSingleTesting() {
|
||||||
time.Sleep(6 * time.Second)
|
time.Sleep(6 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type OpenFilePool struct {
|
type OpenFilePool struct {
|
||||||
c chan *OpenFile
|
c chan *OpenFile
|
||||||
linkItem *linkedlist.Item
|
linkItem *linkedlist.Item[*OpenFilePool]
|
||||||
filename string
|
filename string
|
||||||
version int64
|
version int64
|
||||||
isClosed bool
|
isClosed bool
|
||||||
@@ -21,7 +21,7 @@ func NewOpenFilePool(filename string) *OpenFilePool {
|
|||||||
c: make(chan *OpenFile, 1024),
|
c: make(chan *OpenFile, 1024),
|
||||||
version: fasttime.Now().UnixMilli(),
|
version: fasttime.Now().UnixMilli(),
|
||||||
}
|
}
|
||||||
pool.linkItem = linkedlist.NewItem(pool)
|
pool.linkItem = linkedlist.NewItem[*OpenFilePool](pool)
|
||||||
return pool
|
return pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
package linkedlist
|
package linkedlist
|
||||||
|
|
||||||
type Item struct {
|
type Item[T any] struct {
|
||||||
prev *Item
|
prev *Item[T]
|
||||||
next *Item
|
next *Item[T]
|
||||||
|
|
||||||
Value interface{}
|
Value T
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewItem(value interface{}) *Item {
|
func NewItem[T any](value T) *Item[T] {
|
||||||
return &Item{Value: value}
|
return &Item[T]{Value: value}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,25 +2,25 @@
|
|||||||
|
|
||||||
package linkedlist
|
package linkedlist
|
||||||
|
|
||||||
type List struct {
|
type List[T any] struct {
|
||||||
head *Item
|
head *Item[T]
|
||||||
end *Item
|
end *Item[T]
|
||||||
count int
|
count int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewList() *List {
|
func NewList[T any]() *List[T] {
|
||||||
return &List{}
|
return &List[T]{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Head() *Item {
|
func (this *List[T]) Head() *Item[T] {
|
||||||
return this.head
|
return this.head
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) End() *Item {
|
func (this *List[T]) End() *Item[T] {
|
||||||
return this.end
|
return this.end
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Push(item *Item) {
|
func (this *List[T]) Push(item *Item[T]) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -36,7 +36,7 @@ func (this *List) Push(item *Item) {
|
|||||||
this.add(item)
|
this.add(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Remove(item *Item) {
|
func (this *List[T]) Remove(item *Item[T]) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -58,11 +58,11 @@ func (this *List) Remove(item *Item) {
|
|||||||
this.count--
|
this.count--
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Len() int {
|
func (this *List[T]) Len() int {
|
||||||
return this.count
|
return this.count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Range(f func(item *Item) (goNext bool)) {
|
func (this *List[T]) Range(f func(item *Item[T]) (goNext bool)) {
|
||||||
for e := this.head; e != nil; e = e.next {
|
for e := this.head; e != nil; e = e.next {
|
||||||
goNext := f(e)
|
goNext := f(e)
|
||||||
if !goNext {
|
if !goNext {
|
||||||
@@ -71,12 +71,12 @@ func (this *List) Range(f func(item *Item) (goNext bool)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) Reset() {
|
func (this *List[T]) Reset() {
|
||||||
this.head = nil
|
this.head = nil
|
||||||
this.end = nil
|
this.end = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *List) add(item *Item) {
|
func (this *List[T]) add(item *Item[T]) {
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,19 +12,21 @@ func TestNewList_Memory(t *testing.T) {
|
|||||||
var stat1 = &runtime.MemStats{}
|
var stat1 = &runtime.MemStats{}
|
||||||
runtime.ReadMemStats(stat1)
|
runtime.ReadMemStats(stat1)
|
||||||
|
|
||||||
var list = linkedlist.NewList()
|
var list = linkedlist.NewList[int]()
|
||||||
for i := 0; i < 1_000_000; i++ {
|
for i := 0; i < 1_000_000; i++ {
|
||||||
var item = &linkedlist.Item{}
|
var item = &linkedlist.Item[int]{}
|
||||||
list.Push(item)
|
list.Push(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime.GC()
|
||||||
|
|
||||||
var stat2 = &runtime.MemStats{}
|
var stat2 = &runtime.MemStats{}
|
||||||
runtime.ReadMemStats(stat2)
|
runtime.ReadMemStats(stat2)
|
||||||
t.Log((stat2.HeapInuse-stat1.HeapInuse)/1024/1024, "MB")
|
t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB")
|
||||||
t.Log(list.Len())
|
t.Log(list.Len())
|
||||||
|
|
||||||
var count = 0
|
var count = 0
|
||||||
list.Range(func(item *linkedlist.Item) (goNext bool) {
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
count++
|
count++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
@@ -32,7 +34,7 @@ func TestNewList_Memory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestList_Push(t *testing.T) {
|
func TestList_Push(t *testing.T) {
|
||||||
var list = linkedlist.NewList()
|
var list = linkedlist.NewList[int]()
|
||||||
list.Push(linkedlist.NewItem(1))
|
list.Push(linkedlist.NewItem(1))
|
||||||
list.Push(linkedlist.NewItem(2))
|
list.Push(linkedlist.NewItem(2))
|
||||||
|
|
||||||
@@ -41,42 +43,43 @@ func TestList_Push(t *testing.T) {
|
|||||||
|
|
||||||
var item4 = linkedlist.NewItem(4)
|
var item4 = linkedlist.NewItem(4)
|
||||||
list.Push(item4)
|
list.Push(item4)
|
||||||
list.Range(func(item *linkedlist.Item) (goNext bool) {
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
t.Log(item.Value)
|
t.Log(item.Value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Log("=== after push3 ===")
|
t.Log("=== after push 3 ===")
|
||||||
list.Push(item3)
|
list.Push(item3)
|
||||||
list.Range(func(item *linkedlist.Item) (goNext bool) {
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
t.Log(item.Value)
|
t.Log(item.Value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Log("=== after push4 ===")
|
t.Log("=== after push 4 ===")
|
||||||
list.Push(item4)
|
list.Push(item4)
|
||||||
list.Push(item3)
|
list.Push(item3)
|
||||||
list.Push(item3)
|
list.Push(item3)
|
||||||
list.Push(item3)
|
list.Push(item3)
|
||||||
list.Push(item4)
|
list.Push(item4)
|
||||||
list.Push(item4)
|
list.Push(item4)
|
||||||
list.Range(func(item *linkedlist.Item) (goNext bool) {
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
t.Log(item.Value)
|
t.Log(item.Value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Log("=== after remove ===")
|
t.Log("=== after remove 3 ===")
|
||||||
list.Remove(item3)
|
list.Remove(item3)
|
||||||
list.Range(func(item *linkedlist.Item) (goNext bool) {
|
list.Range(func(item *linkedlist.Item[int]) (goNext bool) {
|
||||||
t.Log(item.Value)
|
t.Log(item.Value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkList_Add(b *testing.B) {
|
func BenchmarkList_Add(b *testing.B) {
|
||||||
var list = linkedlist.NewList()
|
var list = linkedlist.NewList[int]()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var item = &linkedlist.Item{}
|
var item = &linkedlist.Item[int]{}
|
||||||
list.Push(item)
|
list.Push(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user