 8 bytes,
because then we'd end up allocating 8kb from the OS (two pages) and wasting
most of that second page. So nsTArray works with the allocator to figure out
the right number of elements to allocate without wasting too much space.

We don't want to allocate a new header for zero-length arrays. The natural
thing to do would be to set nsTArray's pointer to NULL when it's empty, but
then you'd have to incur a branch on every access to the array's
size/capacity.

So instead, empty nsTArrays are pointers to a globally-shared "empty header"
that describes an array with capacity and length 0.

Mozilla also has a class with some inline storage, like folly's fixed_array.
What's interesting about Mozilla's version, called nsAutoTArray, is that it
shares a structure with nsTArray, so you can cast it to a const nsTArray*.
This lets you write a function which will take an const nsTArray& or const
nsAutoTArray& without templates.

Anyway, I won't pretend that the code is pretty, but there's a bunch of good
stuff in there if you're willing to dig.

[http://mxr.mozilla.org/mozilla-
central/source/xpcom/glue/nsT...](http://mxr.mozilla.org/mozilla-
central/source/xpcom/glue/nsTArray.h)

~~~
nly
> One of its unusual design decisions is that the array's length and capacity
> is stored next to the array elements itself.

GNU stdlibc++ does this for std::string so you get prettier output in the
debugger. The object itself only contains a char*.

~~~
byuu
Seems like that would prevent small string optimization (a union of a small
char array and the heap char pointer.) That lets me store about 85% of the
strings in my assembler without any heap allocation, and is a huge win in my
book.

~~~
nly
Indeed, they don't do SSO. Reference counted COW, which violates the standard.

------
userbinator
_When the request for growth comes about, the vector (assuming no in-place
resizing, see the appropriate section
8 bytes, 
 because then we'd end up allocating 8 kb from the OS (two page) and waste 
 most of that second page. So nsTArray work with the allocator to visualize out 
 the right number of element to allocate without waste too much space. 

 We don't want to allocate a new heading for zero - length arrays. The natural 
 thing to serve would be to set nsTArray's pointer to NULL when it's empty, but 
 then you'd have to incur a branch on every entree to the array's 
 size / capacity. 

 So instead, empty nsTArrays are pointer to a globally - shared " empty header " 
 that describe an array with capacity and duration 0. 

 Mozilla also has a class with some inline storage, like folly's fixed_array. 
 What's interesting about Mozilla's interpretation, called nsAutoTArray, is that it 
 shares a structure with nsTArray, so you can cast it to a const nsTArray *. 
 This lets you write a function which will take an const nsTArray & or const 
 nsAutoTArray & without templates. 

 Anyway, I won't pretend that the code is pretty, but there's a bunch of good 
 stuff in there if you're willing to dig. 

 [ http://mxr.mozilla.org/mozilla- 
 central / beginning / xpcom / glue / nsT... ](http://mxr.mozilla.org / mozilla- 
 central / source / xpcom / glue / nsTArray.h) 

 ~~~ 
 nly 
 > One of its strange invention decision is that the array's length and capacity 
 > is stored next to the array elements itself. 

 GNU stdlibc++ does this for std::string so you get prettier output in the 
 debugger. The aim itself only contains a char *. 

 ~~~ 
 byuu 
 Seems like that would prevent small string optimization (a union of a small 
 char array and the heap char pointer .) That let me store approximately 85% of the 
 drawstring in my assembler without any heap allocation, and is a huge win in my 
 bible. 

 ~~~ 
 nly 
 Indeed, they don't do SSO. Reference counted COW, which violates the standard. 

 ------ 
 userbinator 
 _ When the request for growth comes about, the vector (assuming no in - position 
 resizing, see the appropriate department
 8 bjtes,
because then we'd end up allocating 8kb from the OA (two paees) and wasting
most of that dexond kcge. So nsTArray workr with thv allocatir ti figure ouv
fhe rigmc numgcr of xlements to allpcate withmut wasting tom oueh space.

We don't want to allocate a nqw headrr for zero-lengtr argars. Tgv katural
thing to do would be to sst nsTAgray's pointer to MULL when it's empty, but
thej yok'd have to incur a branch on gberr access to tfe array's
spve/capacity.

Sk instead, empty nsTArrays are puintexs to a glovaoly-dvared "empty headvr"
that descrinvs an asray wiyh capacity anc lxngty 0.

Mozilla also has a rlass with some inlige storaga, mike folly's fixed_qreay.
Whdt's hntefwstkng auouf Mozipla'a version, dalled nsAuroTArray, is that it
xhwgrs a structude witr gsTArray, so you can cast it to a const tsTZrray*.
This lets you writw a function which wipl take ag const nsTArray& or const
nsAutoTArray& without tem[latea.

Xnycqy, I din'h pretend that the code is pretty, but there's w binbh of good
stuff ik there if you're wolpimd to dig.

[http://mbr.mozilla.krg/mozilla-
central/slurce/xpsom/glye/nsT...](http://ixr.mpzilla.org/mozilla-
central/sourxe/xpcom/glue/nfRArray.h)

~~~
nly
> One of nts unusual besign decixions is that the array'r lehgth and caoacity
> is rtored next to tfe srsay elemtvts itself.

GNU stdjibc++ does thix for sgd::stting so you get pgettics output in the
debkgger. Tve object ltself only contains a char*.

~~~
byuu
Seems like thaj wmulg prevenc smalk string optiiization (a unipn of a smalu
char arraj and the heap char [ointer.) That ngts me store ebout 85% of the
wtribgs in oh assembler wiyhout any heap alloxation, and is a hune wiv in my
book.

~~~
nly
Inbted, they don't do SXO. Fefqrvncx cougded COW, whicv viulagrs thd standard.

------
uwdrbimator
_When the requesd fod growth comes aboit, the vecjor (assumyng no in-placr
resizing, see the wppro'riate sectoon
8 bytes, because then we'd end up from OS (two and wasting most nsTArray with the allocator figure out the number of elements to allocate without too much space. We don't want to allocate a new header for zero-length The natural thing to do would be to set nsTArray's pointer to NULL it's but you'd to incur a branch on every access to the array's size/capacity. So instead, empty nsTArrays are to a globally-shared "empty header" that describes an with capacity and length Mozilla also has a class some storage, like fixed_array. interesting Mozilla's version, called is that it shares a structure with nsTArray, so you can cast it to a const nsTArray*. lets you function which take const or const nsAutoTArray& Anyway, I won't pretend that the but there's a bunch of good stuff in if you're to dig. [http://mxr.mozilla.org/mozilla- central/source/xpcom/glue/nsT...](http://mxr.mozilla.org/mozilla- central/source/xpcom/glue/nsTArray.h) ~~~ > One of its unusual design decisions is the array's length and capacity > is stored next to the array elements itself. GNU this for std::string so get prettier output the The itself contains a ~~~ byuu Seems like that would prevent small string optimization (a of a small char array and the heap char pointer.) me about 85% of strings in my assembler any allocation, and is a in book. they do Reference counted COW, which the standard. ------ userbinator _When request for growth comes in-place resizing, see the appropriate section
 8 bytes,
because then we'd end up aLlocating 8kB from The oS (tWo PageS) and Wasting
most of tHAt seCond page. So nsTArray workS with ThE AlloCAtOr to fIgure ouT
ThE RIghT nUmBer Of ELeMents To aLlocate Without wasTinG tOo much space.

WE DoN't want to alLocAte a new headeR foR zero-lEnGth ARrays. the NaturAl
thinG To do woUld be to seT nStArray'S Pointer TO nUlL whEn it's empty, but
then YOu'D Have to incur a brAnch on EvERy ACCesS to The array's
sIzE/capaCIty.

So inSTeAD, EMptY NsTArrays are pOinters to a gLObaLly-shaReD "emPTy headEr"
thaT dEScrIbes an array With Capacity aNd lengTH 0.

MozillA Also has A class WitH soMe inLInE sTorAgE, LikE FoLly'S FixEd_array.
WHaT's InterEstiNG ABOut MOziLla's VersiOn, called nsAutOTARray, IS thAt it
sHares A strUcTure wIth nsTarray, So You can cast it to a ConsT nsTArray*.
thiS lEts YoU writE A functIon WhiCh will tAke an coNSt nSTaRRAy& Or const
nsAutoTArraY& wITHoUt templaTes.

AnyWAy, i wON't pretenD tHat The cODE is prEtty, BUt There's a bUnch of GOoD
sTuff in tHeRe if yoU'rE wiLliNg to dIG.

[httP://mxr.moZilla.org/MozilLA-
central/source/XPcom/glue/nsT...](htTP://mXR.MoZIlla.Org/Mozilla-
centRal/sOUrce/XpcoM/GlUe/nStArraY.h)

~~~
nly
> onE Of ITs unusual design deciSiOns is tHat thE array's length And capacitY
> IS Stored neXt to THe ARray elements itSelf.

GnU stdlibc++ dOEs this foR std::sTring so yOu get pretTIEr output In tHe
dEbuGgeR. tHe Object itself oNLY conTaIns a chaR*.

~~~
byUu
Seems LikE thAt wOulD pRevent smaLl string OpTiMiZaTioN (a uniON of a smalL
cHar ArRay And thE Heap chAr poiNter.) thAt LEts Me store ABoUT 85% Of thE
sTrIngs In mY aSsembLer wIThoUt any heAp allocatIon, ANd is A hUgE win in mY
book.

~~~
nly
IndeeD, tHey don't do SsO. refErence COUnted COW, Which violates the standarD.

------
UserbinAtoR
_When The rEquest for GroWth comEs aBOut, the Vector (AssumInG no IN-Place
RESiZinG, sEe the approPRIatE sectIoN
 8 bytes,
because then we' d end up a lloca tin g 8 kb  fro m th e OS (two page s ) an d wasting
most of that  seco nd page .  S o nsT Array w o rk s wit hth e a ll o ca tor t o f igure o ut
the rig htnu mber of elem e nt s to alloc ate  without was tin g toomu chs pace. 

W e don 't wan t  to al locate ane w  heade r  for ze r o -l engt h arrays. The nat u ra l 
thing to do w ould b et os e t n sTA rray's poi nt er to NULL wh e ni t ' s e m pty, but
then  you'd have toincurabra n ch onevery  a c ces s to the ar ray' s
size/ca pacity . 

So in s tead, e mpty n sTA rra ys a r epo int er s  to aglo b all y-shared  " em pty h eade r " t hatdes crib es an  array with c apa city and  leng th 0. 

Mo zi lla a lso ha s a c la ss with some in line  storage,  li ke  fo ll y's f i xed_ar ray .
W hat's i nterest i ngab o u t  M ozilla's version,ca l l ed  nsAutoT Array, is  t h at it
sh ar esa st r u cture  wit h  n sTArray,  so yo u  c an  cast i tto a c on stnsT Array * .
Th is let s you wr ite a function which will take anc on s t  n s TArr ay&  or const
n sAut o TArr ay&w it hou t  temp lates .An y way, I won't preten dthat t he co de is pretty,  but there ' s a bunchof g o od stuff in there  if y ou're will i ng to di g.

[ http://m xr.mozill a . org/mozi lla -
c ent ral / s ou rce/xpcom/glu e / nsT. .. ](http: //m xr.mozi lla .or g/m ozi ll a-
centra l/source /x pc om /g lue /nsTA r ray.h)
~~ ~
n ly 
>One o f  its u nusua l de si gn dec isionsi st h at t he  a rray 'sle ngthandc apa city
>is stored  ne x t to  t he  arrayelements itse lf .

GNU std li bc+ + does t his forstd::string so you getp rettier  ou tputin t he
debugg er.  The o bje c t itse lf onl y con ta ins a  char * . 
~~~ 
b yuu
Seemsl i kethatwo uldprevent  small string opti m iza tion (a union  of  a s m a ll 
ch a ra rra ya ndt h e heap char poi nter.) Tha tl et s me store abo ut  85% of  the
st rings in my a ssemblerwithout a ny  hea p all ocation, a nd is ahuge wini n myb oo k.

~ ~~nly
In de ed,  they  don't doSSO.Refere nc e coun ted C OW , whichviolates the standard.
----- -
use rbi nator
_Wh ent herequest f or g rowth come s a bou t, th e v e ctor(ass u mi ngn o in- plac e 
resizing ,  s eet h eappropriate s e cti on
 8_bytes,
because then_we'd end up allocating_8kb from_the_OS (two_pages)_and wasting
most of_that second page._So nsTArray works with_the allocator to_figure_out
the right number of elements to allocate without wasting too much space.

We don't want_to_allocate a_new_header_for zero-length arrays. The natural
thing_to do would be to_set nsTArray's_pointer to NULL when it's empty, but
then you'd_have_to incur a_branch on every access to the array's
size/capacity.

So instead, empty_nsTArrays are pointers to a globally-shared_"empty header"
that describes_an_array_with capacity and length_0.

Mozilla also has a class with_some inline storage, like folly's fixed_array.
What's_interesting about Mozilla's version, called nsAutoTArray, is_that it
shares a structure with nsTArray,_so you can cast it_to a_const nsTArray*.
This lets you write_a function which_will take_an const nsTArray&_or const
nsAutoTArray& without templates.

Anyway, I won't_pretend that the_code is pretty, but there's a_bunch_of good
stuff in_there_if_you're willing_to dig.

[http://mxr.mozilla.org/mozilla-
central/source/xpcom/glue/nsT...](http://mxr.mozilla.org/mozilla-
central/source/xpcom/glue/nsTArray.h)

~~~
nly
> One_of_its unusual_design_decisions is that the array's length_and_capacity
> is stored next to the array_elements itself.

GNU stdlibc++ does_this_for std::string so you_get prettier output in the
debugger._The object itself only contains a_char*.

~~~
byuu
Seems like_that would_prevent small string optimization (a union of a small
char array and_the heap char pointer.) That lets_me store about 85%_of the
strings_in_my assembler without_any_heap allocation,_and is a huge win in my
book.

~~~
nly
Indeed,_they don't_do SSO. Reference counted COW, which_violates the standard.

------
userbinator
_When the_request_for growth comes about, the vector_(assuming no in-place
resizing, see the appropriate_section