Reputation: 5749
I have data structures that are very large and need to be assigned and passed around. I also need to get inside the list. Sometimes when a list is in a scalar container, $aList.elems
will say 1
because there is only one element which is the list. To get inside the list, (@$aList).elems
will give the correct number of elements in the list.
My question is: is there a performance disadvantage with using @$aList
frequently, and if there is a performance issue, will assigning @b = @$aList
and use @b
instead solve the issue? I.e., will there be speed bump with switching from list to array context?
Thanks.
Upvotes: 3
Views: 107
Reputation: 5791
$x.elems
and (@$x).elems
should return the same number.
So it sounds like there's a bug in your code that you should fix... Possibly something related to Seq
caching, see below.
As for the performance question, I haven't run benchmarks, but note that @$x
simply calls $x.cache
, which...
$x
is a List
or subclass (which includes Array
), simply returns the object itself (without the surrounding item container), which should be fast.$x
is an object of another type, creates a new List
from it and returns that, which will likely have a bit more overhead depending on the type. e.g.:
"a"
--> List ("a",)
1..10
--> List (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
(1, 2).Seq
--> List (1, 2)
List
object that's created the first time .cache
is called on it, and continues to return the same one on subsequent calls. That's why the method is called .cache
in the first place.(Note that the .list
method also exists, which does the same thing as .cache
except that it doesn't remember the List in the Seq
case.)
Upvotes: 5