Debian Codegraph @

debian/missing-sources/tendril/README.md

raw symbols SCIP
1 # tendril
2
3 **Warning**: This library is at a very early stage of development, and it
4 contains a substantial amount of `unsafe` code. Use at your own risk!
5
6 [![Build Status](https://github.com/servo/tendril/workflows/CI/badge.svg)](https://github.com/servo/tendril/actions)
7
8 [API Documentation](https://doc.servo.org/tendril/index.html)
9
10 ## Introduction
11
12 `Tendril` is a compact string/buffer type, optimized for zero-copy parsing.
13 Tendrils have the semantics of owned strings, but are sometimes views into
14 shared buffers. When you mutate a tendril, an owned copy is made if necessary.
15 Further mutations occur in-place until the string becomes shared, e.g. with
16 `clone()` or `subtendril()`.
17
18 Buffer sharing is accomplished through thread-local (non-atomic) reference
19 counting, which has very low overhead. The Rust type system will prevent
20 you at compile time from sending a tendril between threads. (See below
21 for thoughts on relaxing this restriction.)
22
23 Whereas `String` allocates in the heap for any non-empty string, `Tendril` can
24 store small strings (up to 8 bytes) in-line, without a heap allocation.
25 `Tendril` is also smaller than `String` on 64-bit platforms — 16 bytes versus
26 24. `Option<Tendril>` is the same size as `Tendril`, thanks to
27 [`NonZero`][NonZero].
28
29 The maximum length of a tendril is 4 GB. The library will panic if you attempt
30 to go over the limit.
31
32 ## Formats and encoding
33
34 `Tendril` uses
35 [phantom types](https://doc.rust-lang.org/stable/rust-by-example/generics/phantom.html)
36 to track a buffer's format. This determines at compile time which
37 operations are available on a given tendril. For example, `Tendril<UTF8>` and
38 `Tendril<Bytes>` can be borrowed as `&str` and `&[u8]` respectively.
39
40 `Tendril` also integrates with
41 [rust-encoding](https://github.com/lifthrasiir/rust-encoding) and has
42 preliminary support for [WTF-8][] buffers.
43
44 ## Plans for the future
45
46 ### Ropes
47
48 [html5ever][] will use `Tendril` as a zero-copy text representation. It would
49 be good to preserve this all the way through to Servo's DOM. This would reduce
50 memory consumption, and possibly speed up text shaping and painting. However,
51 DOM text may conceivably be larger than 4 GB, and will anyway not be contiguous
52 in memory around e.g. a character entity reference.
53
54 *Solution:* Build a **[rope][] on top of these strings** and use that as
55 Servo's representation of DOM text. We can perhaps do text shaping and/or
56 painting in parallel for different chunks of a rope. html5ever can additionally
57 use this rope type as a replacement for `BufferQueue`.
58
59 Because the underlying buffers are reference-counted, the bulk of this rope
60 is already a [persistent data structure][]. Consider what happens when
61 appending two ropes to get a "new" rope. A vector-backed rope would copy a
62 vector of small structs, one for each chunk, and would bump the corresponding
63 refcounts. But it would not copy any of the string data.
64
65 If we want more sharing, then a [2-3 finger tree][] could be a good choice.
66 We would probably stick with `VecDeque` for ropes under a certain size.
67
68 ### UTF-16 compatibility
69
70 SpiderMonkey expects text to be in UCS-2 format for the most part. The
71 semantics of JavaScript strings are difficult to implement on UTF-8. This also
72 applies to HTML parsing via `document.write`. Also, passing SpiderMonkey a
73 string that isn't contiguous in memory will incur additional overhead and
74 complexity, if not a full copy.
75
76 *Solution:* Use **WTF-8 in parsing** and in the DOM. Servo will **convert to
77 contiguous UTF-16 when necessary**. The conversion can easily be parallelized,
78 if we find a practical need to convert huge chunks of text all at once.
79
80 ### Source span information
81
82 Some html5ever API consumers want to know the originating location in the HTML
83 source file(s) of each token or parse error. An example application would be a
84 command-line HTML validator with diagnostic output similar to `rustc`'s.
85
86 *Solution:* Accept **some metadata along with each input string**. The type of
87 metadata is chosen by the API consumer; it defaults to `()`, which has size
88 zero. For any non-inline string, we can provide the associated metadata as well
89 as a byte offset.
90
91 [NonZero]: https://doc.rust-lang.org/core/nonzero/struct.NonZero.html
92 [html5ever]: https://github.com/servo/html5ever
93 [WTF-8]: https://simonsapin.github.io/wtf-8/
94 [rope]: https://en.wikipedia.org/wiki/Rope_%28data_structure%29
95 [persistent data structure]: https://en.wikipedia.org/wiki/Persistent_data_structure
96 [2-3 finger tree]: https://www.staff.city.ac.uk/~ross/papers/FingerTree.html