Hey @ilx, I’m going to link you to something you may find interesting: How to use toOwnedSlice() and alternatives That topic went into a lot of details about using ArrayList
effectively. I think you may find it interesting.
I’ll just add two cents here that may also help. First, you can do little things to squeeze a bit more performance out:
var todo_list = ArrayList(TodoItem).init(allocator);
One thing that immediately pops out is that you can use initCapacity
instead of standard init
to give yourself some more initial room to work with.
Another thing is the ordered remove:
_ = self.todo_list.orderedRemove(idx);
I can’t speak for your use case, but if order doesn’t matter, you can use swapRemove
which is much faster. I can imagine that the order of tasks may actually be important. Which actually brings up the issue of “know thy problem”.
Let’s say that I have a list of tasks that I need to complete and I know that I’ll never do more than say 20 per day and they have to be in order. I could start by initializing 20 tasks and put them in the ArrayList
in reverse. The reason for the reverse is that I can now pop
them off the end of the list without needing to shuffle anything around. That advice only works though if I know that my problem allows me to make those assumptions.
I’d also like to point out that the allocator parameter depends on whether or not you actually need to allocate. In the code for json
, it determines if an allocation is required (it’s passed options like alloc_if_needed
).
Just some food for thought.