switch to dynamic dispatch for TokenSource

Benchmarks show no difference. This is probably because we are
bottlenecked on memory allocations, and we should fix that, but we are
not optimizing for performance just yet.

changes. Lines starting # with '#' will be ignored, and an empty
message aborts the commit. # # On branch token-source # Changes to be
committed: # modified: crates/ra_syntax/src/parsing/parser_api.rs #
modified: crates/ra_syntax/src/parsing/parser_impl.rs #
This commit is contained in:
Aleksey Kladov 2019-02-20 22:02:03 +03:00
parent 0c81b9deee
commit d2bce118ae
2 changed files with 7 additions and 9 deletions

View File

@ -4,7 +4,7 @@ use crate::{
SyntaxKind::{self, ERROR}, SyntaxKind::{self, ERROR},
parsing::{ parsing::{
token_set::TokenSet, token_set::TokenSet,
parser_impl::ParserImpl parser_impl::ParserImpl,
}, },
}; };
@ -17,9 +17,7 @@ use crate::{
/// tree, but rather a flat stream of events of the form /// tree, but rather a flat stream of events of the form
/// "start expression, consume number literal, /// "start expression, consume number literal,
/// finish expression". See `Event` docs for more. /// finish expression". See `Event` docs for more.
pub(crate) struct Parser<'t>( pub(crate) struct Parser<'t>(pub(super) ParserImpl<'t>);
pub(super) ParserImpl<crate::parsing::parser_impl::input::ParserInput<'t>>,
);
impl<'t> Parser<'t> { impl<'t> Parser<'t> {
/// Returns the kind of the current token. /// Returns the kind of the current token.

View File

@ -54,7 +54,7 @@ pub(super) fn parse_with<S: TreeSink>(
) -> S::Tree { ) -> S::Tree {
let mut events = { let mut events = {
let input = input::ParserInput::new(text, tokens); let input = input::ParserInput::new(text, tokens);
let parser_impl = ParserImpl::new(input); let parser_impl = ParserImpl::new(&input);
let mut parser_api = Parser(parser_impl); let mut parser_api = Parser(parser_impl);
parser(&mut parser_api); parser(&mut parser_api);
parser_api.0.into_events() parser_api.0.into_events()
@ -65,15 +65,15 @@ pub(super) fn parse_with<S: TreeSink>(
/// Implementation details of `Parser`, extracted /// Implementation details of `Parser`, extracted
/// to a separate struct in order not to pollute /// to a separate struct in order not to pollute
/// the public API of the `Parser`. /// the public API of the `Parser`.
pub(super) struct ParserImpl<S> { pub(super) struct ParserImpl<'a> {
token_source: S, token_source: &'a dyn TokenSource,
pos: InputPosition, pos: InputPosition,
events: Vec<Event>, events: Vec<Event>,
steps: Cell<u32>, steps: Cell<u32>,
} }
impl<S: TokenSource> ParserImpl<S> { impl<'a> ParserImpl<'a> {
fn new(token_source: S) -> ParserImpl<S> { fn new(token_source: &'a dyn TokenSource) -> ParserImpl<'a> {
ParserImpl { ParserImpl {
token_source, token_source,
pos: InputPosition::new(), pos: InputPosition::new(),