Currently the styles are supposed to be scoped. This means that each reference to the stylesheet causes the whole stylesheet to be "stripped" (and the size of the stylesheet) is cumulated repeatedly for each reference, even if the stylesheet is finally "deduplicated" by replacing further references to the same stylesheet by a "link" element. But for page that use the same stylesheet repeatedly, this has the same effect (in terms parser limits) as if we had included the same styles repeatedly.
So we still easily reach the 5MB limit of "stripped contents" and we need complex code to determine when to include or not the "templatestyle" element and avoid expanding the stripped contents. And this happens **even** if the stripped content is identical.
Things would be easier if the "<templatestyle>" tag had a simple "scope=content" attribute, saying that the content does not need to be stripped, and that instead the stylesheet will be generated once (preferably in the page header) and that no further including on the page content of additional "link" elements will be needed.
This does not change the rule about rewriting the all selectors found in the stylesheet in the ".mw-content" element (so that these styles cannot be used to override the other parts of the page.
This would save lot of memory, by just assuming that pages using such stylesheet will not use selectors that may conflict with other contents of the page.
Is it possible to add this "scope=content" attribute or possibly allow to specify additional selectors to restrict the stylesheet to other containers of the page ?
Note that stylesheets may be quite long, containing multiple contextual styles for different classes.
So for example the pages about Unicode Tables in French Wikipedia. Now it works but it is still fragile and it requires complex management for tracking when the same stylesheet may be referenced (basically the code ensures that the stylesheet is now referenced only at the start of each table, but no longer for each cell, or table row (a flag is used to indicate when the stylesheet should be included, the absence meaning that the stylesheet is assumed to be already loaded and the classes are then used directly without reincluding the same stylesheet again and again.
Without these flags, the parser rapidly explode the "max stripped size limit", notably when the stylesheet grows in size (for example when adding new styles for other new Unicode scripts for which we define a set of usable fonts for each script, or because new fonts are made available to support that script), or if we make too many identical references to the same stylesheet, from multiple inclusions of templates.
Another way to manage this would be that a stripped content of the stylesheet does not need to be counted multiple times: a single copy will be generated and all further references will reuse the same generated stylesheet ID for which all the parser will have to do is to just generate the "link" element with that existing ID.