Open Bug 1868110 Opened 2 years ago Updated 2 years ago

getTextAtLineColumn can be slow with out of bound arguments / massive lines

Categories

(DevTools :: Inspector, defect, P2)

defect

Tracking

(Not tracked)

People

(Reporter: emilio, Unassigned)

References

Details

See bug 1867816 comment 9 for a concrete example where the wrong stylesheet text causes getTextAtLineColumn to be super slow. That example had a 12k character long line.

Maybe we should find a better way to deal with it, like processing the CSS text only once, or caching line -> offset mappings so that we don't need to do the search from the beginning all the time?

That seems like it'd be a rather straight-forward improvement.

Bug 1867816 comment 6 has a repro which is trivial if you revert the patch for that bug.

Duplicate of this bug: 1868210

Here's a profile showing the issue: https://share.firefox.dev/3Gub6I9
And here is the slow part:

https://searchfox.org/mozilla-central/rev/78a2c17cc80680a5a82446e4ce7c45a73b935383/devtools/server/actors/utils/style-utils.js#197,199-203

function getTextAtLineColumn(text, line, column) {
...
  if (line > 1) {
    const rx = new RegExp(
      "(?:[^\\r\\n\\f]*(?:\\r\\n|\\n|\\r|\\f)){" + (line - 1) + "}"
    );
    offset = rx.exec(text)[0].length;

Emilio, could we have a InspectorUtils method that would get us the start and end offset of the rules, similar to what we already have for line and column https://searchfox.org/mozilla-central/rev/78a2c17cc80680a5a82446e4ce7c45a73b935383/devtools/server/actors/style-rule.js#91-92 ?

Flags: needinfo?(emilio)

So... yes, but with an asterisk.

As a matter of fact, it'd be easier for the style system to keep around just the offset rather than line + column. However, the main issue there is that the CSS parser works in utf-8 and JS strings are utf-16. So we'd need to track the position plus the position in utf16 which would be rather unfortunate. We track utf-16 columns and that's already some amount of pain, see this...

Would it be possible for DevTools to use utf-8 offsets rather than 16? If so that'd make it a lot easier for the CSS parser...

Flags: needinfo?(emilio) → needinfo?(nchevobbe)

(In reply to Emilio Cobos Álvarez (:emilio) from comment #3)

As a matter of fact, it'd be easier for the style system to keep around just the offset rather than line + column. However, the main issue there is that the CSS parser works in utf-8 and JS strings are utf-16. So we'd need to track the position plus the position in utf16 which would be rather unfortunate. We track utf-16 columns and that's already some amount of pain, see this...

So I think we'd still have use for the column as we display it in the UI, but maybe we could compute it? not sure

Would it be possible for DevTools to use utf-8 offsets rather than 16? If so that'd make it a lot easier for the CSS parser...

not sure either, we could try to use TextEncoder/Decoder but I don't have experience with those and I don't know if this would be fast enough.
But we can definitely try it if adding the InspectorUtils wouldn't take too long

Flags: needinfo?(nchevobbe)
Severity: -- → S3
Priority: -- → P2
You need to log in before you can comment on or make changes to this bug.