Pass around event types as Atoms instead of Strings

`Event` internally stores the `type` as an `Atom`, and we're `String`s
everywhere, which can cause unnecessary allocations to occur since
they'll end up as `Atom`s anyways.
This commit is contained in:
Corey Farwell 2015-12-10 22:06:05 -05:00
parent 996c0a60b8
commit 4accaf50b2
25 changed files with 156 additions and 135 deletions

View file

@ -13,6 +13,7 @@ use dom::bindings::reflector::reflect_dom_object;
use dom::event::Event;
use js::jsapi::{HandleValue, JSContext};
use js::jsval::JSVal;
use string_cache::Atom;
use util::str::DOMString;
// https://dom.spec.whatwg.org/#interface-customevent
@ -37,13 +38,13 @@ impl CustomEvent {
CustomEventBinding::Wrap)
}
pub fn new(global: GlobalRef,
type_: DOMString,
type_: Atom,
bubbles: bool,
cancelable: bool,
detail: HandleValue)
-> Root<CustomEvent> {
let ev = CustomEvent::new_uninitialized(global);
ev.InitCustomEvent(global.get_cx(), type_, bubbles, cancelable, detail);
ev.init_custom_event(global.get_cx(), type_, bubbles, cancelable, detail);
ev
}
#[allow(unsafe_code)]
@ -52,11 +53,26 @@ impl CustomEvent {
init: &CustomEventBinding::CustomEventInit)
-> Fallible<Root<CustomEvent>> {
Ok(CustomEvent::new(global,
type_,
Atom::from(&*type_),
init.parent.bubbles,
init.parent.cancelable,
unsafe { HandleValue::from_marked_location(&init.detail) }))
}
fn init_custom_event(&self,
_cx: *mut JSContext,
type_: Atom,
can_bubble: bool,
cancelable: bool,
detail: HandleValue) {
let event = self.upcast::<Event>();
if event.dispatching() {
return;
}
self.detail.set(detail.get());
event.init_event(type_, can_bubble, cancelable);
}
}
impl CustomEventMethods for CustomEvent {
@ -72,12 +88,6 @@ impl CustomEventMethods for CustomEvent {
can_bubble: bool,
cancelable: bool,
detail: HandleValue) {
let event = self.upcast::<Event>();
if event.dispatching() {
return;
}
self.detail.set(detail.get());
event.InitEvent(type_, can_bubble, cancelable);
self.init_custom_event(_cx, Atom::from(&*type_), can_bubble, cancelable, detail)
}
}