Make capturing fast (#570)

swc_ecma_parser:
 - made capturing fast (Closes #533)
This commit is contained in:
kdy1 2020-01-06 01:02:51 +00:00
parent 8e3827403e
commit 02d4fb60f2
2 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,6 @@
use swc_common;
use std::sync::Arc;
use swc_common::{
self,
errors::{ColorConfig, Handler},
FileName, SourceMap,
};

View File

@ -5,7 +5,7 @@ use crate::{
Context, JscTarget, Syntax,
};
use lexer::TokenContexts;
use std::mem;
use std::{cell::RefCell, mem, rc::Rc};
use swc_common::{BytePos, Span, DUMMY_SP};
pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
@ -84,19 +84,19 @@ impl Tokens for TokensInput {
#[derive(Debug, Clone)]
pub struct Capturing<I: Tokens> {
inner: I,
captured: Vec<TokenAndSpan>,
captured: Rc<RefCell<Vec<TokenAndSpan>>>,
}
impl<I: Tokens> Capturing<I> {
pub fn new(input: I) -> Self {
Capturing {
inner: input,
captured: vec![],
captured: Default::default(),
}
}
/// Take captured tokens
pub fn take(&mut self) -> Vec<TokenAndSpan> {
mem::replace(&mut self.captured, vec![])
mem::replace(&mut *self.captured.borrow_mut(), Default::default())
}
}
@ -106,7 +106,7 @@ impl<I: Tokens> Iterator for Capturing<I> {
fn next(&mut self) -> Option<Self::Item> {
let next = self.inner.next();
self.captured.extend(next.clone());
self.captured.borrow_mut().extend(next.clone());
next
}
}