-
Notifications
You must be signed in to change notification settings - Fork 948
Remove TargetIdGenerator #2818
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
Remove TargetIdGenerator #2818
Conversation
Binary Size ReportAffected SDKs
Test Logs |
e5d7003
to
fca4320
Compare
c3df99e
to
09aaec4
Compare
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.
IMO this change detracts from readability and adds logic duplication throughout the code base. It looked nicer to me that the integer increment by 2 was abstracted away into a TargetIdGenerator class instead of having to be explicitly mentioned elsewhere. It also (in theory) would have allowed for the TargetIdGenerator class to be properly unit tested. Could you provide a bit of explanation as to your reasoning for this change? Does it have to do with tree shaking? Thanks!
this.nextLimboTarget = 1; | ||
} | ||
|
||
nextLimboTargetId(): TargetId { |
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.
nit: Naming the nextLimboTargetId()
method the same as the variable name somewhat implies that it is a simple "getter" method. Consider renaming to something that implies that it is "doing something" not just "getting something". e.g. generateLimboTargetId()
.
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.
Renamed to generateLimboTargetId
.
@@ -170,8 +175,8 @@ export class SpecBuilder { | |||
return this.currentClientState; | |||
} | |||
|
|||
private get limboIdGenerator(): TargetIdGenerator { | |||
return this.clientState.limboIdGenerator; | |||
private nextLimboTargetId(): TargetId { |
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.
Same here; consider renaming the nextLimboTargetId()
method.
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.
Done
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.
I updated the PR a bit to address some of your concerns. I ended up factoring out the "+2" logic into its own function.
TargetIdGenerator
was always a bit overblown (it even used to use bitmasks) and together with its tests it added quite a bit of code to our codebase for what is essentially a very simple sequence number generator.
this.nextLimboTarget = 1; | ||
} | ||
|
||
nextLimboTargetId(): TargetId { |
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.
Renamed to generateLimboTargetId
.
@@ -170,8 +175,8 @@ export class SpecBuilder { | |||
return this.currentClientState; | |||
} | |||
|
|||
private get limboIdGenerator(): TargetIdGenerator { | |||
return this.clientState.limboIdGenerator; | |||
private nextLimboTargetId(): TargetId { |
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.
Done
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.
LGTM. Thank you for factoring out the +2 logic.
/** Target IDs for Limbo resolution are odd. */ | ||
export const TARGET_ID_OFFSET_SYNC_ENGINE: TargetId = 1; | ||
|
||
/** Target IDs for Queries start are even. */ |
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.
This comment doesn't clearly explain why the first value is 2
, when 0
seems like a reasonable first value for even numbers. The comment in target_id_generator is worth preserving:
// We seed the query cache generator to return '2' as its first ID, as there
// is no differentiation in the protocol layer between an unset number and
// the number '0'. If we were to sent a target with target ID '0', the
// backend would consider it unset and replace it with its own ID.
@@ -153,3 +154,20 @@ export class Target { | |||
); | |||
} | |||
} | |||
|
|||
/** Target IDs for Limbo resolution are odd. */ | |||
export const TARGET_ID_OFFSET_SYNC_ENGINE: TargetId = 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.
These aren't "offsets" so much as "initial values". How about INITIAL_TARGET_ID_FOR_SYNC_ENGINE
or SYNC_ENGINE_INITIAL_TARGET_ID
?
Also, for whatever reason we spell BATCHID_UNKNOWN
with no underscore before ID
. Let's make these consistent.
That name also puts BATCHID first so maybe this should be TARGETID_INITIAL_FOR_SYNC_ENGINE
or TARGETID_SYNC_ENGINE_INITIAL
?
Gah! Sorry for the bike shed.
@@ -809,7 +808,8 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer { | |||
this.activeLimboTargetsByKey.size < this.maxConcurrentLimboResolutions | |||
) { | |||
const key = this.enqueuedLimboResolutions.shift()!; | |||
const limboTargetId = this.limboTargetIdGenerator.next(); | |||
const limboTargetId = this.nextLimboTargetId; |
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.
One thing that was nice about the separate class was that it fully encapsulated this post-increment semantic (that is: get the current value and then bump). Now different locations are using this differently and it's not obvious how this is supposed to work.
In particular, it's a little weird that MemoryTargetCache
doesn't use the appropriate TARGET_ID
initializer and doesn't do the post-increment dance, though I guess that's sort of intended?
It's also telling that the client state implements this as a method.
In several cases in this review I had to work to convince myself that this was going to generate the correct initial value and this is uncomfortable.
I bet you'd get most of the size benefit (possibly a bit more), just by simplifying the internals, having a constructor that just takes the initial values you've exported, and possibly removing seek
/after
and having those callers just new up a new TargetIdGenerator
with their desired seed.
34e3971
to
e523c99
Compare
e523c99
to
5797b97
Compare
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.
Updated the PR to revert the removal of the generator class. I simplified the class, which still shaves off a tiny bit of size and a fair bit of mental overhead.
Nonetheless, I am still not 100% convinced that we need our own type, especially since the complicated part of the PR was that I had to manually ensure that the Target IDs generated by the Spec Tests and by the SDK itself match. By sharing the generator logic, we now again rely on the generators to verify their own target IDs.
Either way, I don't feel strongly and spending another hour to shave off 100 bytes is definitely not worth it :)
'Cannot supply target ID from different generator ID' | ||
); | ||
this.nextId = targetId; | ||
this.lastId += OFFSET; |
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.
This is functionally different from what came before. This is now a pre-increment where before it was post-increment. Just double-checking: is this intended?
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.
Yes. The target ID generator now gets initialized with 0 and -1 (shudder), which works around this inconsistency.
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.
LGTM
This PR removes the TargetIdGenerator, which offered a fancy way to add +2 to integers.