Closed Bug 1575343 Opened 2 years ago Closed 2 years ago

Avoid extra copy when calling nsJSUtils::GetCallingLocation()'s nsACString overload


(Core :: DOM: Core & HTML, task)

Not set



Tracking Status
firefox70 --- fixed


(Reporter: mccr8, Assigned: mccr8)




(3 files)

It looks like a number of callers of nsJSUtils::GetCallingLocation()'s nsACString overload do this:

nsAutoCString spec;
nsJSUtils::GetCallingLocation(cx, spec, ...);
... NS_ConvertUTF8toUTF16(spec) ...

This copies the file name into the Cstring, then makes another copy to turn it into a cstring. If I'm reading the code correctly, this could be changed to this to avoid that intermediate copy:

nsAutoString spec;
nsJSUtils::GetCallingLocation(cx, spec, ...);
... spec ...

See Also: → 1574972

The nsAString overload of GetCallingLocation directly converts the
original source file name string into an nsAString. A number of
callers that want the source file name in an nsAString are calling the
nsACString overload of GetCallingLocation, then calling
NS_ConvertUTF8toUTF16. This results in an extra intermediate copy of
the original string data.

This code grabs the URI, then tries to overwrite it with information
from GetCallingLocation, then possibly overwrites it again with the
original information, then converts the string.

My patch reorders things so that we try GetCallingLocation() first, so
we only set the values once. In the case where GetCallingLocation()
succeeds it avoids a string copy from 8 to 16 bit.

Pushed by
part 1 - Avoid a gratuitous string copy by calling the nsAString overload of GetCallingLocation. r=smaug
part 2 - Avoid string copies when getting calling location in nsCSPContext. r=smaug
part 3 - Avoid copies in workers and service workers when getting calling location. r=asuth
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla70
You need to log in before you can comment on or make changes to this bug.