-
Notifications
You must be signed in to change notification settings - Fork 164
chore: optimize Request.read #1410
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
45f32ab
cd73d07
e433734
40c7ed2
3d6c280
3a962f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,7 +93,7 @@ class Request { | |
|
||
Request._(this._request); | ||
|
||
shelf.Request _request; | ||
final shelf.Request _request; | ||
|
||
/// Connection information for the associated HTTP request. | ||
HttpConnectionInfo get connectionInfo { | ||
|
@@ -120,16 +120,12 @@ class Request { | |
|
||
/// Returns a [Future] containing the body as a [String]. | ||
Future<String> body() async { | ||
const requestBodyKey = 'dart_frog.request.body'; | ||
final bodyFromContext = | ||
_request.context[requestBodyKey] as Completer<String>?; | ||
if (bodyFromContext != null) return bodyFromContext.future; | ||
final bodyFromContext = _bodyFutureExpando[_request]; | ||
if (bodyFromContext != null) return bodyFromContext; | ||
|
||
final completer = Completer<String>(); | ||
try { | ||
_request = _request.change( | ||
context: {..._request.context, requestBodyKey: completer}, | ||
); | ||
_bodyFutureExpando[_request] = completer.future; | ||
completer.complete(await _request.readAsString()); | ||
} catch (error, stackTrace) { | ||
completer.completeError(error, stackTrace); | ||
|
@@ -158,3 +154,5 @@ class Request { | |
return Request._(_request.change(headers: headers, path: path, body: body)); | ||
} | ||
} | ||
|
||
final _bodyFutureExpando = Expando<Future<String>>('dart_frog.request.body'); | ||
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. Not that familiar with Expandos, would love to see a description on the PR to explain how this improves performance 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. You all would have to measure it. On the VM, they're pretty cheap. And now you're avoiding copying everything all the time which should be a lot cheaper |
Uh oh!
There was an error while loading. Please reload this page.