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
Storage: fix clear and removeItem #1136
Conversation
The mutations array usually contains an index into StringContext for value of key/value in the operation. Historically, "delete" was modeled as 0th index into the array. Unfortunately, this didn't make sense if the first SET op was genuinely referring to the 0th string value (See #1035). While this fixed the first get/set operation, it also meant that deletes were no longer possible! This PR now models the delete operation as a -1 in the unsigned mutation array, corresponding to 2^16-1.
src/transfer/Messages.ts
Outdated
@@ -107,3 +107,7 @@ export const enum ResolveOrReject { | |||
RESOLVE = 1, | |||
REJECT = 2, | |||
} | |||
|
|||
// Mutations are are modeled as Uint16Array. | |||
// Deletion is specified as a -1, which when placed into the array becomes 2^16-1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we setting them to -1
in the first place? Wouldn't 0
work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b/c then 0 would be overloaded and impossible to distinguish between "remove" or 0th index into Strings array. by picking the last index we make the issue much less likely.
An alternative could be to always have null
as the first element of the strings array (reserved to mean deletions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just define it as index as the + 1
of the strings index? Then 0 is impossible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always sending over +1 from the worker seems messier on both ends than reserving the 0th index. WDYT
update / background Sometimes it is important to model a "null" as the string value. This PR reserves the 0th slot in main-thread StringContext, and starts the count of the worker-thread from 1. This was somewhat suggested by #1136 (comment) as a better alternative than reserving the last slot. |
2e73514
to
ad8ca58
Compare
After a discussion with @jridgewell, we agreed that while reserving 0th slot may be simpler, it could have unexpected ramifications to the codebase vs isolating to a single processor. In a58f111 I made the index +1 in worker, -1 in main-thread as suggested earlier. Can do the 0 reservation in a future PR |
summary
Fixes: ampproject/amphtml#37739
The mutations array usually contains an index into StringContext for
value of key/value in the operation. Historically, "delete" was
modeled as 0th index into the array. Unfortunately, this didn't make
sense if the first op was genuinely referring to the 0th string
value (See #1035).
While #1035 fixed the first get/set operation, it also broke delete operations!
This PR now models the delete operation as a -1 in the unsigned mutation array, corresponding to 2^16-1.
The obvious flaw would be if we ever had that many mutations being transferred at once.
From what I can tell, thats more of a theoretical issue than a real one.