Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToList as Memory<T> #17

Closed
wants to merge 3 commits into from
Closed

ToList as Memory<T> #17

wants to merge 3 commits into from

Conversation

clipperhouse
Copy link
Owner

@clipperhouse clipperhouse commented Jun 21, 2024

Before, we’d allocate a new List, and then call ToArray on each token. The List is new, and each ToArray is a copy of the ReadOnlySpan representing the token.

In this PR, we clone the original input, and create Memory pointing to each token.

We want the semantics that by calling ToList, the new is now entirely independent of the original input. Copies in all cases. This is to avoid gotchas where the underlying original data may be mutated. I think this is what users expect.

@clipperhouse
Copy link
Owner Author

@kevin-montrose @bretcope I don’t know if this is better, see description above. Is it better?

@clipperhouse
Copy link
Owner Author

A little faster, but more allocation:

after: 

| Method              | Mean       | Error    | StdDev   | Gen0     | Gen1     | Gen2     | Allocated   |
| ToList              | 1,484.0 us | 15.28 us | 14.29 us | 382.8125 | 373.0469 | 248.0469 | 2,155,937 B |

before:

| Method              | Mean       | Error    | StdDev   | Gen0     | Gen1     | Gen2     | Allocated   |
| ToList              | 1,847.3 us | 6.59 us  | 5.84 us  | 234.3750 | 201.1719 | 105.4688 | 1,522,633 B |

@clipperhouse
Copy link
Owner Author

Calling it: never mind, given the above results and the simplicity of the original version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant