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

Avoid serializing object-based payload into PacketByteBuf on the main thread #3407

Merged
merged 7 commits into from
Nov 26, 2023

Conversation

deirn
Copy link
Member

@deirn deirn commented Nov 6, 2023

Resolves #3335

graph TD
  ResolvablePayload["<i>ResolvablePayload</i>"] --> RetainedPayload["RetainedPayload(Identifier, PacketByteBuf)"]
  
  ResolvablePayload --> ResolvedPayload["<i>ResolvedPayload</i>"]
  RetainedPayload -.-> ResolvedPayload
  
  ResolvedPayload --> TypedPayload["TypedPayload(FabricPacket)"]
  ResolvedPayload --> UntypedPayload["UntypedPayload(Identifier, PacketByteBuf)"]
Loading

On local connection (singleplayer), ResolvedPayload instances will be used directly, bypassing the serialization altogether.

On remote connection, the sender sends a ResolvedPayload instance. The receiver then receives a PacketByteBuf that gets retained and wrapped into RetainedPayload. This doesn't create a new buffer, unlike the current implementation.

Then the receiver checks for instance of ResolvablePayload and resolves it into ResolvedPayload if it's not yet as such.

  • For TypedPayload, PacketType#read will be called.
  • For UntypedPayload, the buffer from RetainedPayload will be copied into a new one.

Then the handler for the payload will be called as usual.

@Technici4n
Copy link
Member

Does this add a way to enable serialization for singleplayer, for example in dev?

@deirn
Copy link
Member Author

deirn commented Nov 7, 2023

Does this add a way to enable serialization for singleplayer, for example in dev?

No, is it really needed? If you want to check for serialization you can always run the server.

@deirn
Copy link
Member Author

deirn commented Nov 7, 2023

One thing that I forgot to mention: if you send a packet using raw buffer when it's registered using the PacketType it will fail on local connection as it expects it to be TypedPayload when it is a UntypedPayload. Should we also handle the conversion?

Not true anymore, it will now convert between the two accordingly. Still, should it really do so?

@deirn deirn added the fabric-networking Pull requests and issues related to the networking api label Nov 7, 2023
@deirn deirn requested a review from a team November 7, 2023 15:42
@modmuss50 modmuss50 self-requested a review November 19, 2023 14:31
@modmuss50
Copy link
Member

Does this add a way to enable serialization for singleplayer, for example in dev?

No, is it really needed? If you want to check for serialization you can always run the server.

I think by default it should always serialise when in dev, with a system property to control the behaviour.

@deirn deirn force-pushed the channeled-addon-refactor branch from 8cf5c72 to 3573e5a Compare November 20, 2023 06:57
Copy link
Member

@modmuss50 modmuss50 left a comment

Choose a reason for hiding this comment

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

PR is looking good now, I just want to test it out myself next. One small thing regarding the logging around the new system prop.

Comment on lines 56 to 60
if (FORCE_PACKET_SERIALIZATION) {
LOGGER.warn("Force Packet Serialization is enabled");
if (FabricLoader.getInstance().isDevelopmentEnvironment()) LOGGER.warn("This is the default on dev env");
LOGGER.warn("Disable it with -Dfabric-api.networking.force-packet-serialization=false");
}
Copy link
Member

Choose a reason for hiding this comment

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

Im not sure these need to be warning levels, or logged at all in development tbh.

Copy link
Member Author

Choose a reason for hiding this comment

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

How does one know to disable the serialization then? I guess most devs won't need to be concerned about this but still.

Copy link
Member

Choose a reason for hiding this comment

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

I would imagine that 99% of people wont care about this. A single .info log line would be fine, people see warnings and think they have done something wrong. Whats here makes it sound like it should be disabled not that it can be disabled.

Copy link
Member

Choose a reason for hiding this comment

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

Why would one want to disable it in the first place? ... ;)

Copy link
Contributor

@Patbox Patbox Nov 22, 2023

Choose a reason for hiding this comment

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

To make sure it doesn't bug on singleplayer, because of improper/not using copy of an object?

@modmuss50 modmuss50 added the merge me please Pull requests that are ready to merge label Nov 22, 2023
@modmuss50
Copy link
Member

I will initally release this as a beta, many thanks for sorting this. 👍

@modmuss50 modmuss50 merged commit 6225d43 into FabricMC:1.20.2 Nov 26, 2023
5 checks passed
modmuss50 pushed a commit that referenced this pull request Nov 26, 2023
…in thread (#3407)

* channeled network addon refactor

* checkstyle

* fix junit tests

* convert TypedPayload <-> UntypedPayload if necessary

* assert payload size

* add vm arg to force serialization

* change log level to info and make it single line

(cherry picked from commit 6225d43)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fabric-networking Pull requests and issues related to the networking api merge me please Pull requests that are ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement] Serialize custom packet in networking thread instead of the client/server thread
4 participants