-
Notifications
You must be signed in to change notification settings - Fork 48
inline some functions used in parsing #829
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
Draft
pjfanning
wants to merge
3
commits into
apache:main
Choose a base branch
from
pjfanning:inline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
23 changes: 23 additions & 0 deletions
23
http-core/src/main/mima-filters/2.0.x.backwards.excludes/inline-utils.excludes
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
# inline some parsing utils | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.http.impl.engine.parsing.package.asciiString") | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.http.impl.engine.parsing.package.byteAt") | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.http.impl.engine.parsing.package.byteChar") | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.http.impl.engine.parsing.package.escape") | ||
ProblemFilters.exclude[DirectMissingMethodProblem]("org.apache.pekko.http.impl.engine.parsing.package.logParsingError") |
This file contains hidden or 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
31 changes: 31 additions & 0 deletions
31
http-core/src/main/scala-2.13/org/apache/pekko/http/impl/util/CharUtils.scala
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko.http.impl.util | ||
|
||
import org.apache.pekko | ||
import pekko.annotation.InternalApi | ||
|
||
@InternalApi | ||
private[http] object CharUtils { | ||
|
||
/** | ||
* Internal Pekko HTTP Use only. | ||
* | ||
* Efficiently lower-cases the given character. | ||
* Note: only works for 7-bit ASCII letters (which is enough for header names) | ||
*/ | ||
@inline def toLowerCase(c: Char): Char = | ||
if (c >= 'A' && c <= 'Z') (c + 0x20 /* - 'A' + 'a' */ ).toChar else c | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
http-core/src/main/scala-3/org/apache/pekko/http/impl/engine/package.scala
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko.http.impl.engine | ||
|
||
import java.lang.{ StringBuilder => JStringBuilder } | ||
|
||
import org.apache.pekko | ||
import pekko.event.LoggingAdapter | ||
import pekko.http.scaladsl.model.ErrorInfo | ||
import pekko.http.scaladsl.settings.ParserSettings | ||
import pekko.util.ByteString | ||
|
||
import scala.annotation.tailrec | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
package object parsing { | ||
|
||
private[http] inline def escape(c: Char): String = c match { | ||
case '\t' => "\\t" | ||
case '\r' => "\\r" | ||
case '\n' => "\\n" | ||
case x if Character.isISOControl(x) => "\\u%04x".format(c.toInt) | ||
case x => x.toString | ||
} | ||
|
||
/** | ||
* Like `byteChar` but doesn't throw `NotEnoughDataException` if the index is out of bounds. | ||
* Used in places where we know that the index is valid because we checked the length beforehand. | ||
*/ | ||
private[http] inline def safeByteChar(input: ByteString, ix: Int): Char = | ||
(input(ix) & 0xFF).toChar | ||
|
||
private[http] inline def byteChar(input: ByteString, ix: Int): Char = (byteAt(input, ix) & 0xFF).toChar | ||
|
||
private[http] inline def byteAt(input: ByteString, ix: Int): Byte = | ||
if (ix < input.length) input(ix) else throw NotEnoughDataException | ||
|
||
private[http] inline def asciiString(input: ByteString, start: Int, end: Int): String = { | ||
@tailrec def build(ix: Int = start, sb: JStringBuilder = new JStringBuilder(end - start)): String = | ||
if (ix == end) sb.toString else build(ix + 1, sb.append(input(ix).toChar)) | ||
if (start == end) "" else build() | ||
} | ||
|
||
private[http] inline def logParsingError(info: ErrorInfo, log: LoggingAdapter, | ||
settings: ParserSettings.ErrorLoggingVerbosity, | ||
ignoreHeaderNames: Set[String] = Set.empty): Unit = | ||
settings match { | ||
case ParserSettings.ErrorLoggingVerbosity.Off => // nothing to do | ||
case ParserSettings.ErrorLoggingVerbosity.Simple => | ||
if (!ignoreHeaderNames.contains(info.errorHeaderName)) | ||
log.warning(info.summary) | ||
case ParserSettings.ErrorLoggingVerbosity.Full => | ||
if (!ignoreHeaderNames.contains(info.errorHeaderName)) | ||
log.warning(info.formatPretty) | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
http-core/src/main/scala/org/apache/pekko/http/impl/engine/parsing/ParsingException.scala
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* license agreements; and to You under the Apache License, version 2.0: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* This file is part of the Apache Pekko project, which was derived from Akka. | ||
*/ | ||
|
||
/* | ||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package org.apache.pekko.http.impl.engine | ||
package parsing | ||
|
||
import org.apache.pekko | ||
import pekko.annotation.InternalApi | ||
import pekko.http.impl.util.SingletonException | ||
import pekko.http.scaladsl.model.{ ErrorInfo, StatusCode, StatusCodes } | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[parsing] class ParsingException( | ||
val status: StatusCode, | ||
val info: ErrorInfo) extends RuntimeException(info.formatPretty) { | ||
def this(status: StatusCode, summary: String) = | ||
this(status, ErrorInfo(if (summary.isEmpty) status.defaultMessage else summary)) | ||
def this(summary: String) = | ||
this(StatusCodes.BadRequest, ErrorInfo(summary)) | ||
def this(summary: String, detail: String) = | ||
this(StatusCodes.BadRequest, ErrorInfo(summary, detail)) | ||
} | ||
|
||
/** | ||
* INTERNAL API | ||
*/ | ||
@InternalApi | ||
private[parsing] object NotEnoughDataException extends SingletonException |
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.
@jrudolph thanks for the review. I can look at doing a jmh benchmark but it will take me a week or 2 to get back to it.
One question though: do you think the safeByteChar function is a useful change instead of having a few places in our code where we do the masking in place while usually, we call the byteChar function below? There are places where we know it safe so that we don't need to waste a call on checking the index is in bounds.