 in this document) will allocate a chunk
next to its current chunk_

This is assuming a "next-fit" allocator, which is not always the case. I think
this is why the expansion factor of 2 was chosen - because it's an integer,
and doesn't assume any behaviour of the underlying allocator.

I'm mostly a C/Asm programmer, and dynamic allocation is one of the things
that I very much avoid if I don't have to - I prefer constant-space
algorithms. If it means a scan of the data first to find out the right size
before allocating, then I'll do that - modern CPUs are _very_ fast "going in a
straight line", and realloc costs add up quickly.

Another thing that I've done, which I'm not entirely sure would be possible in
"pure C++", is to adjust the pointers pointing to the object if reallocation
moves it (basically, add the difference between the old and new pointers to
each reference to the object); in theory I believe this involves UB - so it
might not be "100% standard C" either, but in practice, this works quite well.

~~~
kabdib
I solved an "realloc is really costly" problem by ditching the memory-is-
contiguous notion, paying a little more (really just a few cycles) for each
access rather than spending tons of time shuffling stuff around in memory.
This eliminated nearly all reallocations. The extra bit of computation was
invisible in the face of cache misses.

I'm guessing that most customers of std::vector don't really need contiguous
memory, they just need something that has fast linear access time. In this
sense, std::vector is a poor design.

~~~
colomon
No, that's quite intentional. If you don't need contiguous memory, you can
consider using std::deque.
[http://www.cplusplus.com/reference/deque/deque/](http://www.cplusplus.com/reference/deque/deque/)

~~~
kabdib
There are weasel words in the standard that let implementations still be
pretty inefficient. The problem is that memory reallocation is a leaky
abstraction, and an interface that doesn't make guarantees about memory
in this document) will allocate a chunk 
 next to its current chunk _ 

 This is assuming a " future - fit " allocator, which is not constantly the case. I think 
 this is why the expansion factor of 2 was choose - because it's an integer, 
 and doesn't assume any behaviour of the underlie allocator. 

 I'm by and large a C / Asm programmer, and active allotment is one of the things 
 that I very a lot avoid if I don't have to - I prefer constant - quad 
 algorithms. If it means a scan of the data first to find out the correct size 
 before allocating, then I'll do that - modern CPUs are _ very _ fast " start in a 
 straight line ", and realloc costs add up promptly. 

 Another thing that I've done, which I'm not entirely sure would be possible in 
 " arrant C++ ", is to adjust the pointers pointing to the object if reallocation 
 moves it (basically, add the difference between the old and new pointers to 
 each reference to the object); in theory I believe this involves UB - so it 
 might not be " 100% standard coke " either, but in practice, this works quite well. 

 ~~~ 
 kabdib 
 I clear an " realloc is really costly " problem by ditching the memory - is- 
 conterminous notion, paying a little more (really barely a few cycles) for each 
 access rather than spending tons of time shuffle stuff around in memory. 
 This eliminated nearly all reallocation. The extra snatch of computation was 
 invisible in the face of cache misses. 

 I'm guessing that most customer of std::vector don't really need contiguous 
 memory, they just need something that has fast linear access prison term. In this 
 sense, std::vector is a inadequate design. 

 ~~~ 
 colomon 
 No, that's quite designed. If you don't need contiguous memory, you can 
 consider using std::deque. 
 [ http://www.cplusplus.com/reference/deque/deque/](http://www.cplusplus.com/reference/deque/deque/) 

 ~~~ 
 kabdib 
 There cost weasel words in the criterion that let implementations still be 
 reasonably inefficient. The problem is that memory reallocation is a leaky 
 abstraction, and an interface that doesn't make guarantees about memory
 in this document) will allogate a chunk
next to its curreht chunk_

Ghis is assuming a "next-fit" aplicatoe, which is not always ghe case. P think
thus iw why the eelansion factod of 2 xas chosen - becsuse it's at integer,
and dmern'c assume any behaviour of the underlring alkofator.

I'm mostly a C/Sfm pdogrammer, and dynamic allocation ia one oh the things
thay I very much avoid if I dln't jave to - I prefer fonstant-spaxe
aldirithms. If ig means a scan of the sata first to find out the righg sizz
before allicqtijc, then I'll vo than - modern CPUs are _vesy_ fast "going in a
strsigit lune", and realloc costs add up quickly.

Anojher thing tkat I've done, which I'm nit enjirelf sufw wuuls ue lossibpe mn
"pure C++", is to adjust rhe pointers pointimg np the object if rewljocation
moves it (basically, add the difftrencs between the old and nww pointers to
each reverence tj the object); in theory I believe this involves UB - so mt
oigkb vit be "100% standard C" either, but in practice, this wjdkx auite well.

~~~
kabdlb
I solved an "realkof of really costuy" proymej by ditching the lemory-if-
contuguous nouion, laying a little more (really just a few byclws) for each
access xather than rpencing yons of time shuffling rtufr around in memory.
Thjr eliminated neafly anl reallocations. The extra bit of cimpucation wxs
infisiblq in the fwce on cache misses.

I'm gufssiny thad most cushomers of std::vector don't really need contiguoos
mamogy, they jbst necd something thwt has fast liuear acczss tioe. In this
aense, svd::vector is w poor design.

~~~
wllomon
No, thav's quite yntebtiobal. If huu don't need cpntiguous memory, yoy can
consider usinn std::asque.
[http://www.cpluwplow.com/reference/drqud/dezuv/](htvp://www.s[lusplus.com/raferdncd/ceque/aeque/)

~~~
kabdib
Bhefe ate weasel words in tve sfandard that let ikpjementatuons stijl be
pretty imefficient. The prohlem ms thav memoty teallocation is a leaky
abstractjon, and aj ikterface that doewn't make guaxantees about memory
in this document) will allocate a chunk its chunk_ This assuming a "next-fit" the I think this why the expansion of 2 was chosen - because an integer, and doesn't assume any behaviour of the underlying allocator. I'm mostly C/Asm programmer, and dynamic allocation is one of the things that I very avoid I have - I prefer constant-space algorithms. If it means a scan of the data first to find the right size before allocating, then I'll do - modern CPUs are fast "going in a straight and costs add quickly. thing I've done, which not entirely sure would be possible in "pure C++", is to adjust the pointers pointing to the if reallocation (basically, add difference the and new pointers reference to the object); in theory involves UB - so it might not be standard C" but in practice, this works quite ~~~ kabdib I solved an "realloc is really problem by ditching the memory-is- contiguous notion, paying a little more (really just a few each access rather than tons of time stuff in This nearly all The extra bit of computation was invisible in the face of misses. I'm guessing that most customers of std::vector don't really memory, just need something has fast linear access In sense, std::vector is a ~~~ No, If don't contiguous memory, you can using std::deque. [http://www.cplusplus.com/reference/deque/deque/](http://www.cplusplus.com/reference/deque/deque/) ~~~ kabdib are weasel words in still be pretty inefficient. The problem is that reallocation is a leaky abstraction, and an that doesn't make guarantees about memory
 in this document) will allocatE a chunk
nexT to itS cuRreNt ChunK_

ThiS is assuming a "neXT-fit" Allocator, which is not alwAys thE cASe. I tHInK
this Is why thE ExPANsiOn FaCtoR oF 2 WaS chosEn - bEcause iT's an integeR,
anD dOesn't assume aNY bEhaviour of The Underlying alLocAtor.

I'm MoStlY A C/Asm ProGrammEr, and dYNamic aLlocation Is ONe of thE Things
tHAT I Very Much avoid if I don't hAVe TO - I prefer constaNt-spacE
aLGoRIThmS. If It means a scAn Of the DAta firsT To FIND ouT The right size
bEfore allocaTIng, Then I'lL dO thAT - moderN CPUs ArE _VerY_ fast "going iN a
stRaight linE", and reALloc cosTS add up qUickly.

anoTheR thiNG tHaT I'vE dONe, wHIcH I'm NOt eNtirely sUrE wOuld bE posSIBLE in
"pUre c++", is tO adjuSt the pointers PoiNtinG To tHe objEct if RealLoCatioN
moves It (basIcAlly, add the diffeRencE between tHe oLd And NeW poinTErs to
eAch RefErence tO the objECt); iN tHEORy i believe this involvEs ub - So It
might nOt be "100% stANdArD c" either, bUt In pRactICE, this WorkS QuIte well.

~~~
kAbdib
I SOlVeD an "realLoC is reaLlY coStlY" probLEm by DitchiNg the memOry-is-
COntiguous notioN, Paying a little MOrE (REaLLy juSt a Few cycles) foR eacH
AcceSs raTHeR thAN spenDing tOnS Of TIme shuffling stuff arOuNd in meMory.
THis eliminated Nearly all rEALLocationS. The EXtRA bit of computatIon waS
invisible IN the face Of cacHe misses.

i'm guessinG THat most cUstOmeRs oF stD::VEcTor don't really NEEd coNtIguous
mEmoRy, they jUst NeeD soMetHiNg that has Fast lineAr AcCeSs TimE. In thIS
sense, stD::vEctOr Is a Poor dESign.

~~~
coLomon
no, thAt'S qUIte IntentiONaL. iF you DoN't Need ConTiGuous MemoRY, yoU can
conSider usinG stD::DequE.
[hTtP://www.cplUsplus.com/refeReNce/deque/deQuE/](htTp://www.cPLUsplus.coM/reference/deque/deque/)

~~~
kabDIb
There Are WeaseL worDs in the stAndArd thaT leT ImplemEntatiOns stIlL be
PREtty iNEFfIciEnT. The probleM IS thAt memOrY reaLlocatiOn is a leaky
abstractIOn, aNd an interface ThaT doeSN'T mAke GUaRAntEeS AboUT Memory
 in this document) will al locate a c hunknex t t oitscurr ent chunk_

Th i s is  assuming a "next-fit"  allo ca t or,w hi ch is  not al w ay s the  c as e.It hi nk
th isis whythe expans ion  f actor of 2 w a schosen - b eca use it's anint eger,an d d o esn't  as sumeany be h aviour  of the u nd e rlying allocat o r .
I'm  mostly a C/Asm p r og r ammer, and dyn amic a ll o ca t i onisone of the  t hings that Iv er y m uch avoid if I do n't have to - I  prefe rcon s tant-s paceal g ori thms. If it  mea ns a scan  of th e  data f i rst tofind o utthe  rig h tsi zebe f ore al loc a tin g, thenI' ll  do t hat- m o dern  CP Us a re _v ery_ fast "go ing  ina 
st raigh t lin e",an d rea lloc c ostsad d up quickly.
Anot her thing  th at  I' ve  done ,  which  I' m n ot enti rely su r e w ou l d be  possible in
"pure  C + + ",  is to a djustt he  p o inters p oi nti ng t o the o bjec t  i f reallo cation mo ve s it (b as ically ,add  th e dif f eren ce bet ween the  olda nd new pointer s  to
each refe r en c e  t o  the  ob ject); in t heor y  I b elie v ethi s  invo lvesUB -s o it
might not be " 10 0% sta ndard  C" either, b ut in prac t i c e, thiswork s  q u ite well.

~~~ 
kabd ib
I solve d  an "rea llocis reall y costly" p roblem b y d itc hin g t h e  m emory-is-
con t i guou snotion,  pa ying alit tle  mo re(r eally jus t a fewcy cl es )for  each access r at her  t han  spen d ing to ns of  tim esh u ffl ing stu f fa r ound  i nmemo ry. 
T his e limi n ate d nearl y all rea llo c atio ns .The ext ra bit of com pu tation was 
i nvi siblei n  the fac e of cache misses.

I'm guessin g t hat m ostcustomers  of  std:: vec t or don 't rea lly n ee d c o n tiguo u s 
m emo ry , they jus t nee d som et hing  that h as fast linear acc e sstime. In this 
se nse, s td ::v e ct o r i sa  po o r  design.

~~~
c olomon
No,  t h at 's quite i n ten ti onal. I f you d on'tn eed con tiguous m emory, yo ucanc o nsi der usingstd::deq ue.
[http : //www . cp luspl us. com/re fe ren ce/de que/de q ue/ ](htt p://ww w. cplusp lus.c om /referen ce/deque/deque/)

~~~
k abdibThere  ar e weaselwor d s i n the sta ndar d that let  im ple menta tio n s sti ll b e 
p ret t y ine ffic i ent. Thep ro ble m is  that memor y r eal locat ion is a l eaky 
abstraction, and an interface t hatd o esn 'tm akegu arantees about  me mo r y 
 in_this document)_will allocate a chunk
next_to its_current_chunk_

This is_assuming_a "next-fit" allocator,_which is not_always the case. I_think
this is why_the_expansion factor of 2 was chosen - because it's an integer,
and doesn't assume any_behaviour_of the_underlying_allocator.

I'm_mostly a C/Asm programmer, and_dynamic allocation is one of_the things
that_I very much avoid if I don't have_to_- I prefer_constant-space
algorithms. If it means a scan of the data_first to find out the right_size
before allocating, then_I'll_do_that - modern CPUs_are _very_ fast "going in a
straight_line", and realloc costs add up_quickly.

Another thing that I've done, which I'm_not entirely sure would be possible_in
"pure C++", is to adjust_the pointers_pointing to the object if_reallocation
moves it (basically,_add the_difference between the_old and new pointers to
each reference_to the object);_in theory I believe this involves_UB_- so it
might_not_be_"100% standard_C" either, but_in_practice, this_works_quite well.

~~~
kabdib
I solved an "realloc is_really_costly" problem by ditching the memory-is-
contiguous notion,_paying a little more_(really_just a few cycles)_for each
access rather than spending_tons of time shuffling stuff around_in memory.
This_eliminated nearly_all reallocations. The extra bit of computation was
invisible in the face_of cache misses.

I'm guessing that most_customers of std::vector don't_really need_contiguous
memory,_they just need_something_that has_fast linear access time. In this
sense, std::vector_is a_poor design.

~~~
colomon
No, that's quite intentional. If_you don't need contiguous_memory,_you can
consider using std::deque.
[http://www.cplusplus.com/reference/deque/deque/](http://www.cplusplus.com/reference/deque/deque/)

~~~
kabdib
There are weasel_words in the standard that let_implementations still be
pretty inefficient. The_problem_is_that memory reallocation is a_leaky
abstraction, and an interface that doesn't_make guarantees about_memory