Create 1D array of objects
How can I create a strcut which can create a 1-D array for HuffmanEntry shown in the example.
The copyinit function doesn't allow me to store data and gives the following error:
11 Replies
Here's how I did. There's a register_passable thing in mojo:
Hey, what prevents you from using
DynamicVectors
?
You could also use @value
and leave out the constructor all together@sora I was using Dynamic Vectors previously but I was getting this issue. Here I've created an example code:
In the above code, the copyinit function is called 6 times. 3 times when I use the append function (which is completely fine).
But when I send the DynamicVector "c" ti the nxt function i.e., newfunc2 and update the member variable "sz" , the copy constructor is called again. Why is it the case. According to my understanding when you use "in out" with the argument, the original object is sent to the function as a reference and not the copy of that object.
Although the DynamicVector "c" is updated outside the "newfunc2" but why is it the case that new "HuffmanEntry" object is being created while updating the "sz" argument of the object which is already stored in vector c? And How to avoid this i.e., update the object store within the dynamic vector instead of creating a new object?
@sora This is the output for reference:
Hmmm, it is indeed curious and surprising. Maybe you could reduce the example and write bug report?
sure thanks
Actually the issue is that I'm creating Jpeg decoder in mojo and I was comparing it with C and python. Using the perf profilling output I see the following:
I.e., almost 87 percent of the time is being taken by the copy constructor because it is being invoked again and again when I try to update anything inside Dynamic Vecotrs 😦
I can't make sense of it and i think it's a bug
BTW, how did you profile your code?
There's a tool named perf
It's pretty easy to use.
just do
And it records the time taken inside each function.
To view the output as shown above in the ss:
And you can download it using apt install perf. Maybe you might need 1-2 more dependencies which can easily be found on web
Ah, thanks for sharing your workflow!
@Chris Lattner Sorry for pinging. Could you make sense of this example, and is it a bug that needs to be filed?
Plus it is not only the case when I update the Dynamic Vector of objects but it also creates new objects using the copy constructor even if I try to access any specific index. e.g.,
or also invokes the copy constructors.
And it has nothing to do with weather the vector is passed to a function via reference or not. Even If I access the vector of objects in the same scope where it's created, copy constructor is called.
I tried to compare it with the C++ vectors to see if Dynamic Vectors are equivalent to vectors in C++:
Both printf("%d\n", myObjects[0].getData()) and printData function doesnot invoke any constructor which makes sense.