github.com/moontrade/nogc@v0.1.7/collections/tree/generic_test.go (about)

     1  //go:build generics
     2  
     3  package tree
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/moontrade/nogc"
     8  	"math/bits"
     9  	"sync/atomic"
    10  	"testing"
    11  	"unsafe"
    12  )
    13  
    14  type Bar struct {
    15  	ID int32
    16  }
    17  
    18  func TestGeneric_Get(t *testing.T) {
    19  	generic := NewARTValueToPointer[int64, Bar](LockKindFair, OwnershipOwned, false)
    20  	generic.Put(10, nogc.AllocOf(Bar{ID: 10}))
    21  	v, found := generic.Get(10)
    22  	_ = found
    23  	var vv *Bar = v.Unwrap()
    24  	println(vv)
    25  
    26  	g1 := NewARTValueToValue[int64, nogc.Pointer](LockKindNone, false)
    27  	_ = g1
    28  
    29  	g2 := NewARTPointerToPointer[int64, Bar]
    30  	_ = g2
    31  
    32  	g3 := NewARTValueToValue[int64, int64](LockKindNone, false)
    33  	_ = g3
    34  
    35  	g4 := NewARTValueToPointer[int64, Bar](LockKindNone, OwnershipNotOwned, false)
    36  	_ = g4
    37  }
    38  
    39  func TestAtomicUintptrDecrement(t *testing.T) {
    40  	x := uintptr(10)
    41  	atomic.AddUintptr(&x, ^uintptr(0))
    42  	atomic.AddUintptr(&x, ^uintptr(0))
    43  	fmt.Println(atomic.LoadUintptr(&x))
    44  }
    45  
    46  func BenchmarkAllocOf(b *testing.B) {
    47  	b.Run("AllocOf Manual", func(b *testing.B) {
    48  		for i := 0; i < b.N; i++ {
    49  			a := nogc.Alloc(unsafe.Sizeof(Bar{}))
    50  			*(*Bar)(a.Unsafe()) = Bar{}
    51  			a.Free()
    52  		}
    53  	})
    54  
    55  	b.Run("AllocOf", func(b *testing.B) {
    56  		for i := 0; i < b.N; i++ {
    57  			a := nogc.AllocOf(Bar{})
    58  			a.Free()
    59  		}
    60  	})
    61  }
    62  
    63  func BenchmarkARTValueToValue_Get(b *testing.B) {
    64  	b.Run("Not ThreadSafe or Generic", func(b *testing.B) {
    65  		g3, _ := NewART(LockKindNone, OwnershipNotOwned, false)
    66  		defer g3.Free()
    67  
    68  		for i := 0; i < b.N; i++ {
    69  			ii := bits.Reverse64(uint64(i))
    70  			g3.PutValue(nogc.FatPointerOfUInt64(&ii), ii)
    71  		}
    72  
    73  		b.ReportAllocs()
    74  		b.ResetTimer()
    75  		for i := 0; i < b.N; i++ {
    76  			ii := bits.Reverse64(uint64(i))
    77  			_, _ = g3.Get(nogc.FatPointerOfUInt64(&ii))
    78  		}
    79  		b.StopTimer()
    80  	})
    81  
    82  	b.Run("Not ThreadSafe", func(b *testing.B) {
    83  		g3 := NewARTValueToValue[int, int](LockKindNone, false)
    84  		defer g3.Free()
    85  
    86  		for i := 0; i < b.N; i++ {
    87  			g3.Put(i, i)
    88  		}
    89  
    90  		b.ReportAllocs()
    91  		b.ResetTimer()
    92  		for i := 0; i < b.N; i++ {
    93  			_, _ = g3.Get(i)
    94  		}
    95  		b.StopTimer()
    96  	})
    97  
    98  	b.Run("ThreadSafe Unfair", func(b *testing.B) {
    99  		g3 := NewARTValueToValue[int, int](LockKindUnfair, false)
   100  		defer g3.Free()
   101  
   102  		for i := 0; i < b.N; i++ {
   103  			g3.Put(i, i)
   104  		}
   105  
   106  		b.ReportAllocs()
   107  		b.ResetTimer()
   108  		for i := 0; i < b.N; i++ {
   109  			_, _ = g3.Get(i)
   110  		}
   111  		b.StopTimer()
   112  	})
   113  
   114  	b.Run("ThreadSafe Fair", func(b *testing.B) {
   115  		g3 := NewARTValueToValue[int, int](LockKindFair, false)
   116  		defer g3.Free()
   117  
   118  		for i := 0; i < b.N; i++ {
   119  			g3.Put(i, i)
   120  		}
   121  
   122  		b.ReportAllocs()
   123  		b.ResetTimer()
   124  		for i := 0; i < b.N; i++ {
   125  			_, _ = g3.Get(i)
   126  		}
   127  		b.StopTimer()
   128  	})
   129  
   130  	b.Run("Go Map Not ThreadSafe", func(b *testing.B) {
   131  		m := make(map[int]int)
   132  
   133  		for i := 0; i < b.N; i++ {
   134  			m[i] = i
   135  		}
   136  
   137  		b.ReportAllocs()
   138  		b.ResetTimer()
   139  		for i := 0; i < b.N; i++ {
   140  			_, _ = m[i]
   141  		}
   142  		b.StopTimer()
   143  	})
   144  }
   145  
   146  func BenchmarkARTValueToValue_Put(b *testing.B) {
   147  	b.Run("Not ThreadSafe", func(b *testing.B) {
   148  		g3 := NewARTValueToValue[int, int](LockKindNone, false)
   149  		defer g3.Free()
   150  
   151  		b.ReportAllocs()
   152  		b.ResetTimer()
   153  		for i := 0; i < b.N; i++ {
   154  			g3.Put(i, i)
   155  		}
   156  		b.StopTimer()
   157  	})
   158  
   159  	b.Run("ThreadSafe Unfair", func(b *testing.B) {
   160  		g3 := NewARTValueToValue[int, int](LockKindUnfair, false)
   161  		defer g3.Free()
   162  
   163  		b.ReportAllocs()
   164  		b.ResetTimer()
   165  		for i := 0; i < b.N; i++ {
   166  			g3.Put(i, i)
   167  		}
   168  		b.StopTimer()
   169  	})
   170  
   171  	b.Run("ThreadSafe Fair", func(b *testing.B) {
   172  		g3 := NewARTValueToValue[int, int](LockKindFair, false)
   173  		defer g3.Free()
   174  
   175  		b.ReportAllocs()
   176  		b.ResetTimer()
   177  		for i := 0; i < b.N; i++ {
   178  			g3.Put(i, i)
   179  		}
   180  		b.StopTimer()
   181  	})
   182  
   183  	b.Run("Go Map Not ThreadSafe", func(b *testing.B) {
   184  		m := make(map[int]int)
   185  
   186  		b.ReportAllocs()
   187  		b.ResetTimer()
   188  		for i := 0; i < b.N; i++ {
   189  			m[i] = i
   190  		}
   191  		b.StopTimer()
   192  	})
   193  }