question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

new PooledList<PlayerState>(Players.Count, preallocate : true)

See original GitHub issue

Is your feature request related to a problem? Please describe. Zero overhead structs clone.

Describe the solution you’d like

            PooledList<PlayerState> players = null;
            if (Players != null)
            {
                players = new PooledList<PlayerState>(Players.Count, preallocate : true); // i guess preallocated just sets _size = capacity;
                for (var i = 0; i < players.Count; i++)
                {
                    PlayerState.Clone(in Players.Span[i], ref players.Span[i]);
                }
            }

Describe alternatives you’ve considered

            PooledList<PlayerState> players = null;
            if (Players != null)
            {
                players = new PooledList<PlayerState>(Players.Count);
                for (var i = 0; i < players.Count; i++)
                {
                    // Clone will fail until
                    players[i] = default; // overhead to create 200+ byte struct on stack and pass it into indexer (not sure if optimized away by compilers?)
                    PlayerState.Clone(in Players.Span[i], ref players.Span[i]);
                }
            }

Additional context Usual List could not do that because it would leak abstraction (that it is actually array list). PooledList already leaks Span as property and ArrayPool as ctor arg(also ctor arg is no of so problem). So adding that method will not increase leakage, but will allow more performance uses for structs.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jtmuellercommented, Mar 28, 2019

Technically you don’t have to pass the count 2 times: just don’t pass anything into the constructor, because one line later you’re setting the capacity. But I get that two lines of code isn’t as nice as one line of code, and maybe adding more constructor overloads would be worth it.

However, there’s another thing to consider when pre-allocating that your sample code doesn’t account for. What if the array we get from the pool already has values in it? In the current published code this doesn’t matter, because the only way to move the internal _size pointer is to overwrite whatever is currently in the array we rented.

By using preallocate to directly move the _size pointer, we’re exposing the random contents of pooled arrays to users who are probably expecting a newly-created list to only have default values in it. This probably means that when preallocate == true we should clear the array we just rented. Do you see this as a performance problem?

0reactions
jtmuellercommented, Mar 29, 2019

I like your initial proposal. I think it’s better than the alternative. If you’re willing to submit a PR for it, I would welcome that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Average daily player count is in freefall. How bad does it ...
So looking at the average daily player numbers and total player counts gains and losses is pretty terrible and I wonder how bad...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found