Skip to content

Commit

Permalink
feat(plugin): Allow taking the inner data from versioned (#5071)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj authored Jun 30, 2022
1 parent dcc4f2a commit e1aa937
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
27 changes: 25 additions & 2 deletions crates/swc_common/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! `swc_common`.
#![allow(unused)]

use std::any::type_name;
use std::{any::type_name, mem};

use anyhow::Error;
use bytecheck::CheckBytes;
Expand Down Expand Up @@ -174,4 +174,27 @@ impl Serialized {
#[derive(Archive, Deserialize, Serialize)]
#[repr(transparent)]
#[archive_attr(repr(transparent), derive(CheckBytes))]
pub struct VersionedSerializable<T>(#[with(AsBox)] pub (u32, T));
pub struct VersionedSerializable<T>(#[with(AsBox)] (u32, T));

impl<T> VersionedSerializable<T> {
pub fn new(value: T) -> Self {
// TODO: we'll add compile time flag to augment schema version.
// User should not try to set version by themselves.
VersionedSerializable((1, value))
}

pub fn version(&self) -> u32 {
self.0 .0
}

pub fn inner(&self) -> &T {
&self.0 .1
}

pub fn take(&mut self) -> T
where
T: Default,
{
mem::take(&mut self.0 .1)
}
}
6 changes: 6 additions & 0 deletions crates/swc_ecma_ast/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub enum Program {
Script(Script),
}

impl Default for Program {
fn default() -> Self {
Program::Module(Module::dummy())
}
}

#[ast_node("Module")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct Module {
Expand Down

1 comment on commit e1aa937

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: e1aa937 Previous: e9bad20 Ratio
es/full/minify/libraries/antd 1727364091 ns/iter (± 54532047) 1680813186 ns/iter (± 10332486) 1.03
es/full/minify/libraries/d3 429979035 ns/iter (± 4506701) 422622792 ns/iter (± 8936562) 1.02
es/full/minify/libraries/echarts 1664779840 ns/iter (± 24718591) 1671741022 ns/iter (± 24112995) 1.00
es/full/minify/libraries/jquery 105003017 ns/iter (± 4254857) 95636253 ns/iter (± 2518036) 1.10
es/full/minify/libraries/lodash 131544173 ns/iter (± 4528322) 123188307 ns/iter (± 4672633) 1.07
es/full/minify/libraries/moment 57582398 ns/iter (± 3173103) 53871720 ns/iter (± 702555) 1.07
es/full/minify/libraries/react 19068493 ns/iter (± 501602) 17727787 ns/iter (± 199977) 1.08
es/full/minify/libraries/terser 618229835 ns/iter (± 22007363) 607997268 ns/iter (± 13278647) 1.02
es/full/minify/libraries/three 567282018 ns/iter (± 11626451) 560432933 ns/iter (± 12574192) 1.01
es/full/minify/libraries/typescript 3559981447 ns/iter (± 69841661) 3545819998 ns/iter (± 48360796) 1.00
es/full/minify/libraries/victory 749315401 ns/iter (± 12156272) 739994182 ns/iter (± 3833991) 1.01
es/full/minify/libraries/vue 156586585 ns/iter (± 3336991) 143291821 ns/iter (± 1983765) 1.09
es/full/codegen/es3 32810 ns/iter (± 903) 32689 ns/iter (± 792) 1.00
es/full/codegen/es5 32809 ns/iter (± 1526) 32666 ns/iter (± 1416) 1.00
es/full/codegen/es2015 32848 ns/iter (± 418) 32717 ns/iter (± 268) 1.00
es/full/codegen/es2016 32798 ns/iter (± 572) 32686 ns/iter (± 254) 1.00
es/full/codegen/es2017 32857 ns/iter (± 563) 32631 ns/iter (± 340) 1.01
es/full/codegen/es2018 32805 ns/iter (± 1033) 32661 ns/iter (± 620) 1.00
es/full/codegen/es2019 32863 ns/iter (± 997) 32644 ns/iter (± 737) 1.01
es/full/codegen/es2020 32858 ns/iter (± 1225) 32655 ns/iter (± 1025) 1.01
es/full/all/es3 197935526 ns/iter (± 11493587) 184634285 ns/iter (± 2965726) 1.07
es/full/all/es5 185281025 ns/iter (± 6656809) 172925744 ns/iter (± 3163746) 1.07
es/full/all/es2015 147599855 ns/iter (± 5394853) 140194435 ns/iter (± 2195994) 1.05
es/full/all/es2016 154102708 ns/iter (± 11544819) 139389793 ns/iter (± 1987027) 1.11
es/full/all/es2017 150505397 ns/iter (± 6463510) 138910548 ns/iter (± 2396173) 1.08
es/full/all/es2018 149309296 ns/iter (± 5816013) 137290854 ns/iter (± 2407335) 1.09
es/full/all/es2019 147273785 ns/iter (± 7370877) 137173045 ns/iter (± 3675016) 1.07
es/full/all/es2020 140181736 ns/iter (± 6566415) 131973204 ns/iter (± 3112624) 1.06
es/full/parser 707473 ns/iter (± 24071) 710051 ns/iter (± 18475) 1.00
es/full/base/fixer 29867 ns/iter (± 486) 29213 ns/iter (± 303) 1.02
es/full/base/resolver_and_hygiene 87353 ns/iter (± 1571) 86639 ns/iter (± 2309) 1.01
serialization of ast node 215 ns/iter (± 5) 214 ns/iter (± 4) 1.00
serialization of serde 225 ns/iter (± 7) 224 ns/iter (± 7) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.