473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

lots of little mallocs or one big one? (somewhat naive)

So, I'm looking at some code that does 770K malloc calls in a row, of
varying size, paired with corresponding freads from a binary file to
initialize. In total, about 58 MB of data is allocated an initialized.
Unsurprisingly, this takes a good bit of time.

Question: Wouldn't it be a lot more efficient to allocate this 58 MB in
one big gulp, then fread in the 58 MB of data from the file to
initialize it, rather than a zillion small calls?

Also, can it ever happen that a malloc of N bytes would fail, yet an
equivalent number of small mallocs summing to N would succeed? What
about the fread?

thanks,
Dave
May 23 '07 #1
6 1793
Dave Stallard wrote:
So, I'm looking at some code that does 770K malloc calls in a row, of
varying size, paired with corresponding freads from a binary file to
initialize. In total, about 58 MB of data is allocated an initialized.
Unsurprisingly, this takes a good bit of time.

Question: Wouldn't it be a lot more efficient to allocate this 58 MB in
one big gulp, then fread in the 58 MB of data from the file to
initialize it, rather than a zillion small calls?
My instinct says yes, but the only sure way would be to try it and see.
C++ doesn't impose any constraints on the efficiency of malloc so
different implementations are optimized for different situations.

I've read that malloc is typically optimized for C, which tends to make
fewer and larger allocations than C++, where you do often need to make a
lot of small allocations.

An alternative to one big allocation would be to use a small object
allocator, i.e. an allocator optimized for a lot of small allocations.
Try google.
>
Also, can it ever happen that a malloc of N bytes would fail, yet an
equivalent number of small mallocs summing to N would succeed?
That is possible, a malloc of N bytes must return a contiguous block of
N bytes. It could be that N bytes are available for allocation but not
in one contiguous block.

What
about the fread?
I think there is likely to be little difference there. The O/S will
likely buffer all I/O itself, so there should be little difference in
doing lots of calls to fread reading a few bytes vs. one call to fread
reading a large number of bytes. But as usual the only way to be sure if
to try it and time it.
thanks,
Dave
john
May 23 '07 #2
"Dave Stallard" <st******@nospam.netwrote in message
news:0O******************************@comcast.com. ..
So, I'm looking at some code that does 770K malloc calls in a row, of
varying size, paired with corresponding freads from a binary file to
initialize. In total, about 58 MB of data is allocated an initialized.
Unsurprisingly, this takes a good bit of time.

Question: Wouldn't it be a lot more efficient to allocate this 58 MB in
one big gulp, then fread in the 58 MB of data from the file to initialize
it, rather than a zillion small calls?
Maybe. It would be faster to allocate 58MB in one call then 770k calls for
smaller chucnks, quite a bit faster (maybe even 770k times faster). Also,
you will need 770k places to hold the pointers to the allocated memory with
smaller calls, just one pointer with the bigger call.

However, alignment can be an issue. Consider you want to read, say, a
character and an integer. On your system (for example) char has sizeof 1,
integer has sizeof 4. So you may allocate 5 bytes. Then read the character
then the integer. However, misaligned integers on some systems are slower
(mine) than an aligned one, and on some systems will cause a program crash.
Typically, a 4 byte integer needs to be aligned on a 4 byte boundary. That
is, an address evenly divisible by 4. On some systems the general rule
*may* apply, that any given built in type needs to be aligned on it's size
(I doubt this is true for all/most systems).

So, code like this may fail (untested code, and I don't use fread so kinda
guessing at it's syntax):

unsigned char* buffer = malloc( sizeof( char ) + sizeof( int ) );
unsigned char* bufferpos = buffer;
fread( bufferpos, sizeof( char ), 1, myfile );
bufferpos += sizeof( char );
fread( bufferpos, sizeof( int ), 1, myfile );

Well, now our buffer contains (supposedly) a character in the first bye, an
integer in the next 4 bytes. Now you get the fun of pulling out the integer
without crashing your system. Which generally means you're going to have to
allocate an integer and move the bytes over one by one with some method to
be safe. If you had allocated an integer in the first place and read into
it, you wouldn't have to do this extra step.

So, in the long run, yes, the malloc would be faster, but you're going to go
through extra steps to move your data into variables you can use, which you
are going ot have to malloc anyway.
Also, can it ever happen that a malloc of N bytes would fail, yet an
equivalent number of small mallocs summing to N would succeed?
Depending on how effecient the OS's mallocing is, yes. With padding and
everything else being an issue.
What about the fread?
What about it?
May 23 '07 #3
Dave Stallard wrote:
So, I'm looking at some code that does 770K malloc calls in a row, of
varying size, paired with corresponding freads from a binary file to
initialize. In total, about 58 MB of data is allocated an initialized.
Unsurprisingly, this takes a good bit of time.

Question: Wouldn't it be a lot more efficient to allocate this 58 MB in
one big gulp, then fread in the 58 MB of data from the file to
initialize it, rather than a zillion small calls?
Measure. Try timing the same thing, but with memory allocation
turned off, just fread() the data and ignore the results. Maybe
it will turn out that file I/O is the bottleneck here and you
won't have to worry about malloc?
Also, can it ever happen that a malloc of N bytes would fail, yet an
equivalent number of small mallocs summing to N would succeed? What
about the fread?
Yes, what John Harrison said.

HTH,
- J.
May 24 '07 #4
John Harrison wrote:
That is possible, a malloc of N bytes must return a contiguous block of
N bytes. It could be that N bytes are available for allocation but not
in one contiguous block.
Doh! Of course. Should have thought of that - thanks.

Dave
May 31 '07 #5
* John Harrison:
>
The O/S will
likely buffer all I/O itself, so there should be little difference in
doing lots of calls to fread reading a few bytes vs. one call to fread
reading a large number of bytes. But as usual the only way to be sure if
to try it and time it.
What I remember from the time of discussion about efficiency of old
versus new iostreams versus FILE* versus native, when I tested this, is
that at that time doing one big gulp was very much more efficient.

I don't expect that to have changed significantly.

But YMMV -- measure.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 31 '07 #6
Alf P. Steinbach wrote:
* John Harrison:
>>
The O/S will likely buffer all I/O itself, so there should be little
difference in doing lots of calls to fread reading a few bytes vs. one
call to fread reading a large number of bytes. But as usual the only
way to be sure if to try it and time it.

What I remember from the time of discussion about efficiency of old
versus new iostreams versus FILE* versus native, when I tested this, is
that at that time doing one big gulp was very much more efficient.

I don't expect that to have changed significantly.

But YMMV -- measure.
You are right. It is because the I/O is typically buffered that the way
you access that buffer, one big gulp or lots of little ones, can make a
big difference. The underlying I/O would be the same in both cases.

I think I've been guilty of fuzzy thinking on this issue until now.

john

Jun 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric R. | last post by:
I was looking through php.net's help guide and a few other documents that I cam across on the web but I am still having trouble finding the information I am looking for. So I came here... Here...
5
by: Haines Brown | last post by:
Re. <title>, my impression has been that a line break is _NOT_ allowed in the contained text. Is that true? Also, I gather a double quotation mark in the text string (and ampersand, etc.) _IS_...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
3
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc....
4
by: Mike Hnatt | last post by:
My goal is to get data from an XML file into a couple of tables in an Access database. The XML file is a little complex so I need control over what I do (I can't just read it into a dataset). ...
54
by: ash | last post by:
i am writing this program (for exercise1-9 in k&r-2nd edition) which removes extra spaces in string example- "test string" will be "test string" #include<stdio.h> #include<string.h>...
3
by: johan2sson | last post by:
The documentation for PyThreadState_SetAsyncExc says "To prevent naive misuse, you must write your own C extension to call this". Anyone care to list a few examples of such naive misuse? Johan
3
by: pragy | last post by:
Hey, can any one help me for writing a program of naive gauss elimintaion technique? It's a technique to solve system of simultaneous linear equations using matrix. thanks
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.