Perf: Prevent partial template being sent to frontend

This commit is contained in:
Luc Mcgrady 2025-12-29 23:19:07 +00:00
parent 87650f5767
commit 286daac650
No known key found for this signature in database
GPG key ID: 4F3D7A0B17CC3D9C
3 changed files with 16 additions and 11 deletions

View file

@ -307,6 +307,11 @@ message NextCardDataResponse {
bool stop_on_answer = 2;
}
message PartialTemplate {
repeated card_rendering.RenderedTemplateNode front = 1;
repeated card_rendering.RenderedTemplateNode back = 2;
}
message NextCardData {
QueuedCards queue = 1;
repeated AnswerButton answer_buttons = 2;
@ -334,13 +339,7 @@ message NextCardDataResponse {
deck_config.DeckConfig.Config.QuestionAction autoAdvanceQuestionAction = 18;
deck_config.DeckConfig.Config.AnswerAction autoAdvanceAnswerAction = 19;
// TODO: We can probably make this a little faster by using oneof and
// preventing the partial_front and back being sent to svelte where it isn't
// used. Alternatively we can use a completely different message for both
// Rust -> Python and the Python -> Svelte though this would be more
// complicated to implement.
repeated card_rendering.RenderedTemplateNode partial_front = 10;
repeated card_rendering.RenderedTemplateNode partial_back = 11;
optional PartialTemplate partialTemplate = 11;
}
optional NextCardData next_card = 1;

View file

@ -711,16 +711,19 @@ def next_card_data() -> bytes:
ctx = TemplateRenderContext.from_existing_card(card, False)
qside = apply_custom_filters(
PartiallyRenderedCard.nodes_from_proto(data.next_card.partial_front),
PartiallyRenderedCard.nodes_from_proto(data.next_card.partialTemplate.front),
ctx,
None,
)
aside = apply_custom_filters(
PartiallyRenderedCard.nodes_from_proto(data.next_card.partial_back),
PartiallyRenderedCard.nodes_from_proto(data.next_card.partialTemplate.back),
ctx,
qside,
)
# Dont send the partialy rendered template to the frontend to save bandwidth
data.next_card.ClearField("partialTemplate")
q_avtags = ctx.col()._backend.extract_av_tags(text=qside, question_side=True)
a_avtags = ctx.col()._backend.extract_av_tags(text=aside, question_side=False)

View file

@ -11,6 +11,7 @@ use anki_proto::generic;
use anki_proto::scheduler;
use anki_proto::scheduler::next_card_data_response::AnswerButton;
use anki_proto::scheduler::next_card_data_response::NextCardData;
use anki_proto::scheduler::next_card_data_response::PartialTemplate;
use anki_proto::scheduler::next_card_data_response::TimerPreferences;
use anki_proto::scheduler::next_card_data_response::TypedAnswer;
use anki_proto::scheduler::ComputeFsrsParamsResponse;
@ -499,8 +500,10 @@ impl crate::services::SchedulerService for Collection {
queue: Some(queue.into()),
css: render.css.clone(),
partial_front: rendered_nodes_to_proto(q_nodes),
partial_back: rendered_nodes_to_proto(render.anodes),
partial_template: Some(PartialTemplate {
front: rendered_nodes_to_proto(q_nodes),
back: rendered_nodes_to_proto(render.anodes),
}),
answer_buttons,
autoplay: !deck_config.disable_autoplay,