Skip to content

Commit

Permalink
Make capturing fast (#570)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - made capturing fast (Closes #533)
  • Loading branch information
kdy1 committed Jan 6, 2020
1 parent 8e38274 commit 02d4fb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions ecmascript/parser/examples/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use swc_common;

use std::sync::Arc;
use swc_common::{
self,
errors::{ColorConfig, Handler},
FileName, SourceMap,
};
Expand Down
10 changes: 5 additions & 5 deletions ecmascript/parser/src/parser/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit 02d4fb6

Please sign in to comment.