Whiteknight / ParserObjects

C# library for parser combinators
https://whiteknight.github.io/ParserObjects
Apache License 2.0
6 stars 0 forks source link

Reevaluate IResult.TryGetData() #193

Closed Whiteknight closed 12 months ago

Whiteknight commented 1 year ago

The .TryGetData method requires many results to include a newly-allocated array or list of objects, but the user frequently will not access this information. This means that we are allocating a new array when we allocate a new result (in parsers which use result data). but because IResult is not disposable (and I do not want to add and enforce it) we cannot rent these data arrays from the ArrayPool because there's no way to return them when we're done.

We could create a SuccessResult variant which takes a single object, and returns that object if it is T. If all our parsers which use result data return only a single item, this might work.

We could remove .TryGetData() entirely. Parsers which need to return data can instead get a variant which returns it. For example, IParser<char, string> Regex() could add a variant IParser<char, RegexMatch> RegexCapture().

There's a lot of extra allocations flying around here, and I want to make sure we keep them under control.

Whiteknight commented 1 year ago

I have made a number of improvements to this mechanism for v5, but the reality is that very few parsers make use of it. I will continue to keep an eye on it, and consider removing it for subsequent releases.

Whiteknight commented 12 months ago

SuccessResult takes a ResultData struct, which wraps an object. The object can either be a single data item or an array of objects of other data items to search through. In either case, this mechanism is pretty speedy and I don't think it requires further reevaluation at this time.