-
Notifications
You must be signed in to change notification settings - Fork 25
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
fix: make atto parser faster than the scala parser combinator one #586
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,33 @@ package cron4s | |
import _root_.atto._ | ||
import Atto._ | ||
import cats.implicits._ | ||
|
||
import cron4s.expr._ | ||
|
||
package object atto { | ||
import CronField._ | ||
import CronUnit._ | ||
|
||
private val sexagesimal: Parser[Int] = int.filter(x => x >= 0 && x < 60) | ||
private val decimal: Parser[Int] = int.filter(x => x >= 0) | ||
private val literal: Parser[String] = takeWhile1(_ >= ' ') | ||
private def oneOrTwoDigitsPositiveInt: Parser[Int] = { | ||
|
||
val getDigits = for { | ||
d1 <- digit | ||
d2 <- opt(digit) | ||
} yield d2.fold(s"$d1")(x => s"$d1$x") | ||
|
||
getDigits.flatMap(s => | ||
try | ||
ok(s.toInt) | ||
catch { | ||
// scala-js can't parse non-alpha digits so we just fail in that case. | ||
case _: java.lang.NumberFormatException => | ||
err[Int]("https://github.com/scala-js/scala-js/issues/2935") | ||
} | ||
) | ||
} namedOpaque "oneOrTwoDigitsPositiveInt" | ||
|
||
private val sexagesimal: Parser[Int] = oneOrTwoDigitsPositiveInt.filter(x => x >= 0 && x < 60) | ||
|
||
private val literal: Parser[String] = takeWhile1(x => x != ' ' && x != '-') | ||
|
||
private val hyphen: Parser[Char] = elem(_ == '-', "hyphen") | ||
private val comma: Parser[Char] = elem(_ == ',', "comma") | ||
|
@@ -54,17 +71,17 @@ package object atto { | |
// Hours | ||
|
||
val hours: Parser[ConstNode[Hour]] = | ||
decimal.filter(x => (x >= 0) && (x < 24)).map(ConstNode[Hour](_)) | ||
oneOrTwoDigitsPositiveInt.filter(x => (x >= 0) && (x < 24)).map(ConstNode[Hour](_)) | ||
|
||
// Days Of Month | ||
|
||
val daysOfMonth: Parser[ConstNode[DayOfMonth]] = | ||
decimal.filter(x => (x >= 1) && (x <= 31)).map(ConstNode[DayOfMonth](_)) | ||
oneOrTwoDigitsPositiveInt.filter(x => (x >= 1) && (x <= 31)).map(ConstNode[DayOfMonth](_)) | ||
|
||
// Months | ||
|
||
private[this] val numericMonths = | ||
decimal.filter(_ <= 12).map(ConstNode[Month](_)) | ||
oneOrTwoDigitsPositiveInt.filter(x => (x >= 0) && (x <= 12)).map(ConstNode[Month](_)) | ||
|
||
private[this] val textualMonths = | ||
literal.filter(Months.textValues.contains).map { value => | ||
|
@@ -78,7 +95,7 @@ package object atto { | |
// Days Of Week | ||
|
||
private[this] val numericDaysOfWeek = | ||
decimal.filter(_ < 7).map(ConstNode[DayOfWeek](_)) | ||
oneOrTwoDigitsPositiveInt.filter(x => (x >= 0) && (x <= 6)).map(ConstNode[DayOfWeek](_)) | ||
|
||
private[this] val textualDaysOfWeek = | ||
literal.filter(DaysOfWeek.textValues.contains).map { value => | ||
|
@@ -111,9 +128,10 @@ package object atto { | |
unit: CronUnit[F] | ||
): Parser[SeveralNode[F]] = { | ||
def compose(b: => Parser[EnumerableNode[F]]) = | ||
sepBy1(b, comma) | ||
.filter(_.size > 1) | ||
.map(values => SeveralNode.fromSeq[F](values.toList).get) | ||
sepBy(b, comma) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were actually checking the size several times, now it's done a single time |
||
.collect { | ||
case first :: second :: tail => SeveralNode(first, second, tail: _*) | ||
} | ||
|
||
compose(between(base).map(between2Enumerable) | base.map(const2Enumerable)) | ||
} | ||
|
@@ -122,7 +140,7 @@ package object atto { | |
unit: CronUnit[F] | ||
): Parser[EveryNode[F]] = { | ||
def compose(b: => Parser[DivisibleNode[F]]) = | ||
((b <~ slash) ~ decimal.filter(_ > 0)).map { | ||
((b <~ slash) ~ oneOrTwoDigitsPositiveInt.filter(_ > 0)).map { | ||
case (exp, freq) => EveryNode[F](exp, freq) | ||
} | ||
|
||
|
@@ -166,8 +184,9 @@ package object atto { | |
} yield CronExpr(sec, min, hour, day, month, weekDay) | ||
|
||
def parse(e: String): Either[Error, CronExpr] = | ||
(cron.parseOnly(e): @unchecked) match { | ||
(phrase(cron).parseOnly(e): @unchecked) match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a correctness fix : it ensure the string is parsed entirely |
||
case ParseResult.Done(_, result) => Right(result) | ||
case ParseResult.Fail("", _, _) => Left(ExprTooShort) | ||
case ParseResult.Fail(rest, _, msg) => | ||
val position = e.length() - rest.length() + 1 | ||
Left(ParseFailed(msg, position, Some(rest))) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the optimization that makes it much faster