Repository navigation
[WIP] shared: Compact Utf8CodeUnits and friends - #47714
SimonSapin wants to merge 4 commits into
Conversation
Signed-off-by: Simon Sapin <[email protected]>
Signed-off-by: Simon Sapin <[email protected]>
|
This is an attempt at changing the memory representation of This saves memory use in the box and fragment tree, but reduces the range of values that can be represented. Not only conversion from Unfortunately this is a nightmare to deal with in the Maybe the all-fallible API approach needs revision, but having invisible potential Rust panic or value truncation doesn’t seem good either. |
|
I feel like the question is: What is the supposed caller do with the error information? If we get an overflow on real websites, don't we have to just increase the value? |
Fixes servo#47696, attempt number 2 Closes servo#47714, the previous attempt Testing: existing WPT and unit tests In that previous attempt I used `NonMaxU32` in order to optimize the size of `Option<Utf8CodeUnits>`. This made `u32::MAX` memory-unsafe to use, so as a precaution I made every operation checked, returning `Result`s. Unsurprisingly, this was a huge pain to deal with. I gave up before getting the `script` crate to compile. ## Smaller `RangeAny` Instead of the `Option` size optimization, per servo#47696 (comment) I’ve changed `RangeAny` from storing two options (each with a 64-bit or 32-bit tag because of alignment) to a single 4-variant enum (with a single tag). So `RangeAny<u32>` occupies 12 bytes instead of 16. This change is internal, the public API is still based on two options (though no longer bare public fields) ## `u32` overflow In this PR I’ve still reduced the width of `Utf*CodeUnits` from `usize` to `u32` but left the full `u32` range available. `impl Add for Utf8CodeUnits` is still there (not replaced with `checked_add`) and still defers to plain integer addition: checked with panic on overflow when compiled in debug mode, or wrapping in release mode. In either compilation mode, trying to deal with offsets greater than `u32::MAX` would be a bug (though not memory-unsafe). To help surface this issue to callers, I’ve changed some APIs from taking `&str` arguments to taking a new `struct Str32(&str)` marker type that says: “this string is expected to not be bigger than 4 GiB (`u32::MAX` bytes)” In practice the strings come from a few places: * `DOMString` in many places in the script crate: multi-representation, UTF-8 or Latin-1 * `Rope` for the text value of `<input>` or `<textarea>` elements: UTF-8 `Vec<String>` (currently does not cache its total length) * Text value of `CharacterData` DOM nodes: UTF-8 `String` * Rendered text in layout: mostly from DOM text (`CharacterData`) nodes, but for `InlineFormattingContext` also computes offsets spanning multiple DOM nodes Maybe we could systematically enforce limits on their sizes (with e.g. JS exceptions instead of Rust panics or silently incorrect behavior), but I haven’t done so in this PR. The script crate was already using `u32` is some places, so I suspect there were already bugs and/or limitations with very large strings including [a limitation from SpiderMonkey](https://stackoverflow.com/a/65570725). ## Splitting surrogate pairs As a drive-by improvement, `Utf*CodeUnits` is used in more places instead of `usize`, and their methods instead of ad-hoc offset or length computations. The latter points out a subtle aspect of converting a UTF-16 offset to UTF-8 or UTF-32: if the UTF-16 offset would split a surrogate pair (for a non-BMP code point), `Utf16CodeUnits` methods used to arbitrarily snap to the code point boundary *after* the whole pair. When using this method to implement the `maxlength` HTML attribute of `<input>` or `<textarea>` however, the snap must be to *before* the surrogate pair. So I’ve changed the behavior of the `Utf16CodeUnits` methods, which also affects the result of changing the selection range through DOM APIs. --------- Signed-off-by: Simon Sapin <[email protected]> Signed-off-by: Martin Robinson <[email protected]> Co-authored-by: Martin Robinson <[email protected]>
Fixes servo#47696, attempt number 2 Closes servo#47714, the previous attempt Testing: existing WPT and unit tests In that previous attempt I used `NonMaxU32` in order to optimize the size of `Option<Utf8CodeUnits>`. This made `u32::MAX` memory-unsafe to use, so as a precaution I made every operation checked, returning `Result`s. Unsurprisingly, this was a huge pain to deal with. I gave up before getting the `script` crate to compile. ## Smaller `RangeAny` Instead of the `Option` size optimization, per servo#47696 (comment) I’ve changed `RangeAny` from storing two options (each with a 64-bit or 32-bit tag because of alignment) to a single 4-variant enum (with a single tag). So `RangeAny<u32>` occupies 12 bytes instead of 16. This change is internal, the public API is still based on two options (though no longer bare public fields) ## `u32` overflow In this PR I’ve still reduced the width of `Utf*CodeUnits` from `usize` to `u32` but left the full `u32` range available. `impl Add for Utf8CodeUnits` is still there (not replaced with `checked_add`) and still defers to plain integer addition: checked with panic on overflow when compiled in debug mode, or wrapping in release mode. In either compilation mode, trying to deal with offsets greater than `u32::MAX` would be a bug (though not memory-unsafe). To help surface this issue to callers, I’ve changed some APIs from taking `&str` arguments to taking a new `struct Str32(&str)` marker type that says: “this string is expected to not be bigger than 4 GiB (`u32::MAX` bytes)” In practice the strings come from a few places: * `DOMString` in many places in the script crate: multi-representation, UTF-8 or Latin-1 * `Rope` for the text value of `<input>` or `<textarea>` elements: UTF-8 `Vec<String>` (currently does not cache its total length) * Text value of `CharacterData` DOM nodes: UTF-8 `String` * Rendered text in layout: mostly from DOM text (`CharacterData`) nodes, but for `InlineFormattingContext` also computes offsets spanning multiple DOM nodes Maybe we could systematically enforce limits on their sizes (with e.g. JS exceptions instead of Rust panics or silently incorrect behavior), but I haven’t done so in this PR. The script crate was already using `u32` is some places, so I suspect there were already bugs and/or limitations with very large strings including [a limitation from SpiderMonkey](https://stackoverflow.com/a/65570725). ## Splitting surrogate pairs As a drive-by improvement, `Utf*CodeUnits` is used in more places instead of `usize`, and their methods instead of ad-hoc offset or length computations. The latter points out a subtle aspect of converting a UTF-16 offset to UTF-8 or UTF-32: if the UTF-16 offset would split a surrogate pair (for a non-BMP code point), `Utf16CodeUnits` methods used to arbitrarily snap to the code point boundary *after* the whole pair. When using this method to implement the `maxlength` HTML attribute of `<input>` or `<textarea>` however, the snap must be to *before* the surrogate pair. So I’ve changed the behavior of the `Utf16CodeUnits` methods, which also affects the result of changing the selection range through DOM APIs. --------- Signed-off-by: Simon Sapin <[email protected]> Signed-off-by: Martin Robinson <[email protected]> Co-authored-by: Martin Robinson <[email protected]>
Fixes servo#47696, attempt number 2 Closes servo#47714, the previous attempt Testing: existing WPT and unit tests In that previous attempt I used `NonMaxU32` in order to optimize the size of `Option<Utf8CodeUnits>`. This made `u32::MAX` memory-unsafe to use, so as a precaution I made every operation checked, returning `Result`s. Unsurprisingly, this was a huge pain to deal with. I gave up before getting the `script` crate to compile. ## Smaller `RangeAny` Instead of the `Option` size optimization, per servo#47696 (comment) I’ve changed `RangeAny` from storing two options (each with a 64-bit or 32-bit tag because of alignment) to a single 4-variant enum (with a single tag). So `RangeAny<u32>` occupies 12 bytes instead of 16. This change is internal, the public API is still based on two options (though no longer bare public fields) ## `u32` overflow In this PR I’ve still reduced the width of `Utf*CodeUnits` from `usize` to `u32` but left the full `u32` range available. `impl Add for Utf8CodeUnits` is still there (not replaced with `checked_add`) and still defers to plain integer addition: checked with panic on overflow when compiled in debug mode, or wrapping in release mode. In either compilation mode, trying to deal with offsets greater than `u32::MAX` would be a bug (though not memory-unsafe). To help surface this issue to callers, I’ve changed some APIs from taking `&str` arguments to taking a new `struct Str32(&str)` marker type that says: “this string is expected to not be bigger than 4 GiB (`u32::MAX` bytes)” In practice the strings come from a few places: * `DOMString` in many places in the script crate: multi-representation, UTF-8 or Latin-1 * `Rope` for the text value of `<input>` or `<textarea>` elements: UTF-8 `Vec<String>` (currently does not cache its total length) * Text value of `CharacterData` DOM nodes: UTF-8 `String` * Rendered text in layout: mostly from DOM text (`CharacterData`) nodes, but for `InlineFormattingContext` also computes offsets spanning multiple DOM nodes Maybe we could systematically enforce limits on their sizes (with e.g. JS exceptions instead of Rust panics or silently incorrect behavior), but I haven’t done so in this PR. The script crate was already using `u32` is some places, so I suspect there were already bugs and/or limitations with very large strings including [a limitation from SpiderMonkey](https://stackoverflow.com/a/65570725). ## Splitting surrogate pairs As a drive-by improvement, `Utf*CodeUnits` is used in more places instead of `usize`, and their methods instead of ad-hoc offset or length computations. The latter points out a subtle aspect of converting a UTF-16 offset to UTF-8 or UTF-32: if the UTF-16 offset would split a surrogate pair (for a non-BMP code point), `Utf16CodeUnits` methods used to arbitrarily snap to the code point boundary *after* the whole pair. When using this method to implement the `maxlength` HTML attribute of `<input>` or `<textarea>` however, the snap must be to *before* the surrogate pair. So I’ve changed the behavior of the `Utf16CodeUnits` methods, which also affects the result of changing the selection range through DOM APIs. --------- Signed-off-by: Simon Sapin <[email protected]> Signed-off-by: Martin Robinson <[email protected]> Co-authored-by: Martin Robinson <[email protected]>
Fixes #47696