惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
F
Full Disclosure
B
Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
月光博客
月光博客
I
Intezer
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
P
Proofpoint News Feed
C
Check Point Blog
N
News | PayPal Newsroom
H
Heimdal Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
G
GRAHAM CLULEY
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
爱范儿
爱范儿
Martin Fowler
Martin Fowler
U
Unit 42
T
Troy Hunt's Blog
S
Securelist
V
V2EX
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News

Henri Sivonen’s pages

Parin vuoden tutkimattomuus crates.io: Rust Package Registry Asiakirjatonta toimintaa It’s not wrong that "🤦🏼‍♂️".length == 7 Koulutartuntojen tilastointimenettely Perusteasiakirjoja hallussapitämättä ikärajoitettu Asiantuntijat ja nukkuva vallan vahtikoira Koronapassilausunto Suppealla tietopohjalla ohimeneväksi väitetty Text Encoding Menu in 2021 The Text Encoding Submenu Is Gone An HTML5 Conformance Checker Not Part of the Technology Stack Browser Technology Stack Bogo-XML Declaration Returns to Gecko A Look at Encoding Detection and Encoding Menu Telemetry from Firefox 86 Why Supporting Unlabeled UTF-8 in HTML on the Web Would Be Problematic Rust Target Names Aren’t Passed to LLVM Toimintamalli Activating Browser Modes with Doctype Johtopäätöksiä mallin rakenteesta Tehtävänmäärittelyä kirjoittamatta ja kuolemia laskematta laumasuojamallinnettu Character Encoding Menu in 2014 Erillissuosituksen tarpeettomuudesta yleissuosituksen poikkeukseksi? STM:n maskiaikajana Rust 2021 Oma-aloitteisesti mallinnettu Kokopinovaatimuksin kilpailutettu chardetng: A More Compact Character Encoding Detector for the Legacy Web Varauksia paisutellen tiedotettu Perusteasiakirjoitta tiedotettu Always Use UTF-8 & Always Label Your HTML Saying So IME Smoke Testing The Validator.nu HTML Parser About the Hiragino Fonts with CSS It’s Time to Stop Adding New Features for Non-Unicode Execution Encodings in C++ Rust 2020 The Last of the Parsing Quirks About about:blank Rust 2019 a Web-Compatible Character Encoding Library in Rust How I Wrote a Modern C++ Library in Rust Using cargo-fuzz to Transfer Code Review of Simple Safe Code to Complex Code that Uses unsafe A Rust Crate that Also Quacks Like a Modern C++ Library #Rust2018 No Namespaces in JSON, Please A Lecture about HTML5 Julkisesti luotettu varmenne ikidomainille TLS:ää (SSL:ää) varten -webkit-HTML5 Lists in Attribute Values The Sad Story of PNG Gamma “Correction” If You Want Software Freedom on Phones, You Should Work on Firefox OS, Custom Hardware and Web App Self-Hostablility HTML5 Parser Improvements ARIA in HTML5 Integration: Document Conformance (Draft, Take Two) Schema.org and Pre-Existing Communities Lowering memory requirements by replacing Schematron HTML5 Parsing in Gecko: A Build Introducing SAX Tree NVDL Support in Validator.nu HOWTO Avoid Being Called a Bozo When Producing XML An Unofficial Q&A about the Discontinuation of the XHTML2 WG Thoughts on HTML5 Becoming a W3C Recommendation Four Finnish Banks Training Users to Give Banking Credentials to Another Site Unimpressed by Leopard Sergeant Semantics The Content Sink Inheritance Diagram – 2006-06-30 What is EME? About Points and Pixels as Units Social Media Impression Management The spacer Element Is Gone Openmind 2006 Performance Mistake XHTML and Mobile Devices WebM-Enabled Browser Usage Share Exceeds H.264-Enabled Browser Usage Share on Desktop (in StatCounter Numbers) HTML5 Parser-Based View Source Syntax Highlighting Vendor Prefixes Are Hurting the Web Accept-Charset Is No More Dualroids Writing Structural Stylable Document in Mozilla Editor ISO-8859-15 on haitallinen Hourglass The Scientific Method According to Hixie Maemo Source Code Karpelan lukkovertaus ontuu Digitaalisesta arkistoinnista ARIA in HTML5 Integration: Document Conformance (Draft) XHTML—What’s the Point? (Draft, incomplete) Mac OS X Browser Comparison HOWTO Spot a Wannabe Web Standards Advocate An Idea About Intermediate Language Trees and Web UI Generation Thoughts on Using SSL/TLS Certificates as the Solution to Phishing Bureaucracy Meets the Web Europe Day HOWTO Establish a 100% Literacy Rate What to Do with All These Photos? Charmod Norm Checking Validator Web Service Interface Ideas DTDs Don’t Work on the Web EFFI’s Day in Court Speaking at XTech
The Performance Cost of the HTML Tree Builder
Henri Sivonen · 2012-09-17 · via Henri Sivonen’s pages

I’ve been thinking about the performance gap between the Validator.nu HTML Parser and Xerces. What can be attributed to the “extra fix-ups” that an HTML parser has to do and what can be attributed to my code being worse than the Xerces code?

Introduction

Tokenizing XML and HTML is pretty similar. Sure, an HTML tokenizer has to check each name character for upper case, but then an XML tokenizer has to check the silliness that is the Name production. The main difference is in tree construction layer. In general, comparing an XML parser and an HTML parser from different authors doesn’t tell much about the performance cost of the “extra fix-ups” HTML needs. Parsers may have otherwise fundamentally better or worse implementation strategies, and different code bases have enjoyed different amounts of attention and tweaking. To compare the tree construction layers, the tokenizing layer needs to be kept constant.

To run a proper benchmark, I implemented a very thin token handler that trivially maps HTML5 tokens to SAX events. This token handler is only 115 lines of code (mostly autogenerated by Eclipse) compared to 3927 lines of the real HTML5 SAX streamer code. With this thin layer, the resulting parser is similar to an XML5 parser without support for Namespaces.

Method

I chose to use the front page of Wikipedia (saved on 2008-04-01) as the test in data, because it is a well-known, real-world Web page that happens to be well-formed text/html-compatible XHTML, so it can be used for testing both HTML and XML parsers.

Since I wanted to test the parser core, I eliminated the effect of IO and character decoding by letting the parsers read pre-converted UTF-16 data from a CharArrayReader. Instead of building a tree, the parsers ran in SAX mode. The content handler an XML serializer writing to a mock Writer that wrote to nowhere. (This way, there was some code to touch each attribute in case Xerces builds attributes lazily. I have not checked if it does.) All the parsers were set to intern element and attribute name strings. The XML parsers were set not to read the DTD.

I ran each parser in a loop first for 10 minutes for warming up HotSpot and then for another 10 minutes to actually record the benchmark. I ran that tests on Mac OS X 10.5.4 on Intel Core 2 Duo (x86_64).

I also included “fast read()” variants of the Validator.nu HTML Parser. These variants have the per-character error reporting and validator-precision source location tracking commented out. (I only commented them out in the most obvious place—there’s more potential for removing stuff that’s only interesting in a validator.)

Results

Here are the results in number of iterations per time relative to the tokenizer of the Validator.nu HTML Parser with the thin SAX layer. (Note, the two VMs are not equally fast. The 1.6 x86_64 server VM is over 50% faster than the x86 1.5 client VM.)

Parser 1.5 x86 Client 1.6 x86_64 Server
Xerces-J 2.9.1 with Namespaces 109% 112%
Xerces-J 2.9.1 without Namespaces 141% 142%
Ælfred2 (Validator.nu fork) with Namespaces 72% 75%
Validator.nu HTML Parser streaming SAX mode 89% 93%
Validator.nu HTML tokenizer with thin SAX layer 100% 100%
Validator.nu HTML Parser streaming SAX mode, fast read() 95% 93%
Validator.nu HTML tokenizer with thin SAX layer, fast read() 107% 104%

The difference between Xerces with and without Namespaces sure looks interesting. Let’s see what the numbers look like relative to Xerces without Namespaces.

Parser 1.5 x86 Client 1.6 x86_64 Server
Xerces-J 2.9.1 with Namespaces 78% 79%
Xerces-J 2.9.1 without Namespaces 100% 100%
Ælfred2 (Validator.nu fork) with Namespaces 51% 53%
Validator.nu HTML Parser streaming SAX mode 63% 66%
Validator.nu HTML tokenizer with thin SAX layer 71% 71%
Validator.nu HTML Parser streaming SAX mode, fast read() 68% 66%
Validator.nu HTML tokenizer with thin SAX layer, fast read() 76% 74%

Analysis

  • Even when input is well-formed, the latent ability of the parser to deal with the HTML tree building rules has a cost. XHTML-as-text/html advocates take note: Making your markup well-formed doesn’t make an HTML parser go as fast as an XML parser with the same tokenizer implementation would go.

  • Even with Namespace support, Xerces beats the Validator.nu HTML Parser’s tokenizer (which is copying data even when not needed). In my defense, Xerces has more person years invested in it and doesn’t have validator-precision source location tracking.

  • Removing the per-character part of validator-precision reporting makes the Validator.nu tokenizer almost match Xerces with Namespaces on the 1.5 client VM, but the relative gain from removing reporting is smaller on the 1.6 server VM.

  • Namespace support in Xerces is relatively more costly than HTML5 tree building rules in the Validator.nu HTML Parser. That’t right, on a page with a default namespace for elements at the top and attributes (except on the root element) in no namespace, enabling Namespaces lops off over 20% of the performance.

  • An XML parser (here the patched Ælfred2) can be a lot slower than an HTML parser. XML advocates take note: Just having an XML parser doesn’t guarantee performance over HTML—especially if the HTML side is getting more attention.

  • The performance cost of the HTML tree builder is rather small after all: 7% on the better VM.

Summary

Xerces is faster. Namespaces are worse than the much-maligned HTML “extra fix-ups” (21% hit vs. 7% hit). An XML parser can be slow.