digitalmars.D.learn - new Type[count] takes too much?
- Namespace (9/9) Oct 31 2013 I'm sure we had already this conversation but I don't find the
- bearophile (6/14) Oct 31 2013 In Python if you append items to an array, it sovra-allocates,
- Namespace (3/18) Oct 31 2013 So you agree with me.
- Marco Leise (5/8) Oct 31 2013 That's bearophilian for 'over'. :p
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/10) Oct 31 2013 Apparently, a variation of Italian prefix sopra- :)
- H. S. Teoh (7/20) Oct 31 2013 [...]
- safety0ff (7/17) Oct 31 2013 To me it looks like it is derived directly from the way the GC
- Namespace (3/22) Oct 31 2013 Maybe (and hopefully) I'm wrong, but it seems that the static
- Jonathan M Davis (13/38) Oct 31 2013 T[] buffer = new T[N];
- Namespace (4/56) Oct 31 2013 Hm, seems not very performant for a *system* language. But fine,
- Jonathan M Davis (3/5) Oct 31 2013 I believe that C's malloc does the same thing.
- Jonathan M Davis (8/19) Oct 31 2013 You're making the assumption that it would be normal to not want to then...
- Marco Leise (17/38) Oct 31 2013 Maybe run a comparison with another popular GC enabled
- Namespace (4/23) Oct 31 2013 I disagree. If I want to append I use reserve and I think that is
- Jonathan M Davis (9/35) Oct 31 2013 I would fully expect that most people don't bother with reserve or capac...
- Namespace (3/46) Oct 31 2013 Currently Appender isn't more performant than built-in arrays. ;)
- Jonathan M Davis (7/8) Nov 01 2013 If Appender is not more performant for appending than just appending to ...
- John Colvin (3/54) Nov 01 2013 I have found it more performant. A sensible test case where it is
- Namespace (2/2) Nov 01 2013 It is. I don't know if it is fixed already, and I don't have the
I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?
Oct 31 2013
Namespace:T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?In Python if you append items to an array, it sovra-allocates, but if you create an array with the [x] * n syntax, then it creates a list (array) exactly of size n. Bye, bearophile
Oct 31 2013
On Thursday, 31 October 2013 at 09:27:11 UTC, bearophile wrote:Namespace:Never heard of 'sovra'. What does it mean?T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?In Python if you append items to an array, it sovra-allocates,but if you create an array with the [x] * n syntax, then it creates a list (array) exactly of size n. Bye, bearophileSo you agree with me.
Oct 31 2013
Am Thu, 31 Oct 2013 10:42:35 +0100 schrieb "Namespace" <rswhite4 googlemail.com>:On Thursday, 31 October 2013 at 09:27:11 UTC, bearophile wrote:That's bearophilian for 'over'. :p -- MarcoIn Python if you append items to an array, it sovra-allocates,Never heard of 'sovra'. What does it mean?
Oct 31 2013
On 10/31/2013 01:10 PM, Marco Leise wrote:Am Thu, 31 Oct 2013 10:42:35 +0100 schrieb "Namespace" <rswhite4 googlemail.com>:Apparently, a variation of Italian prefix sopra- :) http://en.wiktionary.org/wiki/sopra- AliOn Thursday, 31 October 2013 at 09:27:11 UTC, bearophile wrote:That's bearophilian for 'over'. :pIn Python if you append items to an array, it sovra-allocates,Never heard of 'sovra'. What does it mean?
Oct 31 2013
On Thu, Oct 31, 2013 at 01:28:13PM -0700, Ali Çehreli wrote:On 10/31/2013 01:10 PM, Marco Leise wrote:[...] http://en.wiktionary.org/wiki/sovra :) T -- Nearly all men can stand adversity, but if you want to test a man's character, give him power. -- Abraham LincolnAm Thu, 31 Oct 2013 10:42:35 +0100 schrieb "Namespace" <rswhite4 googlemail.com>:Apparently, a variation of Italian prefix sopra- :) http://en.wiktionary.org/wiki/sopra-On Thursday, 31 October 2013 at 09:27:11 UTC, bearophile wrote:That's bearophilian for 'over'. :pIn Python if you append items to an array, it sovra-allocates,Never heard of 'sovra'. What does it mean?
Oct 31 2013
On Thursday, 31 October 2013 at 09:15:53 UTC, Namespace wrote:I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?To me it looks like it is derived directly from the way the GC allocates chunks: Next power of two if less than 4096 otherwise some multiple of 4096. Unless you modify the GC, this behaviour is present whether you can see it or not (http://dpaste.dzfl.pl/5481ffc2 .)
Oct 31 2013
On Thursday, 31 October 2013 at 09:48:23 UTC, safety0ff wrote:On Thursday, 31 October 2013 at 09:15:53 UTC, Namespace wrote:Maybe (and hopefully) I'm wrong, but it seems that the static array is on the heap?I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?To me it looks like it is derived directly from the way the GC allocates chunks: Next power of two if less than 4096 otherwise some multiple of 4096. Unless you modify the GC, this behaviour is present whether you can see it or not (http://dpaste.dzfl.pl/5481ffc2 .)
Oct 31 2013
On Thursday, October 31, 2013 10:59:48 Namespace wrote:On Thursday, 31 October 2013 at 09:48:23 UTC, safety0ff wrote:T[] buffer = new T[N]; is a dynamic array. Now, in the code in your dpaste link, you have a static array which is in a struct which is on the heap, so that's different, but now you're dealing with the amount of memory that gets allocated on the heap for the struct, and as the GC is going to allocate in multiples of 2, more than the size of the struct is going to be allocated. It might be that some of the memory beyond the end of the struct might actually be used for another object rather than the struct using the memory block by itself (I'm not sure what the GC does in that regard), but it's definitely going to allocate in multiples of 2, so if the GC has to go up another multiple of 2 to make the struct fit, it'll go up another multiple of 2. - Jonathan M DavisOn Thursday, 31 October 2013 at 09:15:53 UTC, Namespace wrote:Maybe (and hopefully) I'm wrong, but it seems that the static array is on the heap?I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?To me it looks like it is derived directly from the way the GC allocates chunks: Next power of two if less than 4096 otherwise some multiple of 4096. Unless you modify the GC, this behaviour is present whether you can see it or not (http://dpaste.dzfl.pl/5481ffc2 .)
Oct 31 2013
On Thursday, 31 October 2013 at 10:12:10 UTC, Jonathan M Davis wrote:On Thursday, October 31, 2013 10:59:48 Namespace wrote:Hm, seems not very performant for a *system* language. But fine, thanks for your explanation. :)On Thursday, 31 October 2013 at 09:48:23 UTC, safety0ff wrote:T[] buffer = new T[N]; is a dynamic array. Now, in the code in your dpaste link, you have a static array which is in a struct which is on the heap, so that's different, but now you're dealing with the amount of memory that gets allocated on the heap for the struct, and as the GC is going to allocate in multiples of 2, more than the size of the struct is going to be allocated. It might be that some of the memory beyond the end of the struct might actually be used for another object rather than the struct using the memory block by itself (I'm not sure what the GC does in that regard), but it's definitely going to allocate in multiples of 2, so if the GC has to go up another multiple of 2 to make the struct fit, it'll go up another multiple of 2. - Jonathan M DavisOn Thursday, 31 October 2013 at 09:15:53 UTC, Namespace wrote:Maybe (and hopefully) I'm wrong, but it seems that the static array is on the heap?I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?To me it looks like it is derived directly from the way the GC allocates chunks: Next power of two if less than 4096 otherwise some multiple of 4096. Unless you modify the GC, this behaviour is present whether you can see it or not (http://dpaste.dzfl.pl/5481ffc2 .)
Oct 31 2013
On Thursday, October 31, 2013 11:16:35 Namespace wrote:Hm, seems not very performant for a *system* language. But fine, thanks for your explanation. :)I believe that C's malloc does the same thing. - Jonathan M Davis
Oct 31 2013
On Thursday, October 31, 2013 10:15:51 Namespace wrote:I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption. Also, from what I understand of how allocators work, they normally grab memory in sizes which are a multiple of 2, so I would expect it to be highly abnormal to ever get exactly the amount of memory that you requested. You're pretty much always going to get extra. - Jonathan M Davis
Oct 31 2013
Am Thu, 31 Oct 2013 02:52:49 -0700 schrieb Jonathan M Davis <jmdavisProg gmx.com>:On Thursday, October 31, 2013 10:15:51 Namespace wrote:Maybe run a comparison with another popular GC enabled language and check the memory use to find pathological cases. I'd assume that D's GC doesn't waste more memory than any other for dynamic arrays.I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption.That doesn't happen much in my programming practice. I'm with the OP on this.Also, from what I understand of how allocators work, they normally grab memory in sizes which are a multiple of 2, so I would expect it to be highly abnormal to ever get exactly the amount of memory that you requested. You're pretty much always going to get extra. - Jonathan M DavisThat's true. Although now I wonder if you could gain anything from having memory pages split into chunks of T.sizeof from which you can allocate single objects or whole arrays of T with a compact layout... +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |T|T|T[6] |T|T| | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -- Marco
Oct 31 2013
On Thursday, 31 October 2013 at 09:53:39 UTC, Jonathan M Davis wrote:On Thursday, October 31, 2013 10:15:51 Namespace wrote:I disagree. If I want to append I use reserve and I think that is the most common approach.I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption.
Oct 31 2013
On Thursday, October 31, 2013 23:06:22 Namespace wrote:On Thursday, 31 October 2013 at 09:53:39 UTC, Jonathan M Davis wrote:I would fully expect that most people don't bother with reserve or capacity and that they simply create the array at whatever size they create it at and then append to it without worrying about reserve. But we'd have to get real data on that to know for sure one way or the other. Certainly, most folks who are focusing on append performance seem to use Appender, and for the most part, I would suggest that anyone using reserve should be using Appender instead. - Jonathan M DavisOn Thursday, October 31, 2013 10:15:51 Namespace wrote:I disagree. If I want to append I use reserve and I think that is the most common approach.I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption.
Oct 31 2013
On Thursday, 31 October 2013 at 23:48:19 UTC, Jonathan M Davis wrote:On Thursday, October 31, 2013 23:06:22 Namespace wrote:Currently Appender isn't more performant than built-in arrays. ;)On Thursday, 31 October 2013 at 09:53:39 UTC, Jonathan M Davis wrote:I would fully expect that most people don't bother with reserve or capacity and that they simply create the array at whatever size they create it at and then append to it without worrying about reserve. But we'd have to get real data on that to know for sure one way or the other. Certainly, most folks who are focusing on append performance seem to use Appender, and for the most part, I would suggest that anyone using reserve should be using Appender instead. - Jonathan M DavisOn Thursday, October 31, 2013 10:15:51 Namespace wrote:I disagree. If I want to append I use reserve and I think that is the most common approach.I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption.
Oct 31 2013
On Friday, November 01, 2013 01:16:53 Namespace wrote:Currently Appender isn't more performant than built-in arrays. ;)If Appender is not more performant for appending than just appending to a naked array, then something is very wrong with Appender, because it's whole job is to make sure that it calls all of the appropriate functions on its internal array which it needs to in order to make appending efficient (e.g. assumeSafeAppend). - Jonathan M Davis
Nov 01 2013
On Friday, 1 November 2013 at 00:16:55 UTC, Namespace wrote:On Thursday, 31 October 2013 at 23:48:19 UTC, Jonathan M Davis wrote:I have found it more performant. A sensible test case where it is slower should be filed as a bug.On Thursday, October 31, 2013 23:06:22 Namespace wrote:Currently Appender isn't more performant than built-in arrays. ;)On Thursday, 31 October 2013 at 09:53:39 UTC, Jonathan M Davis wrote:I would fully expect that most people don't bother with reserve or capacity and that they simply create the array at whatever size they create it at and then append to it without worrying about reserve. But we'd have to get real data on that to know for sure one way or the other. Certainly, most folks who are focusing on append performance seem to use Appender, and for the most part, I would suggest that anyone using reserve should be using Appender instead. - Jonathan M DavisOn Thursday, October 31, 2013 10:15:51 Namespace wrote:I disagree. If I want to append I use reserve and I think that is the most common approach.I'm sure we had already this conversation but I don't find the thread. T[] buffer = new T[N]; assumes more space than stated (in average 2010 elements more. See: http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like reserve and that is IMO wrong. If I reserve memory with buffer.reserve(N), I want to have at least N elements. That behaviour is correct. But if I use new T[N] I mostly want exactly N elements and no extra space. Thoughts?You're making the assumption that it would be normal to not want to then append to something you allocated with new T[N], and I don't think that that's a valid assumption.
Nov 01 2013
It is. I don't know if it is fixed already, and I don't have the time to search for the thread. But afaik monarch filled the bug.
Nov 01 2013