Skip to content

Commit 16169b6

Browse files
committed
simplified code branching in some areas
1 parent 8ef4737 commit 16169b6

File tree

7 files changed

+146
-108
lines changed

7 files changed

+146
-108
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
The first streamable, low memory XML, HTML, JSX and Angular Template parser for [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly).
99

1010
Sax Wasm is a sax style parser for XML, HTML, JSX and Angular Templates written in [Rust](https://www.rust-lang.org/en-US/), compiled for
11-
WebAssembly with the sole motivation to bring **faster than native speeds** to XML and JSX parsing for node and the web.
11+
WebAssembly with the sole motivation to bring the **fastest possible speeds** to XML and JSX parsing for node and the web.
1212
Inspired by [sax js](https://github.com/isaacs/sax-js) and rebuilt with Rust for WebAssembly, sax-wasm brings optimizations
1313
for speed and support for JSX syntax.
1414

@@ -20,12 +20,12 @@ All parsers are tested using a large XML document (3 MB) containing a variety of
2020

2121
| Parser with Advanced Features | time/ms (lower is better)| JS | Runs in browser |
2222
|--------------------------------------------------------------------------------------------|-------------------------:|:------:|:---------------:|
23-
| [sax-wasm](https://github.com/justinwilaby/sax-wasm) | 0.466 |||
24-
| [saxes](https://github.com/lddubeau/saxes) | 0.868 |||
25-
| [ltx(using Saxes as the parser)](https://github.com/xmppjs/ltx) | 0.881 |||
26-
| [node-xml](https://github.com/dylang/node-xml) | 1.549 || |
27-
| [node-expat](https://github.com/xmppo/node-expat) | 1.551 |||
28-
| [sax-js](https://github.com/isaacs/sax-js) | 1.869 || * |
23+
| [sax-wasm](https://github.com/justinwilaby/sax-wasm) | 27.91 |||
24+
| [saxes](https://github.com/lddubeau/saxes) | 40.99 |||
25+
| [ltx(using Saxes as the parser)](https://github.com/xmppjs/ltx) | 41.87 |||
26+
| [sax-js](https://github.com/isaacs/sax-js) | 105.18 || * |
27+
| [node-xml](https://github.com/dylang/node-xml) | 115.17 |||
28+
| [node-expat](https://github.com/xmppo/node-expat) | 148.885 || |
2929
<sub>*built for node but *should* run in the browser</sub>
3030

3131
## Installation

lib/sax-wasm.wasm

1.05 KB
Binary file not shown.

src/js/__test__/benchmark.mjs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { URL } from 'url';
33
import { resolve } from 'path';
44
import { Buffer } from 'buffer';
55

6-
import { SAXParser } from '../../../lib/cjs/index.js';
6+
import { SAXParser, SaxEventType } from '../../../lib/cjs/index.js';
77

88
import nodeXml from 'node-xml';
99
import expat from 'node-expat';
@@ -24,7 +24,7 @@ async function benchmarkSaxWasmParser() {
2424
let t = process.hrtime();
2525
let offset = 0;
2626
while (offset < xml.length) {
27-
parser.write(Buffer.from(xml.slice(offset, chunkLen)));
27+
parser.write(Buffer.from(xml.slice(offset, chunkLen + offset)));
2828
offset += chunkLen;
2929
}
3030
parser.end();
@@ -38,7 +38,7 @@ async function benchmarkNodeXmlParser() {
3838
let t = process.hrtime();
3939
let offset = 0;
4040
while (offset < xml.length) {
41-
parser.parseString(Buffer.from(xml.slice(offset, chunkLen)));
41+
parser.parseString(Buffer.from(xml.slice(offset, chunkLen + offset)));
4242
offset += chunkLen;
4343
}
4444
let [s, n] = process.hrtime(t);
@@ -50,7 +50,7 @@ async function benchmarkExpatParser() {
5050
let t = process.hrtime();
5151
let offset = 0;
5252
while (offset < xml.length) {
53-
parser.parse(Buffer.from(xml.slice(offset, chunkLen)));
53+
parser.parse(Buffer.from(xml.slice(offset, chunkLen + offset)));
5454
offset += chunkLen;
5555
}
5656

@@ -64,7 +64,7 @@ async function benchmarkSaxesParser() {
6464

6565
let offset = 0;
6666
while (offset < xml.length) {
67-
parser.write(Buffer.from(xml.slice(offset, chunkLen)));
67+
parser.write(Buffer.from(xml.slice(offset, chunkLen + offset)));
6868
offset += chunkLen;
6969
}
7070
let [s, n] = process.hrtime(t);
@@ -78,7 +78,7 @@ async function benchmarkSaxParser() {
7878

7979
let offset = 0;
8080
while (offset < xml.length) {
81-
parser.write(Buffer.from(xml.slice(offset, chunkLen)));
81+
parser.write(Buffer.from(xml.slice(offset, chunkLen + offset)));
8282
offset += chunkLen;
8383
}
8484
let [s, n] = process.hrtime(t);
@@ -91,7 +91,7 @@ async function benchmarkLtxParser() {
9191

9292
let offset = 0;
9393
while (offset < xml.length) {
94-
parser.write(Buffer.from(xml.slice(offset, chunkLen)));
94+
parser.write(Buffer.from(xml.slice(offset, chunkLen + offset)));
9595
offset += chunkLen;
9696
}
9797

@@ -100,7 +100,7 @@ async function benchmarkLtxParser() {
100100
}
101101

102102
async function benchmark() {
103-
let t = 100;
103+
let t = 10;
104104
let benchmarks = { saxWasm: [], nodeXml: [], saxes: [], sax: [], expat: [], ltx: [] };
105105
while (t--) {
106106
benchmarks.saxWasm.push(await benchmarkSaxWasmParser());

src/sax/grapheme_iterator.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ impl GraphemeClusters<'_> {
110110
/// let result = gc.take_until_ascii(&[b' ']);
111111
/// assert!(result.is_some());
112112
/// ```
113+
113114
pub fn take_until_ascii(&mut self, chars: &[u8]) -> Option<GraphemeResult<'_>> {
114115
let mut cursor = self.cursor;
115116
let start = self.cursor;
116117
let mut line = self.line;
117118
let mut character = self.character;
118119
let mut end = self.cursor;
119-
loop {
120-
if self.byte_len <= cursor {
121-
return None;
122-
}
120+
121+
while cursor < self.byte_len {
123122
let next_byte = unsafe { *self.bytes.get_unchecked(cursor) };
124123
if chars.contains(&next_byte) {
125124
break;
@@ -135,6 +134,11 @@ impl GraphemeClusters<'_> {
135134
end += len;
136135
cursor = end;
137136
}
137+
138+
if self.cursor == cursor {
139+
return None;
140+
}
141+
138142
self.cursor = cursor;
139143
self.line = line;
140144
self.character = character;

0 commit comments

Comments
 (0)