Closed Bug 1408853 Opened 8 years ago Closed 5 years ago

Remove nsID::ToString()

Categories

(Core :: XPCOM, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED WONTFIX
Tracking Status
firefox58 --- affected
firefox59 --- ?

People

(Reporter: n.nethercote, Assigned: n.nethercote)

Details

Attachments

(2 files, 1 obsolete file)

nsID::ToString() uses heap allocation. nsId::ToProvidedString() uses stack allocation and is usually as or more convenient.
NS_Result() is infallible, but not obviously so. moz_xstrdup() is more obviously infallible, so this patch replaces the former with the latter and removes redundant failure checks.
Attachment #8918780 - Flags: review?(erahm)
Assignee: nobody → n.nethercote
Status: NEW → ASSIGNED
nsID::ToProvidedString() is just as convenient, sometimes more so, and it avoids a heap allocation.
Attachment #8918784 - Flags: review?(erahm)
nsID::ToProvidedString() is just as convenient, sometimes more so (esp. in combination with nsIDToCString) and it avoids a heap allocation.
Attachment #8918789 - Flags: review?(erahm)
Attachment #8918784 - Attachment is obsolete: true
Attachment #8918784 - Flags: review?(erahm)
Comment on attachment 8918780 [details] [diff] [review] (part 1) - Use moz_xstrdup() in nsJSID Review of attachment 8918780 [details] [diff] [review]: ----------------------------------------------------------------- I have a slight preference for just swapping in `strdup` and keeping fallibility. The code itself looks fine. ::: js/xpconnect/src/XPCJSID.cpp @@ +55,5 @@ > nsJSID::SetName(const char* name) > { > MOZ_ASSERT(!mName || mName == gNoString ,"name already set"); > MOZ_ASSERT(name,"null name"); > + mName = moz_xstrdup(name); I'm inclined to just make this `strdup` and keep fallibility. Is there a better reason to make this infallible other than "it's technically not fallible already because `NS_strdup` is confusing?" Clearly this was *intended* to be fallible. @@ +584,5 @@ > NS_ENSURE_TRUE(registrar, NS_ERROR_FAILURE); > > nsCID* cid; > if (NS_FAILED(registrar->ContractIDToCID(str, &cid))) > return NS_ERROR_FAILURE; It's weird to me that now we handle allocation failure here, but not on the following line.
Attachment #8918780 - Flags: review?(erahm)
Comment on attachment 8918789 [details] [diff] [review] (part 2) - Remove nsID::ToString() Review of attachment 8918789 [details] [diff] [review]: ----------------------------------------------------------------- Looks good, just a question about options for the `*reinterpret_cast<char(*)[NSID_LENGTH]>` casts. ::: js/xpconnect/src/XPCJSID.cpp @@ +81,5 @@ > return NS_ERROR_NULL_POINTER; > } > > if (!mNumber) { > + mNumber = new char[NSID_LENGTH]; If we keep this it should use `moz_xmalloc` which would presumably match an eventual `free`. @@ +82,5 @@ > } > > if (!mNumber) { > + mNumber = new char[NSID_LENGTH]; > + mID.ToProvidedString(*reinterpret_cast<char(*)[NSID_LENGTH]>(mNumber)); Oh dear. WDYT of: > mNumber = moz_xstrdup(nsIDToCString(mID).get()); It's worse perf, but more readable. Otherwise I think we should add a helper function to hide the cast: > nsID::ToProvidedString(const Span<char>& aStr) const { > MOZ_ASSERT(aStr.Length() >= 39); > ToProvidedString(...awful cast...); > } > // ... > mID.ToProvidedString(MakeSpan(mNumber, NSID_LENGTH)); ::: js/xpconnect/src/XPCWrappedJS.cpp @@ +709,5 @@ > XPC_LOG_ALWAYS(("interface name is %s", name)); > if (name) > free(name); > + XPC_LOG_ALWAYS(("IID number is %s", > + nsIDToCString(GetClass()->GetIID()).get())); Nice cleanup, all of these are *much* better. ::: xpcom/ds/nsSupportsPrimitives.cpp @@ +77,5 @@ > { > NS_ASSERTION(aResult, "Bad pointer"); > > if (mData) { > + *aResult = new char[NSID_LENGTH]; Same comment as above.
Attachment #8918789 - Flags: review?(erahm) → feedback+
> I'm inclined to just make this `strdup` and keep fallibility. Is there a > better reason to make this infallible other than "it's technically not > fallible already because `NS_strdup` is confusing?" Clearly this was > *intended* to be fallible. Infallible is something of a default because it's simpler -- less code, fewer cases to worry about. The exception is of course for potentially large allocations, but these are not potentially large.
> If we keep this it should use `moz_xmalloc` which would presumably match an > eventual `free`. Good point. > > + mNumber = moz_xmalloc(NSID_LENGTH); > > + mID.ToProvidedString(*reinterpret_cast<char(*)[NSID_LENGTH]>(mNumber)); > > Oh dear. WDYT of: > > > mNumber = moz_xstrdup(nsIDToCString(mID).get()); > > It's worse perf, but more readable. Otherwise I think we should add a helper > function to hide the cast: > > > nsID::ToProvidedString(const Span<char>& aStr) const { > > MOZ_ASSERT(aStr.Length() >= 39); > > ToProvidedString(...awful cast...); > > } > > // ... > > mID.ToProvidedString(MakeSpan(mNumber, NSID_LENGTH)); Hmm, I don't particularly like either of those. I'll think about this a bit more.
Comment on attachment 8918780 [details] [diff] [review] (part 1) - Use moz_xstrdup() in nsJSID Review of attachment 8918780 [details] [diff] [review]: ----------------------------------------------------------------- Okay. Please update the comment to say "NS_strdup() is infallible..."
Attachment #8918780 - Flags: review+
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: