Bug 1850505 - Avoid extra copy in mozHunspellFileMgrHost::GetLine r=emilio

This was first designed to also positively impact
-ftrivial-auto-var-init by getting rid of the nsAutoCString buffer
initialization in favor of an std::string, but we ended up replacing the
initial std::string buffer by an nsAutoCString which should perform
better on small lines.

Differential Revision: https://phabricator.services.mozilla.com/D187032
This commit is contained in:
serge-sans-paille 2023-08-29 19:34:35 +00:00
parent 87f3a025e7
commit 5d02b07c1e
2 changed files with 8 additions and 15 deletions

View file

@ -39,7 +39,7 @@ Result<Ok, nsresult> mozHunspellFileMgrHost::Open(
return Ok(); return Ok();
} }
Result<Ok, nsresult> mozHunspellFileMgrHost::ReadLine(nsCString& aLine) { Result<Ok, nsresult> mozHunspellFileMgrHost::ReadLine(nsACString& aLine) {
if (!mStream) { if (!mStream) {
return Err(NS_ERROR_NOT_INITIALIZED); return Err(NS_ERROR_NOT_INITIALIZED);
} }
@ -67,15 +67,8 @@ Result<int64_t, nsresult> mozHunspellFileMgrHost::GetSize(
return ret; return ret;
} }
bool mozHunspellFileMgrHost::GetLine(std::string& aResult) { bool mozHunspellFileMgrHost::GetLine(nsACString& aResult) {
nsAutoCString line; return !ReadLine(aResult).isErr();
auto res = ReadLine(line);
if (res.isErr()) {
return false;
}
aResult.assign(line.BeginReading(), line.Length());
return true;
} }
/* static */ /* static */
@ -159,20 +152,20 @@ tainted_hunspell<bool> mozHunspellCallbacks::GetLine(
tainted_hunspell<char**> t_aLinePtr) { tainted_hunspell<char**> t_aLinePtr) {
mozHunspellFileMgrHost& inst = mozHunspellFileMgrHost& inst =
mozHunspellCallbacks::GetMozHunspellFileMgrHost(t_aFd); mozHunspellCallbacks::GetMozHunspellFileMgrHost(t_aFd);
std::string line; nsAutoCString line;
bool ok = inst.GetLine(line); bool ok = inst.GetLine(line);
// If the getline fails, return a null which is "graceful" failure // If the getline fails, return a null which is "graceful" failure
if (ok) { if (ok) {
// Copy the line into the sandbox. This memory is eventually freed by // Copy the line into the sandbox. This memory is eventually freed by
// hunspell. // hunspell.
size_t size = line.size() + 1; size_t size = line.Length() + 1;
tainted_hunspell<char*> t_line = aSandbox.malloc_in_sandbox<char>(size); tainted_hunspell<char*> t_line = aSandbox.malloc_in_sandbox<char>(size);
if (t_line == nullptr) { if (t_line == nullptr) {
// If malloc fails, we should go to "graceful" failure path // If malloc fails, we should go to "graceful" failure path
ok = false; ok = false;
} else { } else {
rlbox::memcpy(aSandbox, t_line, line.c_str(), size); rlbox::memcpy(aSandbox, t_line, line.get(), size);
} }
*t_aLinePtr = t_line; *t_aLinePtr = t_line;
} else { } else {

View file

@ -32,7 +32,7 @@ class mozHunspellFileMgrHost final {
explicit mozHunspellFileMgrHost(const nsCString& aFilename); explicit mozHunspellFileMgrHost(const nsCString& aFilename);
~mozHunspellFileMgrHost() = default; ~mozHunspellFileMgrHost() = default;
bool GetLine(std::string& aResult); bool GetLine(nsACString& aResult);
int GetLineNum() const { return mLineNum; } int GetLineNum() const { return mLineNum; }
static Result<int64_t, nsresult> GetSize(const nsCString& aFilename); static Result<int64_t, nsresult> GetSize(const nsCString& aFilename);
@ -42,7 +42,7 @@ class mozHunspellFileMgrHost final {
const nsCString& aPath, nsCOMPtr<nsIChannel>& aChannel, const nsCString& aPath, nsCOMPtr<nsIChannel>& aChannel,
nsCOMPtr<nsIInputStream>& aStream); nsCOMPtr<nsIInputStream>& aStream);
mozilla::Result<mozilla::Ok, nsresult> ReadLine(nsCString& aLine); mozilla::Result<mozilla::Ok, nsresult> ReadLine(nsACString& aLine);
int mLineNum = 0; int mLineNum = 0;
nsCOMPtr<nsIInputStream> mStream; nsCOMPtr<nsIInputStream> mStream;