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

ICU4X data structs don't actually borrow any data #632

Closed
sffc opened this issue Apr 11, 2021 · 3 comments · Fixed by #745 or #820
Closed

ICU4X data structs don't actually borrow any data #632

sffc opened this issue Apr 11, 2021 · 3 comments · Fixed by #745 or #820
Assignees
Labels
C-data-infra Component: provider, datagen, fallback, adapters duplicate This issue or pull request already exists S-medium Size: Less than a week (larger bug fix or enhancement) T-bug Type: Bad behavior, security, privacy
Milestone

Comments

@sffc
Copy link
Member

sffc commented Apr 11, 2021

I discovered that even though we religiously use Cow<'s, str> and similar in our data structs, we are not actually benefiting from zero-copy deserialization, because the default implementation of Deserialize for Cow<'s, str> is to always use Owned data. See a discussion here: serde-rs/serde#1497

We need our structs to support zero-copy deserialization. This requires adding #[serde(borrow)] to opt in where possible, keeping the following issues in mind: serde-rs/serde#2016, serde-rs/serde#723.

Unfortunately, if we make this change, it means that we will no longer be able to read from an std::io::Read when deserializing. We must always load the buffer and then point into the buffer. This might be an okay constraint for ICU4X, but I want to make sure we are explicit about it.

If we want to retain the ability to deserialize into a 'static, we could add functions that flatten the lifetime on the data struct after we deserialize it from a borrowed buffer.

CC @Manishearth

@sffc sffc added C-data-infra Component: provider, datagen, fallback, adapters discuss-priority Discuss at the next ICU4X meeting T-bug Type: Bad behavior, security, privacy labels Apr 11, 2021
@sffc sffc mentioned this issue Apr 15, 2021
@zbraniecki
Copy link
Member

zbraniecki commented Apr 15, 2021

https://docs.serde.rs/serde_json/fn.from_reader.html

Note that counter to intuition, this function is usually slower than reading a file completely into memory and then applying from_str or from_slice on it. See issue #160.

The ToOwned is relatively easy for flat structs like Vec<Cow>> it becomes challenging for nested structs like AST.
In Fluent Rs I worked it around by making it generic over Slice and Slice is implemented for String or Cow:

@sffc sffc added S-medium Size: Less than a week (larger bug fix or enhancement) and removed discuss-priority Discuss at the next ICU4X meeting labels Apr 17, 2021
@sffc
Copy link
Member Author

sffc commented Apr 17, 2021

2021-04-15: We are okay with the constraint that deserializers operating on std::io::Read will not work. @Manishearth is investigating some of the upstream bugs. I will work on the changes to ensure that we can achieve zero-copy deserialization on all of the existing data structs.

@sffc
Copy link
Member Author

sffc commented May 21, 2021

Closing this as a duplicate of #667

@sffc sffc closed this as completed May 21, 2021
@sffc sffc added the duplicate This issue or pull request already exists label May 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-data-infra Component: provider, datagen, fallback, adapters duplicate This issue or pull request already exists S-medium Size: Less than a week (larger bug fix or enhancement) T-bug Type: Bad behavior, security, privacy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants