Skip to content

Commit

Permalink
Adding no ptr
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Schendel <[email protected]>
  • Loading branch information
amitschendel committed Jan 28, 2025
1 parent 068e12a commit 7865ee9
Show file tree
Hide file tree
Showing 10 changed files with 715 additions and 711 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/softwarecomposition/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ type StackFrame struct {
}

type CallStackNode struct {
Children []*CallStackNode
Children []CallStackNode
Parent *CallStackNode
Frame *StackFrame
}
Expand Down
1,192 changes: 596 additions & 596 deletions pkg/apis/softwarecomposition/v1beta1/generated.pb.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkg/apis/softwarecomposition/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ type StackFrame struct {
}

type CallStackNode struct {
Children []*CallStackNode `json:"children" protobuf:"bytes,1,rep,name=children"`
Parent *CallStackNode `json:"parent" protobuf:"bytes,2,opt,name=parent"`
Frame *StackFrame `json:"frame" protobuf:"bytes,3,opt,name=frame"`
Children []CallStackNode `json:"children" protobuf:"bytes,1,rep,name=children"`
Parent *CallStackNode `json:"parent" protobuf:"bytes,2,opt,name=parent"`
Frame *StackFrame `json:"frame" protobuf:"bytes,3,opt,name=frame"`
}

type CallStack struct {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions pkg/apis/softwarecomposition/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions pkg/apis/softwarecomposition/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 41 additions & 30 deletions pkg/registry/file/callstack/callstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func copyNode(node *types.CallStackNode) *types.CallStackNode {
return nil
}
newNode := &types.CallStackNode{
Children: make([]*types.CallStackNode, 0),
Children: make([]types.CallStackNode, 0),
Parent: nil,
Frame: nil,
}
Expand All @@ -23,20 +23,6 @@ func copyNode(node *types.CallStackNode) *types.CallStackNode {
return newNode
}

// copySubtree creates a deep copy of a CallStackNode and all its children
func copySubtree(node *types.CallStackNode) *types.CallStackNode {
if node == nil {
return nil
}
newNode := copyNode(node)
for _, child := range node.Children {
childCopy := copySubtree(child)
childCopy.Parent = newNode
newNode.Children = append(newNode.Children, childCopy)
}
return newNode
}

// framesEqual checks if two StackFrames are equal
func framesEqual(f1, f2 *types.StackFrame) bool {
if f1 == nil && f2 == nil {
Expand All @@ -49,13 +35,13 @@ func framesEqual(f1, f2 *types.StackFrame) bool {
}

// getNodesToProcess returns the nodes that should be processed for unification
func getNodesToProcess(cs *types.CallStack) []*types.CallStackNode {
func getNodesToProcess(cs *types.CallStack) []types.CallStackNode {
if cs == nil {
return nil
}
if cs.Root.Frame != nil {
// If root has a frame, treat the root itself as a node to process
return []*types.CallStackNode{cs.Root}
return []types.CallStackNode{*cs.Root}
}
// Otherwise process its children
return cs.Root.Children
Expand All @@ -65,14 +51,13 @@ func getNodesToProcess(cs *types.CallStack) []*types.CallStackNode {
func createDummyRoot() *types.CallStack {
return &types.CallStack{
Root: &types.CallStackNode{
Children: make([]*types.CallStackNode, 0),
Children: make([]types.CallStackNode, 0),
Parent: nil,
Frame: nil,
},
}
}

// UnifyCallStacks takes two CallStacks and returns a unified CallStack
func UnifyCallStacks(cs1, cs2 *types.CallStack) *types.CallStack {
unified := createDummyRoot()

Expand All @@ -82,30 +67,31 @@ func UnifyCallStacks(cs1, cs2 *types.CallStack) *types.CallStack {

// Process nodes from cs1
for _, node1 := range getNodesToProcess(cs1) {
subtree := copySubtree(node1)
subtree := copySubtree(&node1)
subtree.Parent = unified.Root
unified.Root.Children = append(unified.Root.Children, subtree)
unified.Root.Children = append(unified.Root.Children, *subtree)
}

// Process nodes from cs2
for _, node2 := range getNodesToProcess(cs2) {
merged := false
for _, existingChild := range unified.Root.Children {
for i := range unified.Root.Children {
existingChild := &unified.Root.Children[i]
if framesEqual(node2.Frame, existingChild.Frame) {
// If frames are equal at this level, try to merge their children
for _, child2 := range node2.Children {
foundMatch := false
childFound := false
for _, existingGrandChild := range existingChild.Children {
if framesEqual(child2.Frame, existingGrandChild.Frame) {
foundMatch = true
childFound = true
break
}
}
if !foundMatch {
if !childFound {
// Add this as a new path under the existing node
subtree := copySubtree(child2)
subtree.Parent = existingChild
existingChild.Children = append(existingChild.Children, subtree)
childCopy := copySubtree(&child2)
childCopy.Parent = existingChild
existingChild.Children = append(existingChild.Children, *childCopy)
}
}
merged = true
Expand All @@ -114,15 +100,40 @@ func UnifyCallStacks(cs1, cs2 *types.CallStack) *types.CallStack {
}
if !merged {
// Add this as a completely new path
subtree := copySubtree(node2)
subtree := copySubtree(&node2)
subtree.Parent = unified.Root
unified.Root.Children = append(unified.Root.Children, subtree)
unified.Root.Children = append(unified.Root.Children, *subtree)
}
}

return unified
}

func copySubtree(node *types.CallStackNode) *types.CallStackNode {
if node == nil {
return nil
}
newNode := &types.CallStackNode{
Children: make([]types.CallStackNode, 0),
Parent: nil,
Frame: nil,
}
if node.Frame != nil {
newNode.Frame = &types.StackFrame{
FileID: node.Frame.FileID,
Lineno: node.Frame.Lineno,
}
}
for _, child := range node.Children {
childCopy := copySubtree(&child)
if childCopy != nil {
childCopy.Parent = newNode
newNode.Children = append(newNode.Children, *childCopy)
}
}
return newNode
}

// UnifyIdentifiedCallStacks takes a list of IdentifiedCallStack and returns a list of unified CallStacks
func UnifyIdentifiedCallStacks(stacks []types.IdentifiedCallStack) []types.IdentifiedCallStack {
// Group CallStacks by CallID
Expand Down
26 changes: 13 additions & 13 deletions pkg/registry/file/callstack/callstack_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,54 @@ import (

// Helper function to create a linear call stack of specified depth
func createLinearCallStack(depth int) *types.CallStack {
root := &types.CallStackNode{
Children: make([]*types.CallStackNode, 0),
root := types.CallStackNode{
Children: make([]types.CallStackNode, 0),
}

current := root
for i := 1; i <= depth; i++ {
newNode := &types.CallStackNode{
newNode := types.CallStackNode{
Frame: &types.StackFrame{
FileID: strconv.Itoa(i),
Lineno: strconv.Itoa(i),
},
Children: make([]*types.CallStackNode, 0),
Parent: current,
Children: make([]types.CallStackNode, 0),
Parent: &current,
}
current.Children = append(current.Children, newNode)
current = newNode
}

return &types.CallStack{Root: root}
return &types.CallStack{Root: &root}
}

// Helper function to create a branching call stack with specified depth and width
func createBranchingCallStack(depth, width int) *types.CallStack {
root := &types.CallStackNode{
Children: make([]*types.CallStackNode, 0),
Children: make([]types.CallStackNode, 0),
}

var addChildren func(*types.CallStackNode, int, int)
addChildren = func(node *types.CallStackNode, currentDepth, maxDepth int) {
var addChildren func(types.CallStackNode, int, int)
addChildren = func(node types.CallStackNode, currentDepth, maxDepth int) {
if currentDepth >= maxDepth {
return
}

for i := 0; i < width; i++ {
child := &types.CallStackNode{
child := types.CallStackNode{
Frame: &types.StackFrame{
FileID: strconv.Itoa(currentDepth + 1),
Lineno: strconv.Itoa(i + 1),
},
Children: make([]*types.CallStackNode, 0),
Parent: node,
Children: make([]types.CallStackNode, 0),
Parent: &node,
}
node.Children = append(node.Children, child)
addChildren(child, currentDepth+1, maxDepth)
}
}

addChildren(root, 0, depth)
addChildren(*root, 0, depth)
return &types.CallStack{Root: root}
}

Expand Down
Loading

0 comments on commit 7865ee9

Please sign in to comment.