Closed Bug 263973 Opened 21 years ago Closed 21 years ago

[FIX]Make it possible to use StringUnicharInputStream without allocating

Categories

(Core :: XPCOM, defect, P1)

x86
Linux
defect

Tracking

()

RESOLVED FIXED
mozilla1.8alpha5

People

(Reporter: bzbarsky, Assigned: bzbarsky)

References

Details

Attachments

(1 file, 2 obsolete files)

If we know our string data will outlive the stream, we should be able to tell it to just use the data without taking ownership of it.
Attached patch Patch (obsolete) — Splinter Review
Comment on attachment 161877 [details] [diff] [review] Patch Comments? Note that I don't want to just use nsAString here, with assignment into an nsString, because the string coming from JS when inline style is being set ends up just being an nsDependentString around the JS string ptr/length. Since all the stream _really_ wants is a PRUnichar* pointer and a length, it can use the dependent string without copying, but would have to copy to get an nsString out of it.
Attachment #161877 - Flags: superreview?(dbaron)
Attachment #161877 - Flags: review?(darin)
Priority: -- → P1
Target Milestone: --- → mozilla1.8alpha5
Comment on attachment 161877 [details] [diff] [review] Patch >- nsString* aString); >+ const nsAFlatString* aString, How about nsSubstring? Also, how hard would it be to just make it take const nsSubstring& and assign to its own nsString object, which in many cases wouldn't copy? Would that cause additional copies for any callers? > nsresult StringUnicharInputStream::Close() > { > mPos = mLen; >- if (nsnull != mString) { >+ if (mString && mOwnsString) { > delete mString; >- mString = 0; >+ mString = nsnull; > } You want to assign null outside the if.
(In reply to comment #3) > Also, how hard would it be to just make it take const nsSubstring& and assign > to its own nsString object, which in many cases wouldn't copy? Would that > cause additional copies for any callers? Feel free to ignore this now that I read comment 2.
I can do nsSubstring if that's now the preferred thing, sure. And yes, the assignment should be outside the if. Will do.
Comment on attachment 161877 [details] [diff] [review] Patch >Index: xpcom/io/nsIUnicharInputStream.h >+ * it destroys the string if aTakeOwnership is set. If aTakeOwnership >+ * is not st, you must ensure that the string outlives the stream! "is not set" Do you want to mention how it destroys the string (i.e., that it calls |delete aString;|). > extern NS_COM nsresult > NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult, >+ const nsAFlatString* aString, nsAFlatString is a typedef for nsString, and really serves no purpose anymore. I'd rather see us eliminate nsAFlatString, and just use nsString in its place. Fewer string types is better ;-) also, why not allow |const nsAString *|? you can easily get a single fragment buffer from a nsAString like so: const nsAString &str = ...; nsAString::const_iterator begin; str.BeginReading(begin); const PRUnichar *buf = begin.get(); then you would not need to use PromiseFlatString at the callsites. >Index: xpcom/io/nsUnicharInputStream.cpp >+ NS_PRECONDITION(aString, "null ptr"); ... >+ if (!aString) { > return NS_ERROR_NULL_POINTER; > } maybe just use this instead: NS_ENSURE_ARG_POINTER(aString); r=darin
Attachment #161877 - Flags: review?(darin) → review+
Oh, good point. I keep forgetting "all strings are flat". I'll change to const nsAString& with the iterator fu as needed.
xpcom/io/nsIUnicharInputStream.h If aTakeOwnership + * is not st, you must ensure that the string outlives the stream! "st" should be "set", I suppose :-)
Attached patch Updated to comments (obsolete) — Splinter Review
Attachment #161877 - Attachment is obsolete: true
Attachment #161877 - Flags: superreview?(dbaron)
Attachment #161921 - Flags: superreview?(dbaron)
Comment on attachment 161921 [details] [diff] [review] Updated to comments >+ if (mString && mOwnsString) { > delete mString; > } Some compilers don't like deleting a const pointer. You probably need nsAString* mutable_string = NS_CONST_CAST(nsAString*, mString); delete mutable_string;
Comment on attachment 161921 [details] [diff] [review] Updated to comments with my previous comment (twice), sr=dbaron
Attachment #161921 - Flags: superreview?(dbaron) → superreview+
Attachment #161921 - Attachment is obsolete: true
Fixed.
Status: NEW → RESOLVED
Closed: 21 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: