Open Bug 1225745 Opened 9 years ago Updated 2 years ago

Rename nsAuto{,C}String to nsStack{,C}String

Categories

(Core :: XPCOM, defect)

defect

Tracking

()

People

(Reporter: glandium, Assigned: glandium)

References

(Depends on 1 open bug, Blocks 1 open bug)

Details

(Keywords: dev-doc-needed)

Attachments

(1 file, 1 obsolete file)

One reason why nsAuto{,C}String are poorly used (cf. bug 1225682 + all the uses as function return value or argument I will file another bug about) are likely caused by the poor naming of the type.

Let's fix this by renaming it.
Blocks: 1225746
Can't these uses be prevented via static analysis (MOZ_STACK_CLASS) without roto-tilling the entire tree with a name change?
Are you going to rename every use of Auto in the tree to Stack for stack objects?  Like Mutex::AutoLock, etc?  Otherwise this change is going to make our naming more inconsistent and harder for developers.

Like it or not, we use "Auto" to signal that a class should be on the stack.  You should rename them all or none of them, IMO.
(In reply to Ben Kelly [:bkelly] from comment #2)
> Can't these uses be prevented via static analysis (MOZ_STACK_CLASS) without
> roto-tilling the entire tree with a name change?

That's bug 1225682

(In reply to Ben Kelly [:bkelly] from comment #3)
> Are you going to rename every use of Auto in the tree to Stack for stack
> objects?  Like Mutex::AutoLock, etc?  Otherwise this change is going to make
> our naming more inconsistent and harder for developers.
> 
> Like it or not, we use "Auto" to signal that a class should be on the stack.
> You should rename them all or none of them, IMO.

Auto in those classes has essentially the meaning of "RAII". AutoString has nothing to do with RAII.
That is, nsAutoString is not any more special for RAII than nsString. The Auto part is that if the string is smaller than 64 characters, it's store inside the nsAutoString object itself instead of on the heap.
So you're changing nsAutoTArray<> as well?
I'm not sure a complete name change here is worthwhile.

(In reply to Ben Kelly [:bkelly] from comment #6)
> So you're changing nsAutoTArray<> as well?

I think nsAutoTArray should not be renamed (despite the inconsistency), because it at least lets the user control the amount of storage.  nsAuto*String has no such knob...though it's not entirely clear to much just how much space these are wasting...perhaps glandium has some data.

mozilla::Vector has the better design here of letting you just specify an amount of inline storage and dispensing with the "Auto" concept entirely.  I think LLVM's classes use "Small" for things with inline storage (SmallVec, SmallString, etc.).
Flags: needinfo?(mh+mozilla)
> Like it or not, we use "Auto" to signal that a class should be on the stack.

As Mike said, our usage of "Auto" is inconsistent and confusing. In many cases (e.g. AutoLock, nsAutoPtr) it means "this is an RAII type". (MFBT also used "Scoped" for this.) IMO this is the "normal" meaning of "Auto", and isn't too bad, though still potentially confusing.

In the case of nsAutoString, it means "this string has inline chars". IMO this is a terrible use of "Auto", being totally non-obvious. And there are many misuses of nsAutoString it in the code, e.g. for function arguments, which demonstrates that people are misunderstanding what it's meant to be for.

I'm not aware of any cases where "Auto" means "this should be on the stack". (RAII types are often used on the stack, but that's not mandatory.)


> You should rename them all or none of them, IMO.

Getting rid of all confusing uses of "Auto" is a good idea. But I disagree that they need to all be done at once. "Perfect is the enemy of good" and all that.
> I think nsAutoTArray should not be renamed (despite the inconsistency),
> because it at least lets the user control the amount of storage. 
> nsAuto*String has no such knob...though it's not entirely clear to much just
> how much space these are wasting...perhaps glandium has some data.

The "control the amount of storage" part is entirely orthogonal. As I said in comment 8, in our codebase "Auto" mostly means "RAII type". Having it mean something else for some types is horribly confusing.

(I fully admit to only just now fully understanding what nsAutoString and nsAutoTArray are. I always vaguely assumed they were RAII types somehow -- though I'm not even sure what that would mean for strings and arrays -- and I never really looked closely enough at them to know for sure.)
(In reply to Nathan Froyd [:froydnj] from comment #7)
> I'm not sure a complete name change here is worthwhile.

We don't run static analysis on local builds. You can be sure that keeping the ambiguous name will lead people to burn static analysis when landing their changes.

(In reply to Nathan Froyd [:froydnj] from comment #7)
> I'm not sure a complete name change here is worthwhile.
> 
> (In reply to Ben Kelly [:bkelly] from comment #6)
> > So you're changing nsAutoTArray<> as well?
> 
> I think nsAutoTArray should not be renamed (despite the inconsistency),
> because it at least lets the user control the amount of storage. 
> nsAuto*String has no such knob...though it's not entirely clear to much just
> how much space these are wasting...perhaps glandium has some data.

From a quick test it looks like it's a few dozen kilobytes of waste for a small session. Nothing too catastrophic, but that doesn't make it a justification to keep the current status quo.
Flags: needinfo?(mh+mozilla)
This is how I created the patch:

git grep -l nsAutoString | xargs sed -i s/nsAutoString/nsStackString/g
git grep -l nsAutoCString | xargs sed -i s/nsAutoCString/nsStackCString/g
git grep -l nsTAutoString | xargs sed -i s/nsTAutoString/nsTStackString/g

Then I adjusted xpcom/string/string-template-def-char.h and xpcom/string/string-template-def-unichar.h for spaces.
Assignee: nobody → mh+mozilla
Attachment #8689851 - Flags: review?(nfroyd)
I don't know why, but the previous one wasn't on top of the patch that adds MOZ_STACK_CLASS to nsTAutoString_CharT for some reason.
Attachment #8689851 - Attachment is obsolete: true
Attachment #8689851 - Flags: review?(nfroyd)
Attachment #8689854 - Flags: review?(nfroyd)
Comment on attachment 8689854 [details] [diff] [review]
Rename nsAuto{,C}String to nsStack{,C}String

Review of attachment 8689854 [details] [diff] [review]:
-----------------------------------------------------------------

Given comments in the dependent bug, I'm not so sure this patch is going to fly.
Attachment #8689854 - Flags: review?(nfroyd)
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: