From a8ac0d3b4c27e222a8618d542981208a76dacf1f Mon Sep 17 00:00:00 2001 From: nekobit Date: Wed, 12 Oct 2022 15:11:45 +0000 Subject: [PATCH] Memory optimizations (in progress) FossilOrigin-Name: 22237b8da97b3a0053609d84c43b07ac2929beb913f66d8677750eec6b4343cc --- src/base_page.c | 26 +++++++++++++++++++------- src/global_perl.h | 12 ++++++------ src/notifications.c | 8 ++++---- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/base_page.c b/src/base_page.c index 9bb501d..9af03e2 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -69,19 +69,30 @@ void render_base_page(struct base_page* page, FCGX_Request* req, struct session* PERL_STACK_INIT; - if (page->session) - mXPUSHs(newRV_noinc((SV*)page->session)); - else - mXPUSHs(newRV_noinc((SV*)perlify_session(ssn))); - mXPUSHs(newRV_inc((SV*)template_files)); - mXPUSHs(newSVpv(page->content, 0)); + AV* pl_notifs = perlify_notifications(notifs, notifs_len); + SV* pl_notifs_rv = newRV_noinc(pl_notifs); + SV* real_ssn = page->session ? page->session : perlify_session(ssn); + XPUSHs(sv_2mortal(newRV_noinc((SV*)real_ssn))); + + XPUSHs(sv_2mortal(newRV_inc((SV*)template_files))); + SV* content = newSVpv(page->content, 0); + XPUSHs(sv_2mortal(content)); if (notifs && notifs_len) { - mXPUSHs(newRV_noinc((SV*)perlify_notifications(notifs, notifs_len))); + XPUSHs(sv_2mortal(pl_notifs_rv)); } else ARG_UNDEFINED(); + SvREFCNT_dec(pl_notifs_rv); + SvREFCNT_dec(pl_notifs); + SvREFCNT_dec(content); + SvREFCNT_dec(real_ssn); + + av_undef(pl_notifs); + sv_set_undef(pl_notifs_rv); + sv_set_undef(real_ssn); + sv_set_undef(content); // Run function PERL_STACK_SCALAR_CALL("base_page"); @@ -89,6 +100,7 @@ void render_base_page(struct base_page* page, FCGX_Request* req, struct session* send_result(req, NULL, "text/html", dup, 0); + free(page->content); mstdnt_cleanup_notifications(notifs, notifs_len); mastodont_storage_cleanup(&storage); Safefree(dup); diff --git a/src/global_perl.h b/src/global_perl.h index 6c544c6..d30fe98 100644 --- a/src/global_perl.h +++ b/src/global_perl.h @@ -23,9 +23,9 @@ #include // hv_stores(ssn_hv, "id", newSVpv(acct->id, 0)); -#define hvstores_str(hv, key, val) if (val) { hv_stores((hv), key, newSVpv((val), 0)); } -#define hvstores_int(hv, key, val) hv_stores((hv), key, newSViv((val))) -#define hvstores_ref(hv, key, val) if (val) { hv_stores((hv), key, newRV_noinc((SV*)(val))); } +#define hvstores_str(hv, key, val) if (val) { hv_stores((hv), key, sv_2mortal(newSVpv((val), 0))); } +#define hvstores_int(hv, key, val) hv_stores((hv), key, sv_2mortal(newSViv((val)))) +#define hvstores_ref(hv, key, val) if (val) { hv_stores((hv), key, sv_2mortal(newRV_noinc((SV*)(val)))); } /* Seeing all this shit littered in Treebird's code made me decide to write some macros */ #define PERL_STACK_INIT perl_lock(); \ @@ -39,7 +39,7 @@ SPAGAIN /* you MUST assign scalar from savesharedsvpv, then free when done */ -#define PERL_GET_STACK_EXIT savesharedsvpv(POPs); \ +#define PERL_GET_STACK_EXIT savesvpv(POPs); \ PUTBACK; \ FREETMPS; \ LEAVE; \ @@ -49,8 +49,8 @@ if (!(types && len)) return NULL; \ AV* av = newAV(); \ for (size_t i = 0; i < len; ++i) \ - av_push(av, newRV_noinc((SV*)perlify_##type(types + i))); \ - return av; \ + av_push(av, sv_2mortal(newRV_noinc((SV*)sv_2mortal(perlify_##type(types + i))))); \ + return sv_2mortal(av); \ } diff --git a/src/notifications.c b/src/notifications.c index 2cf923f..42f685b 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -191,11 +191,11 @@ HV* perlify_notification(const struct mstdnt_notification* const notif) hvstores_int(notif_hv, "created_at", notif->created_at); hvstores_str(notif_hv, "emoji", notif->emoji); hvstores_str(notif_hv, "type", mstdnt_notification_t_to_str(notif->type)); - hvstores_ref(notif_hv, "account", perlify_account(notif->account)); - hvstores_ref(notif_hv, "pleroma", perlify_notification_pleroma(notif->pleroma)); - hvstores_ref(notif_hv, "status", perlify_status(notif->status)); + hvstores_ref(notif_hv, "account", sv_2mortal(perlify_account(notif->account))); + hvstores_ref(notif_hv, "pleroma", sv_2mortal(perlify_notification_pleroma(notif->pleroma))); +hvstores_ref(notif_hv, "status", sv_2mortal(perlify_status(notif->status))); - return notif_hv; + return (HV*)sv_2mortal((SV*)notif_hv); } PERLIFY_MULTI(notification, notifications, mstdnt_notification)