performance - Passing Scala Map as an argument to a function takes too much time -
i have scala companion object method accepts map parameter. in passes map function in different companion object no changes. , actual method call takes time when method execution fast (i measured everything). if don't pass map (use null instead) works fast, passing argument, actual method call slow.
am missing something, , map being recreated , not reference passed?
object contentelementparser { def parse(node: node, assets: map[string, asset]): option[contentelement] = { //some logic here assetparser.getasset(subnode, assets) //this call slow because of assets map } } object assetparser { def getasset(node: node, assetmap: map[string, asset]): asset = { //logic } }
it's being passed reference. else going on--you're measuring first time use map, requires class loading (subsequent calls faster), or you're doing lots more work when pass map opposed null
, or out of memory , you're measuring garbage collection time instead.
it copied if there implicit conversion in scope, if type signature same in both places, wouldn't issue since "no conversion" has priority.
here bytecode parse
call (with content
method added asset
produces option[contentelement]
, , sub
method added node
fill in subnode
):
def parse(node: node, assets: map[string, asset]): option[contentelement] = assetparser.getasset(node.sub, assets).content public scala.option parse(node, scala.collection.immutable.map); code: 0: getstatic #19; //field assetparser$.module$:lassetparser$; 3: aload_1 4: invokevirtual #25; //method node.sub:()lnode; 7: aload_2 8: invokevirtual #29; //method assetparser$.getasset: (lnode;lscala/collection/immutable/map;)lasset; 11: invokevirtual #35; //method asset.content:()lscala/some; 14: areturn
see? no map copying. aload_2
map that's passed in. nothing happens except it's passed on getasset
via invokevirtual
.
Comments
Post a Comment