Skip to content

Commit ee64f24

Browse files
maltekmpollmeier
authored andcommitted
ProtoToGraphNodeMappings: use mutable datastructures internally
the immmutable maps have some unneccessary overhead
1 parent f04c034 commit ee64f24

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

codepropertygraph/src/main/scala/io/shiftleft/codepropertygraph/cpgloading/ProtoToGraphNodeMappings.scala

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,21 @@ package io.shiftleft.codepropertygraph.cpgloading
33
import flatgraph.*
44
import io.shiftleft.proto.cpg.Cpg.CpgStruct
55

6+
import scala.collection.mutable
67
import scala.jdk.CollectionConverters.*
78

8-
/** Mutable datastructure to preserve mapping between proto and cpg nodes during ProtoToCpg import.
9+
/** Mutable data structure to preserve mapping between proto and cpg nodes during ProtoToCpg import.
910
*
1011
* Context: we need to run two passes: 1) add nodes and 2) set node properties and add edges (this is due to
1112
* flatgraph-specific implementation details)
1213
*
13-
* Because of that, we need to remember the mapping from proto node id to gnode. Typically that's just a plain mapping,
14-
* but there's one special case for TYPE nodes: some (parallel) frontends create duplicate TYPE nodes which we need to
15-
* deduplicate...
14+
* Because of that, we need to remember the mapping from proto node id to gnode. Typically, that's just a plain
15+
* mapping. But there's one special case for TYPE nodes: some (parallel) frontends create duplicate TYPE nodes which we
16+
* need to deduplicate...
1617
*/
1718
class ProtoToGraphNodeMappings {
18-
private var protoNodeIdToGNode = Map.empty[Long, DNode]
19-
private var typeFullNameToGNode = Map.empty[String, DNode]
20-
21-
def addAll(other: ProtoToGraphNodeMappings): Unit = {
22-
val intersection1 = this.protoNodeIdToGNode.keySet.intersect(other.protoNodeIdToGNode.keySet)
23-
val intersection2 = this.typeFullNameToGNode.keySet.intersect(other.typeFullNameToGNode.keySet)
24-
assert(
25-
intersection1.isEmpty,
26-
s"unexpected duplicate entries in protoNodeIdToGNode mappings. protoNodeIds: $intersection1"
27-
)
28-
assert(
29-
intersection2.isEmpty,
30-
s"unexpected duplicate entries in typeFullNameToGNode mappings. FullNames: $intersection2"
31-
)
32-
33-
this.protoNodeIdToGNode = this.protoNodeIdToGNode ++ other.protoNodeIdToGNode
34-
this.typeFullNameToGNode = this.typeFullNameToGNode ++ other.typeFullNameToGNode
35-
}
19+
private val protoNodeIdToGNode = mutable.LongMap.empty[DNode]
20+
private val typeFullNameToGNode = mutable.Map.empty[String, DNode]
3621

3722
def add(protoNode: CpgStruct.Node, node: DNode): Unit = {
3823
protoNodeIdToGNode += protoNode.getKey -> node
@@ -48,7 +33,7 @@ class ProtoToGraphNodeMappings {
4833
}
4934
}
5035

51-
/** This will fail hard if the DiffGraph hasn't been applied yet, which is the assumption for it's use case. In other
36+
/** This will fail hard if the DiffGraph hasn't been applied yet, which is the assumption for its use case. In other
5237
* words, we specifically don't want to invoke `find(protoNode).flatMap(_.storedRef)` here
5338
*/
5439
def findGNode(protoNode: CpgStruct.Node): Option[GNode] =

0 commit comments

Comments
 (0)