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

Improve merging of Char parsers #291

Merged
merged 2 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/js/src/main/scala/cats/parse/BitSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ object BitSetUtil {
def toIter(m: Int, bs: BitSet): Iterator[Char] =
bs.iterator.map { i => (i + m).toChar } ++ Iterator.single(m.toChar)

bs.flatMap { case (m, bs) => toIter(m, bs) }
bs.iterator.flatMap { case (m, bs) => toIter(m, bs) }.toSet
}
}
2 changes: 1 addition & 1 deletion core/jvm/src/main/scala/cats/parse/BitSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ object BitSetUtil {
.takeWhile(_ >= 0)
.map { i => (m + i).toChar }

bs.flatMap { case (m, bs) => toIter(m, bs) }
bs.iterator.flatMap { case (m, bs) => toIter(m, bs) }.toSet
}
}
16 changes: 11 additions & 5 deletions core/shared/src/main/scala/cats/parse/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2603,11 +2603,17 @@ object Parser {
*/
def mergeCharIn[A, P0 <: Parser0[A]](ps: List[P0]): List[P0] = {
@annotation.tailrec
def loop(ps: List[P0], front: List[(Int, BitSetUtil.Tpe)], result: Chain[P0]): Chain[P0] = {
def loop(ps: List[P0], front: List[CharIn], result: Chain[P0]): Chain[P0] = {
@inline
def frontRes: Chain[P0] =
if (front.isEmpty) Chain.nil
else Chain.one(Parser.charIn(BitSetUtil.union(front)).asInstanceOf[P0])
front match {
case Nil => Chain.nil
case one :: Nil => Chain.one(one.asInstanceOf[P0])
case many =>
// we need to union
val minBs: List[(Int, BitSetUtil.Tpe)] = many.map { case CharIn(m, bs, _) => (m, bs) }
Chain.one(Parser.charIn(BitSetUtil.union(minBs)).asInstanceOf[P0])
Comment on lines +2614 to +2615
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we have a version of union with Iterator and avoid the copy in minBs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we certainly could. I wasn't sure it was worth it because we already iterate through the number of parsers regularly, but the characters inside the parsers (which we have to iterate when allocating a new CharIn to compute the ranges) could be more expensive, which was what I was targeting here.

Do you want me to address this before merging?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can address this as a follow-up for sure!

}

ps match {
case Nil => result ++ frontRes
Expand All @@ -2616,8 +2622,8 @@ object Parser {
// and any direct prefix CharIns
val tail1 = tail.filterNot(_.isInstanceOf[CharIn])
(result :+ AnyChar.asInstanceOf[P0]) ++ Chain.fromSeq(tail1)
case CharIn(m, bs, _) :: tail =>
loop(tail, (m, bs) :: front, result)
case (ci: CharIn) :: tail =>
loop(tail, ci :: front, result)
case h :: tail =>
// h is not an AnyChar or CharIn
// we make our prefix frontRes
Expand Down