Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #91

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ selectors = "0.23.0"
smallvec = "1.9.0"
tendril = "0.4.3"
indexmap = { version = "1.9.1", optional = true }
ahash = "0.8"

[dependencies.getopts]
version = "0.2.21"
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ fn main() {
.any(|m| m)
};

process::exit(if matched { 0 } else { 1 });
process::exit(i32::from(!matched));
}
16 changes: 8 additions & 8 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! HTML nodes.

use ahash::{HashMap, HashSet};
use std::collections::{hash_map, hash_set};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::ops::Deref;

Expand Down Expand Up @@ -223,10 +223,10 @@ pub struct Element {
pub name: QualName,

/// The element ID.
pub id: Option<LocalName>,
pub id: Option<String>,

/// The element classes.
pub classes: HashSet<LocalName>,
pub classes: HashSet<String>,
Copy link
Member

Choose a reason for hiding this comment

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

If we include #101, it might be preferable to keep this as LocalName as class names should be highly redundant seen over one or even multiple documents. Lazy initialization would then potentially allow avoiding some of the cost of deduplicating these strings when they are never matched for a given element.

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right. Maybe we should consider merging #91 and #101 at the same time after #101 is ready. However, this would require a major update to the crate since #101 causes breaking changes to be made to the API.


/// The element attributes.
pub attrs: Attributes,
Expand All @@ -238,16 +238,16 @@ impl Element {
let id = attrs
.iter()
.find(|a| a.name.local.deref() == "id")
.map(|a| LocalName::from(a.value.deref()));
.map(|a| String::from(a.value.deref()));

let classes: HashSet<LocalName> = attrs
let classes: HashSet<String> = attrs
.iter()
.find(|a| a.name.local.deref() == "class")
.map_or(HashSet::new(), |a| {
.map_or(HashSet::default(), |a| {
a.value
.deref()
.split_whitespace()
.map(LocalName::from)
.map(String::from)
.collect()
});

Expand Down Expand Up @@ -300,7 +300,7 @@ impl Element {
#[allow(missing_debug_implementations)]
#[derive(Clone)]
pub struct Classes<'a> {
inner: hash_set::Iter<'a, LocalName>,
inner: hash_set::Iter<'a, String>,
}

impl<'a> Iterator for Classes<'a> {
Expand Down
7 changes: 3 additions & 4 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,18 @@ impl<'i> parser::Parser<'i> for Parser {
pub struct Simple;

impl parser::SelectorImpl for Simple {
// see: https://github.com/servo/servo/pull/19747#issuecomment-357106065
type ExtraMatchingData = String;
type AttrValue = CssString;
type Identifier = CssLocalName;
type LocalName = CssLocalName;
type NamespacePrefix = CssLocalName;
type NamespaceUrl = Namespace;
type BorrowedNamespaceUrl = Namespace;
type BorrowedLocalName = CssLocalName;

type NonTSPseudoClass = NonTSPseudoClass;
type PseudoElement = PseudoElement;

// see: https://github.com/servo/servo/pull/19747#issuecomment-357106065
type ExtraMatchingData = String;
type PseudoElement = PseudoElement;
}

/// Wraps [`String`] so that it can be used with [`selectors`]
Expand Down