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

Polled SSR Stream #2824

Merged
merged 32 commits into from
Sep 10, 2022
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5bf1ae8
Switch to pinned channels.
futursolo Aug 4, 2022
fb2603e
Fix ServerRenderer so it's not blocked until the result is resolved.
futursolo Aug 5, 2022
892b759
Fix tests.
futursolo Aug 5, 2022
29a46bd
Remove unused SendError.
futursolo Aug 5, 2022
20fc4a3
Merge branch 'master' into pinned-channels
futursolo Aug 7, 2022
8a41c3e
Implement a stream to be polled alongside rendering.
futursolo Aug 11, 2022
8688045
Update Buffer Size.
futursolo Aug 11, 2022
3bd9e0a
Make Send renderer work.
futursolo Aug 11, 2022
ed36760
Remove pinned channels.
futursolo Aug 11, 2022
2799a22
Unified Naming.
futursolo Aug 11, 2022
02a838d
Optimise code.
futursolo Aug 12, 2022
3de0770
Restore capacity.
futursolo Aug 12, 2022
689980e
merge 'master' into polled-stream
futursolo Aug 12, 2022
cbbd765
Remove unused profile.
futursolo Aug 12, 2022
2045c49
Merge branch 'master' into polled-stream
futursolo Aug 12, 2022
a470862
Default to separate resolver.
futursolo Aug 12, 2022
3ac02fd
Reduce allocations on string.
futursolo Aug 12, 2022
345b745
Adjust API.
futursolo Aug 13, 2022
d1a6ab3
Remove duplicate trait bound.
futursolo Aug 13, 2022
f0863a4
Update docs.
futursolo Aug 13, 2022
7dcaf6e
Remove capacity setting.
futursolo Aug 13, 2022
ced12ac
Merge branch 'master' into polled-stream
futursolo Aug 14, 2022
475f52e
Merge branch 'master' into polled-stream
futursolo Aug 16, 2022
e757d35
Unsafe?
futursolo Aug 16, 2022
0a399ba
Separate files.
futursolo Aug 16, 2022
6ae5a65
Adjust inlining.
futursolo Aug 16, 2022
f310575
Merge branch 'master' into polled-stream
futursolo Aug 29, 2022
e18e0fd
Fix test.
futursolo Aug 29, 2022
67348d3
Update notice.
futursolo Aug 29, 2022
f491d6c
Update documentation.
futursolo Sep 4, 2022
2a300fe
Merge branch 'master' into polled-stream
futursolo Sep 4, 2022
a78f0b3
Fix tests.
futursolo Sep 4, 2022
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
Prev Previous commit
Next Next commit
Adjust API.
futursolo committed Aug 13, 2022
commit 345b74540e41098e091d3f03bba45cac118ad5a8
113 changes: 56 additions & 57 deletions packages/yew/src/platform/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This module contains types for I/O functionality.
//! Asynchronous utilities to work with `String`s.

use std::cell::RefCell;
use std::fmt::{self, Write};
@@ -25,22 +25,26 @@ struct BufStreamInner {
}

impl BufStreamInner {
#[inline]
const fn new() -> Self {
Self {
buf: String::new(),
state: BufStreamState::Ready,
}
}

#[inline]
fn wake(&self) {
if let BufStreamState::Pending(ref waker) = self.state {
waker.wake_by_ref();
}
}

fn finish(&mut self) {
self.wake();
self.state = BufStreamState::Done;
#[inline]
fn try_reserve(&mut self, capacity: usize) {
if self.buf.is_empty() {
self.buf.reserve(capacity);
}
}
}

@@ -50,7 +54,8 @@ pub(crate) struct BufWriter {
}

impl BufWriter {
pub fn capacity(&self) -> usize {
#[inline]
pub const fn capacity(&self) -> usize {
self.capacity
}
}
@@ -62,67 +67,60 @@ impl Write for BufWriter {
}

let mut inner = self.inner.borrow_mut();
inner.wake();

if inner.buf.is_empty() {
inner.buf.reserve(self.capacity);
}
inner.wake();
inner.try_reserve(self.capacity);

inner.buf.write_str(s)
}

fn write_char(&mut self, c: char) -> fmt::Result {
let mut inner = self.inner.borrow_mut();
inner.wake();

if inner.buf.is_empty() {
inner.buf.reserve(self.capacity);
}
inner.wake();
inner.try_reserve(self.capacity);

inner.buf.write_char(c)
}

fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
let mut inner = self.inner.borrow_mut();

if inner.buf.is_empty() {
inner.buf.reserve(self.capacity);
}
inner.wake();
inner.try_reserve(self.capacity);

inner.buf.write_fmt(args)
}
}

pub(crate) struct BufStream {
inner: Rc<RefCell<BufStreamInner>>,
}
impl Drop for BufWriter {
fn drop(&mut self) {
let mut inner = self.inner.borrow_mut();

impl BufStream {
pub fn new<C, F>(capacity: usize, f: C) -> (BufStream, impl Future<Output = ()>)
where
C: FnOnce(BufWriter) -> F,
F: Future<Output = ()>,
{
let inner = Rc::new(RefCell::new(BufStreamInner::new()));

let resolver = {
let inner = inner.clone();
let w = {
let inner = inner.clone();
BufWriter { inner, capacity }
};

async move {
f(w).await;
inner.borrow_mut().finish();
}
};

(Self { inner }, resolver)
inner.wake();
inner.state = BufStreamState::Done;
}
}

impl Stream for BufStream {
/// Creates an asynchronous buffer that operates over String.
pub(crate) fn buffer(capacity: usize) -> (BufWriter, BufReader) {
let inner = Rc::new(RefCell::new(BufStreamInner::new()));

let w = {
let inner = inner.clone();
BufWriter { inner, capacity }
};

let r = BufReader { inner };

(w, r)
}

pub(crate) struct BufReader {
inner: Rc<RefCell<BufStreamInner>>,
}

impl Stream for BufReader {
type Item = String;

fn poll_next(
@@ -132,9 +130,7 @@ impl Stream for BufStream {
let mut inner = self.inner.borrow_mut();

if !inner.buf.is_empty() {
let mut buf = String::new();
std::mem::swap(&mut buf, &mut inner.buf);

let buf = std::mem::take(&mut inner.buf);
return Poll::Ready(Some(buf));
}

@@ -147,7 +143,7 @@ impl Stream for BufStream {
}
}

impl FusedStream for BufStream {
impl FusedStream for BufReader {
fn is_terminated(&self) -> bool {
let inner = self.inner.borrow();

@@ -158,35 +154,38 @@ impl FusedStream for BufStream {
}
}

/// A buffered asynchronous string Stream.
///
/// This combines a BufWriter - BufReader pair and a resolving future.
/// The resoloving future will be polled as the stream is polled.
#[pin_project]
pub(crate) struct ResolvedBufStream<F>
pub(crate) struct BufStream<F>
where
F: Future<Output = ()>,
{
#[pin]
resolver: MaybeDone<F>,
inner: BufStream,
inner: BufReader,
}

impl<F> ResolvedBufStream<F>
impl<F> BufStream<F>
where
F: Future<Output = ()>,
{
pub fn new<C>(capacity: usize, f: C) -> ResolvedBufStream<impl Future<Output = ()>>
/// Creates a `BufStream`.
pub fn new<C>(capacity: usize, f: C) -> Self
where
C: FnOnce(BufWriter) -> F,
F: Future<Output = ()>,
{
let (inner, resolver) = BufStream::new(capacity, f);
let (w, r) = buffer(capacity);
let resolver = future::maybe_done(f(w));

ResolvedBufStream {
inner,
resolver: future::maybe_done(resolver),
}
BufStream { inner: r, resolver }
}
}

impl<F> Stream for ResolvedBufStream<F>
impl<F> Stream for BufStream<F>
where
F: Future<Output = ()>,
{
@@ -204,7 +203,7 @@ where
}
}

impl<F> FusedStream for ResolvedBufStream<F>
impl<F> FusedStream for BufStream<F>
where
F: Future<Output = ()>,
{
4 changes: 2 additions & 2 deletions packages/yew/src/server_renderer.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use futures::stream::{Stream, StreamExt};
use tracing::Instrument;

use crate::html::{BaseComponent, Scope};
use crate::platform::fmt::{ResolvedBufStream, DEFAULT_BUF_SIZE};
use crate::platform::fmt::{BufStream, DEFAULT_BUF_SIZE};
use crate::platform::{run_pinned, spawn_local};

/// A Yew Server-side Renderer that renders on the current thread.
@@ -105,7 +105,7 @@ where
let scope = Scope::<COMP>::new(None);

let outer_span = tracing::Span::current();
ResolvedBufStream::new(self.capacity, move |mut w| async move {
BufStream::new(self.capacity, move |mut w| async move {
let render_span = tracing::debug_span!("render_stream_item");
render_span.follows_from(outer_span);
scope
16 changes: 8 additions & 8 deletions packages/yew/src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ mod feat_ssr {

use super::*;
use crate::html::AnyScope;
use crate::platform::fmt::{BufStream, BufWriter};
use crate::platform::fmt::{self, BufWriter};

impl VList {
pub(crate) async fn render_into_stream(
@@ -178,20 +178,20 @@ mod feat_ssr {
child.render_into_stream(w, parent_scope, hydratable).await;
}
[first_child, rest_children @ ..] => {
let capacity = w.capacity();
let buf_capacity = w.capacity();
let mut child_streams = Vec::with_capacity(self.children.len() - 1);

// Concurrently render rest children into a separate buffer.
let rest_child_furs = rest_children.iter().map(|child| {
let (s, resolver) = BufStream::new(capacity, move |mut w| async move {
let (mut w, r) = fmt::buffer(buf_capacity);

child_streams.push(r);

async move {
child
.render_into_stream(&mut w, parent_scope, hydratable)
.await;
});

child_streams.push(s);

resolver
}
});

// Concurrently resolve all child futures.