m4rw3r / chomp

A fast monadic-style parser combinator designed to work on stable Rust.
Apache License 2.0
243 stars 19 forks source link

Size hint for internal iterator used by `many*` and `count` #24

Closed m4rw3r closed 8 years ago

m4rw3r commented 8 years ago

Implementing Iterator::size_hint will enable more efficient allocation for the containers. According to profiling a lot of time is spent allocating and reallocating in some situations, a better size_hint would improve performance.

It might also be feasible to implement a combinator which is a hybrid of count and many, where specifying both a lower and upper bound is possible. This would make it a lot more efficient to allocate some parts (and by using monadic composition it can even reserve space for a certain known number of elements which was specified earlier in the message).

Spec for bounded many

fn many(Input<I>, R, Parser) -> ParseResult<I, T, E>
  where R:      BoundedRange,
        Parser: FnMut(Input<I>) -> ParseResult<I, U, E>,
        T:      FromIterator<U>

trait BoundedRange { ... }

impl BoundedRange for Range { ... }
impl BoundedRange for RangeFull { ... }
impl BoundedRange for RangeFrom { ... }
impl BoundedRange for RangeTo { ... }
shepmaster commented 8 years ago

Aww, did you decide this wasn't feasible?

m4rw3r commented 8 years ago

It is merged in a local copy of master which I have not yet pushed, along with #18. Currently I am polishing some things and updating all the doctests to use parse_only instead of Input::new and unwrap. So it is not gone at all, just that the feature itself is done :)

The performance is promising, equal or slightly better performance in most cases except for in very small benchmarks.

shepmaster commented 8 years ago

It is merged in a local copy of master which I have not yet pushed,

Ah, gotcha. I'm just so used to seeing issues automatically closed by Github when the corresponding commit is pushed to master. When I didn't see the "X closed by Y" message, I just assumed that meant that it couldn't be done.

The performance is promising

Great!

m4rw3r commented 8 years ago

@shepmaster You can try it out now if you want, 0.2.0 is out :)