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

overrideTags is highly memory inefficient #214

Open
zen0wu opened this issue Sep 24, 2021 · 1 comment
Open

overrideTags is highly memory inefficient #214

zen0wu opened this issue Sep 24, 2021 · 1 comment

Comments

@zen0wu
Copy link

zen0wu commented Sep 24, 2021

Based on our profiling result, it accounts for 5% of total memory allocation in our production servers.

Based on the implementation here,

hot-shots/lib/helpers.js

Lines 33 to 43 in f28d646

formatTags(child, telegraf).forEach(tag => {
const idx = typeof tag === 'string' ? tag.indexOf(':') : -1;
if (idx < 1) { // Not found or first character
toAppend.push(tag);
} else {
const key = tag.substring(0, idx);
const value = tag.substring(idx + 1);
childCopy[key] = childCopy[key] || [];
childCopy[key].push(value);
}
});

It generates many intermediate strings by parsing the given formatted tags.

  • When an array of tag pairs are parsed, [ "a:b", "c:d" ], it reparses them into { "a": "b", "c": "d" } and then reforms the strings
    • [ "a:b", "c:d" ] -> { "a": "b", "c": "d" } -> [ "a:b", "c:d" ]
  • When an object is passed, it first format the tags using formatTags and then do the above. This is extra bad since it allocates "a:b", then take it apart, then reform "a:b". Might be the same memory wise due to v8's string interning, but at least it wastes many cpu cycles.
    • { "a": "b", "c": "d" } -> [ "a:b", "c:d" ] -> { "a": "b", "c": "d" } -> [ "a:b", "c:d" ]
@bdeitte
Copy link
Collaborator

bdeitte commented Jun 20, 2022

Sorry for the slow response on this, and thanks for the info. As a heads up, no one is fixing any of the issues that come up here- this is all community-driven. So if you are interested in doing this, please feel free. Thanks.

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

2 participants