Skip to content

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

Merged
merged 6 commits into from
Apr 2, 2020
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Mar 28, 2020

This PR removes the TargetIdGenerator, which offered a fancy way to add +2 to integers.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Mar 28, 2020

Binary Size Report

Affected SDKs

SDKTypeBase (7b17e03)Head (49624d6)Diff
@firebase/firestore/memorybrowser212770.00212492.00-278.00 (-0.13%)
module211136.00210858.00-278.00 (-0.13%)
esm2017169785.00169548.00-237.00 (-0.14%)
main376777.00376232.00-545.00 (-0.14%)
@firebase/firestorebrowser269110.00268827.00-283.00 (-0.11%)
module267081.00266798.00-283.00 (-0.11%)
esm2017215496.00215260.00-236.00 (-0.11%)
main487448.00486884.00-564.00 (-0.12%)
firebasefirebase.js846328.00846043.00-285.00 (-0.03%)
firebase-firestore.memory.js256172.00255892.00-280.00 (-0.11%)
firebase-firestore.js311240.00310955.00-285.00 (-0.09%)
Metric Unit: byte

Test Logs

@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/removegenerators branch from e5d7003 to fca4320 Compare March 28, 2020 03:00
Copy link
Contributor

@dconeybe dconeybe left a 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 {
Copy link
Contributor

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().

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian left a 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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@dconeybe dconeybe left a 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. */
Copy link
Contributor

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;
Copy link
Contributor

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;
Copy link
Contributor

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.

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Apr 1, 2020
@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/removegenerators branch from 34e3971 to e523c99 Compare April 1, 2020 05:24
@schmidt-sebastian schmidt-sebastian force-pushed the mrschmidt/removegenerators branch from e523c99 to 5797b97 Compare April 1, 2020 05:27
Copy link
Contributor Author

@schmidt-sebastian schmidt-sebastian left a 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;
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@wilhuff wilhuff assigned schmidt-sebastian and unassigned wilhuff Apr 2, 2020
@schmidt-sebastian schmidt-sebastian merged commit d69f79c into master Apr 2, 2020
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/removegenerators branch April 2, 2020 23:36
@firebase firebase locked and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants