Closed GoogleCodeExporter closed 9 years ago
This should only matter if you use SparseArray<X> where X has a constructor. We
use Thread* and int in this source code. If you want to adapt it for use in
another project, that's fine, but it doesn't seem to matter here.
Original comment by rsc@golang.org
on 10 Jan 2014 at 1:42
Not really. If you follow the calls on dense_.resize(), you get this:
resize()
_M_default_append()
__uninitialized_default_n_a()
_Construct_default_a_impl()
::new((void *)__p) _Tp(std::forward<_Args>(__args)...)
And the call to __uninitialized_default_n_a() includes a for-loop that performs
O(n) iterations over the new elements to default construct them, so you end up
paying the cost there. This becomes noticeable when the array is very sparse.
Original comment by dhruvb...@gmail.com
on 10 Jan 2014 at 4:03
But Thread* and int have no default constructor. What work can it possibly be
doing?
Original comment by rsc@swtch.com
on 10 Jan 2014 at 4:18
Just executing the for loop 'N' times involves O(N) comparisons and O(N)
integer increments. That's work.
Also, default initialization of pointer and integral types involves zeroing out
the memory, which means that in a demand paged system, you invoke the demand
paging code, which ends up assigning real memory to the virtual addresses you
initialize with zeros.
Original comment by dhruvb...@gmail.com
on 10 Jan 2014 at 10:18
Are you saying that if I have
vector<int> v;
v.resize(10);
that the now 10 entries in v are guaranteed to be zeroed? Because I don't
believe that.
Russ
Original comment by rsc@golang.org
on 10 Jan 2014 at 2:37
It seems that all entries in v are guaranteed to be zeroed.
1.
http://stackoverflow.com/questions/3866202/vector-resize-automatic-fill/3866422#
3866422
2. http://stackoverflow.com/a/7413968/155951
Original comment by dhruvb...@gmail.com
on 10 Jan 2014 at 3:40
And: http://stackoverflow.com/a/13029377/155951
Original comment by dhruvb...@gmail.com
on 10 Jan 2014 at 3:40
Original issue reported on code.google.com by
dhruvb...@gmail.com
on 24 Dec 2013 at 7:34