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

MaybeInaccessibleMessage deserializer is tremendously inefficient #1462

Open
cavallium opened this issue Dec 4, 2024 · 0 comments
Open

MaybeInaccessibleMessage deserializer is tremendously inefficient #1462

cavallium opened this issue Dec 4, 2024 · 0 comments

Comments

@cavallium
Copy link

MaybeInaccessibleMessage deserializes to Message and InaccessibleMessage based on the date field.

You used jackson annotations to define that behavior:

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "date",
        visible = true,
        defaultImpl = Message.class
)
@JsonSubTypes({
        @JsonSubTypes.Type(value = InaccessibleMessage.class, name = "0")
})
public interface MaybeInaccessibleMessage extends BotApiObject {
}

Unfortunately, jackson implementation of JsonTypeInfo.As.EXISTING_PROPERTY stores every date value that it receives in a Map<String, Deserializer>, with every date number mapped to a different deserializer instance based on the value.
That annotation is mainly designed to take a field value with a low cardinality, and not a value with infinite possible different numbers, like a date.

In an instance with a lot of messages, that can lead to a huge overhead and unnecessary memory allocation / usage.

Please, use a different way to deserialize the MaybeInaccessibleMessage, for example using a custom Deserializer instead of the JsonTypeInfo annotation:

public class EfficientMessageDeserializer extends JsonDeserializer<MaybeInaccessibleMessage> {

	@Override
	public MaybeInaccessibleMessage deserialize(JsonParser p, DeserializationContext ctxt)
			throws IOException, JacksonException {
		var msg = p.getCodec().readValue(p, Message.class);
		if (msg.getDate() == 0) {
			return InaccessibleMessage
					.builder()
					.messageId(msg.getMessageId())
					.chat(msg.getChat())
					.date(msg.getDate())
					.build();
		} else {
			return msg;
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant