github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/structs/transporttrie/diff.go (about)

     1  package transporttrie
     2  
     3  func (originalTrie *Trie) Diff(srcTrie *Trie) *Trie {
     4  	dstTrie := originalTrie.Clone(1, 1)
     5  	dstTrie.root = originalTrie.root.clone()
     6  
     7  	srcNodes := []*trieNode{srcTrie.root}
     8  	dstNodes := []*trieNode{dstTrie.root}
     9  
    10  	for len(srcNodes) > 0 {
    11  		st := srcNodes[0]
    12  		srcNodes = srcNodes[1:]
    13  
    14  		dt := dstNodes[0]
    15  		dstNodes = dstNodes[1:]
    16  
    17  		for _, srcChildNode := range st.children {
    18  			dt.findNodeAt(srcChildNode.name, func(dstChildNode *trieNode) {
    19  				if srcChildNode.value > dstChildNode.value {
    20  					dstChildNode.value = 0
    21  				} else {
    22  					dstChildNode.value -= srcChildNode.value
    23  				}
    24  				srcNodes = append([]*trieNode{srcChildNode}, srcNodes...)
    25  				dstNodes = append([]*trieNode{dstChildNode}, dstNodes...)
    26  			})
    27  		}
    28  	}
    29  	return dstTrie
    30  }