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

Reinvent icu_pattern with support for multiple backends #4610

Merged
merged 46 commits into from
Feb 27, 2024

Conversation

sffc
Copy link
Member

@sffc sffc commented Feb 14, 2024

After experimenting with this for some time, it's clear that there is not a single storage solution that works optimally for every use case. I have therefore proceeded to make Pattern generic over its backend. This leads to cleaner, more flexible code.

In this initial PR, I added support for a straightforward and efficient backend SinglePlaceholder. I will follow up with additional backends in future PRs.

Replaces #4579

@sffc sffc added the discuss-priority Discuss at the next ICU4X meeting label Feb 14, 2024
utils/pattern/src/num_pattern/mod.rs Outdated Show resolved Hide resolved
utils/pattern/src/num_pattern/mod.rs Outdated Show resolved Hide resolved
@sffc
Copy link
Member Author

sffc commented Feb 15, 2024

Some notes from diving deeper into this with @robertbastian @Manishearth:

  • Reminder that placeholders could appear in either order (as we had previously discovered) so some sorting needs to take place
  • Placeholders could also appear adjacent to each other; need to handle patterns such as "{1}{0}"
  • If we use a UTF-8 varint encoding for the placeholder offsets, limiting the length to 128 means that we can be forward-compatible when changing between a varint and non-varint encoding
  • @sffc and @Manishearth suggested some tweaks to the model to handle these cases
  • The first model in Add new version of icu_pattern crate #4579 is simpler and more straightforward and probably efficient for short strings, but frontloading the offsets is likely more efficient for long strings
  • Since deserialization needs to read the whole string to check for UTF-8, there shouldn't be a significant difference in validation speed between the models; the first model could have a more expensive validation in debug mode

placeholder 1 at index 10
placeholder 2 at index 5
(7, 5), some string
(7,5) 01234{1}56{0}789

[(usize,usize);N]
[(7,0),(5,1)] -> [(5,1),(7,0)]

[(usize, &dyn Writeable)]

"AA{1}{0}BB"

(p1, i2) (p0, i2)

p1 p2
(i2, 2ème), (i2, 1ère)

(0x21, 0x20) "AABB"
[(2,1,0),(2,0,1)]

VZV<str>
VZV<placeholderlist>

utils/pattern/src/single.rs Show resolved Hide resolved
utils/pattern/src/single.rs Outdated Show resolved Hide resolved
utils/pattern/src/single.rs Outdated Show resolved Hide resolved
@sffc
Copy link
Member Author

sffc commented Feb 27, 2024

The conflict is from 17f2cb0. Accepting mine.

robertbastian
robertbastian previously approved these changes Feb 27, 2024
let placeholder_offset_char = store.chars().next().ok_or(Error::InvalidPattern)?;
let initial_offset = char::len_utf8(placeholder_offset_char);
let placeholder_offset = placeholder_offset_char as usize;
if placeholder_offset > store.len() - initial_offset + 1 {
Copy link
Member

Choose a reason for hiding this comment

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

Ah I see.

robertbastian
robertbastian previously approved these changes Feb 27, 2024
Copy link
Member

@zbraniecki zbraniecki left a comment

Choose a reason for hiding this comment

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

lgtm! Thanks for picking it up!

@sffc sffc merged commit 59a8e63 into unicode-org:main Feb 27, 2024
30 checks passed
@sffc sffc deleted the neopattern2 branch February 27, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants