why

Why the Engine.trees is a []methodTree,not is a map[string]*node
My question is why Engine.trees is a slice instead of a map?
In httprouter,the tree is map[string]*node,but in gin, it is
type methodTree struct { method string root *node }
type methodTrees []methodTree

1 thought on “why

  1. in my test, slice is a little faster than map when 9 method,
    actually, I think they are no difference when small amount.

    Running tool: /usr/local/go/bin/go test -benchmem -run=^$ demo -bench .
    
    goos: darwin
    goarch: amd64
    pkg: demo
    BenchmarkSlice-8   	54080906	        21.5 ns/op	       0 B/op	       0 allocs/op
    BenchmarkMap-8     	46982827	        25.9 ns/op	       0 B/op	       0 allocs/op
    PASS
    ok  	demo	2.438s
    Success: Benchmarks passed.
    

    my test code

    package main
    
    import (
    	"crypto/rand"
    	"fmt"
    	"math/big"
    )
    
    type Tree struct {
    	method string
    	val    int
    }
    
    var methods []string = []string{
    	"GET", "POST", "PUT", "DELETE", "HEAD", "CONNECT", "OPTIONS", "TRACE", "PATCH",
    }
    
    var sliceTree []Tree = []Tree{}
    var mapTree map[string]int = make(map[string]int)
    
    func initSlice() {
    	for i := 0; i < len(methods); i++ {
    		sliceTree = append(sliceTree, Tree{
    			method: methods[i],
    			val:    1,
    		})
    
    	}
    }
    
    func initMap() {
    	for i := 0; i < len(methods); i++ {
    		mapTree[methods[i]] = 1
    	}
    }
    
    func findSlice(method string) int {
    	for _, m := range sliceTree {
    		if m.method == method {
    			return m.val
    		}
    	}
    
    	return 0
    }
    
    func findMap(method string) int {
    	v, _ := mapTree[method]
    
    	return v
    }
    
    func getRandom() {
    
    	for i := 0; i < 100; i++ {
    		res, _ := rand.Int(rand.Reader, big.NewInt(9))
    		fmt.Print(res)
    		fmt.Print(",")
    	}
    }
    
    func main() {
    	// getRandom()
    	initMap()
    	initSlice()
    	fmt.Println(len(sliceTree))
    	fmt.Println(len(mapTree))
    }

    benchmark

    package main
    
    import (
    	"testing"
    )
    
    var randomInt []int = []int{
    	1, 8, 7, 4, 6, 6, 0, 5, 3, 8, 7, 4, 8, 0, 2, 0, 3, 7, 4, 3, 2, 3, 1, 4, 3, 7, 6, 7, 2, 0, 0, 8, 4, 4, 1, 0, 2, 4, 2, 2, 4, 6, 8, 4, 6, 1, 1, 3, 6, 3, 1, 4, 5, 8, 5, 3, 8, 4, 2, 8, 3, 2, 0, 0, 1, 2, 5, 7, 2, 1, 0, 6, 5, 7, 4, 2, 4, 8, 7, 5, 0, 2, 8, 5, 8, 5, 3, 0, 5, 6, 7, 4, 7, 1, 5, 8, 5, 8, 3, 4,
    }
    
    func BenchmarkSlice(b *testing.B) {
    	initSlice()
    
    	for i := 0; i < b.N; i++ {
    		n := randomInt[i%%len(randomInt)]
    		findSlice(methods[n])
    	}
    }
    
    func BenchmarkMap(b *testing.B) {
    
    	initMap()
    	for i := 0; i < b.N; i++ {
    		n := randomInt[i%%len(randomInt)]
    		findMap(methods[n])
    	}
    }

Comments are closed.

5 thoughts on “Why!

  1. @christophstockinger it saddens me to read these words. Angular might not be perfect and it doesn’t claim to be. The docs have come a long way, but the amount of effort that was put into it to make it better should not be underestimated.

    But we do need to show respect to one another. If you do not like Angular and would never like to use it, that is perfectly fine, there is so much more to choose from.

    But keep in mind that showing this disrespect towards the Angular core team and more importantly all the people contributing and maintaining this open source project is not cool. Feedback like this will not help in any way.

  2. The tickets here are to report bugs or request features. Not for useless framework bashing or discussions u can do in a chat. Useless tickets, without any action or valuable feedback costs money and time. And as a CEO u should know that this is not cool.
    Give valuable feedback with specified snippets scenarios where your approach would be different. If u like vue. Then use vue.js. angular is not vue.js.

    Btw: Its also a question of feedback culture. Just saying someone its all sh… ur doing is no valuable feedback, nothing this person can work with.

Comments are closed.