Professional Web Applications Themes

filling a block of mem with zeroes - Mac Programming

In article <cs.unm.edu>, Keith Wiley <unm.edu> wrote:   You might want to read: <http://developer.apple.com/doentation/Performance/Conceptual/Managing Memory/Tasks/MemoryAlloc.html>...

  1. #1

    Default Re: filling a block of mem with zeroes

    In article <cs.unm.edu>,
    Keith Wiley <unm.edu> wrote:
     

    You might want to read:
    <http://developer.apple.com/doentation/Performance/Conceptual/Managing
    Memory/Tasks/MemoryAlloc.html>
    Sean Guest

  2. #2

    Default Re: filling a block of mem with zeroes

    In article <cs.unm.edu>,
    Keith Wiley <unm.edu> wrote:
     

    Well, first off, it isn't faster. At best, BlockMove runs at the same
    speed your own "grab an element, stuff an element" loop would. At worst,
    it takes a substantial hit due to several factors, including memory
    caching, virtual versus "real" memory, and so on. Quite a few different
    things go into figuring out exactly what the performance is actually
    going to be on a particular machine.

    Using the toolbox, NewPtrClear(Size) will (ignoring the possibility of
    an Out Of Memory error) hand you back a pointer to a block of zeroed
    memory "Size" bytes long. But I guarantee you that it's zeroing that
    block exactly like you would: one element at a time.

    Regardless of what kind of "wrapper" gets put around it, in *EVERY*
    case, zeroing a block of memory boils down to a construct that's
    essentially identical to the following:

    for (X=BlockStart;X<(BlockEnd-BlockStart)+1;)
    Block[X++] = 0;

    There might be some "setup" done to check for a valid range, and
    similar, but the "guts" of the routine is going to look very much like
    that shown above, regardless of what "bells and whistles" have been
    grafted onto it.

    It's possible, with "wide data" machines (By "wide data", I mean a
    machine that ships more than one byte down the wires for a single write
    cycle) to do something like:

    long MyZero = 0L;

    and replace the "Block[X++]" line above with something like:

    Block[X+=4] = MyZero;

    to allow you to zero 4 bytes at once (Assuming the machine's concept of
    a "long" is four contiguous memory locations), but that's something of a
    special case, and is, of course, machine-dependent.

    --
    Don Bruder - net <--- Preferred Email - SpamAssassinated.
    Hate SPAM? See <http://www.spamassassin.org> for some seriously great info.
    I will choose a path that's clear: I will choose Free Will! - N. Peart
    Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
    Don Guest

  3. #3

    Default filling a block of mem with zeroes

    I generally use BlockMove to copy large arrays instead of copying them
    element by element, on the assumption that BlockMove is faster. Acting on
    the same assumption, it would be nice to fill a large array with zeroes in
    one big chunk intead of element by element.

    Assuming I'm right that these approaches are faster in the first place, is
    there any way to do this?

    Thanks.

    __________________________________________________ ______________________
    Keith Wiley unm.edu
    http://www.unm.edu/~keithw http://www.mp3.com/KeithWiley

    "Yet mark his perfect self-contentment, and hence learn his lesson,
    that to be self-contented is to be vile and ignorant, and that to
    aspire is better than to be blindly and impotently happy."
    -- Edwin A. Abbott, Flatland
    __________________________________________________ ______________________
    Keith Guest

  4. #4

    Default Re: filling a block of mem with zeroes

    Don Bruder wrote: 

    yeah, but if you read the URL sean mcbride posted, you'll see that
    calloc() has special properties on macosx. specifically, the memory
    isn't zeroed until the first time it's used. so there's a "lazy"
    optimization you could make the os do for you.
    Jhnny Guest

  5. #5

    Default Re: filling a block of mem with zeroes

    In article <nashville.comcast.net>,
    Jhnny Fvrt (it means "genetic antagonism") <com>
    wrote:
     

    And for the OP: the Carbon APIs like NewPtr and NewPtrClear call
    directly to malloc and calloc.
    Sean Guest

  6. #6

    Default Re: filling a block of mem with zeroes

    In article <cs.unm.edu>,
    Keith Wiley <unm.edu> wrote:
     

    The wisest use of your time is to assume that the library for your
    platform is optimized, and use the standard library call:

    In C:

    #include <string.h>
    memset(buffer, 0, sizeof buffer);

    In C++:

    #include <cstring>
    using std::memset;

    memset(buffer, 0, sizeof buffer);

    For Metrowerks, they often determine routines like this are better
    written in assembly language than as ordinary optimized C code.

    As has been pointed out, on OS X for VERY large arrays, initialized to
    zero, there are virtual memory techniques that application programmers
    don't have direct access to give you pages of pristine memory set to
    zero.

    Remember: the lines of code you don't write tend not to have bugs in
    them. Use well tested libraries where possible, and where appropriate.
    There's too much to do to spend your time re-inventing the wheel.
    David Guest

  7. #7

    Default Re: filling a block of mem with zeroes

    In article <lU_Mb.8447$sonic.net>, Don Bruder
    <net> wrote:
     
    >
    > Well, first off, it isn't faster. At best, BlockMove runs at the same
    > speed your own "grab an element, stuff an element" loop would. At worst,
    > it takes a substantial hit due to several factors, including memory
    > caching, virtual versus "real" memory, and so on. Quite a few different
    > things go into figuring out exactly what the performance is actually
    > going to be on a particular machine.[/ref]

    I haven't looked at the guts of the system recently, but if the
    elements you were copying were bigger than some threshold, the "grab an
    element, stuff an element" part of that loop used to be a call to
    BlockMove anyway. Doing the whole array in one shot would cut down a
    lot of overhead.

    BlockMove is also highly optimised, making the best use of the
    processor's and computer's bus bandwidth. Unless your array elements
    are of a size that are natural for the computer to work with, a call to
    BlockMove will almost certainly move the data much more efficiently.

    The C Standard Library memmove() or memcpy() functions should be
    similarly optimised. (Under OSX BlockMove might translate directly into
    a call to memmove().)

     

    It will be doing it in a way that is most efficient for the system.

     

    That is actually a fairly inefficient way to zero memory. The low
    level OS zeroing would probably partially unroll the loop. (Same goes
    for its implementations of memmove().)
     

    Since we are talking about the Mac here, you are dealing with a machine
    that has wide data. If you want to be cross platform you can use the C
    bzero() function which should do a fairly efficient zeroing of memory
    on whatever platform you're running.
    Don Guest

  8. #8

    Default Re: filling a block of mem with zeroes

    In article <cs.unm.edu>,
    Keith Wiley <unm.edu> wrote:
     

    You're right that these approaches are faster. If you're copying enough
    data, they're significantly faster than doing it one element at a time.

    Better yet if you're writing a Mach-O application on Mac OS X is to use
    memmove() to copy data and bzero() to zero it (or memset() with 0, but
    bzero() will be oh-so-slightly faster). Both of these routines are
    carefully optimized for performance and will result in excellent
    performance for the data set size you're using and the processor you're
    running on.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  9. #9

    Default Re: filling a block of mem with zeroes

    In article <sf.sbcglobal.net>,
    David Phillip Oster <org> wrote:
     

    I'm not sure which techniques you're thinking of, but application
    programmers definitely have direct access to a number of ways to get
    zero-filled pages on Mac OS X.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  10. #10

    Default Re: filling a block of mem with zeroes

    In article <lU_Mb.8447$sonic.net>,
    Don Bruder <net> wrote:
     

    I can't speak for NewPtrClear specifically, I'd be appalled if it worked
    that way.
     

    Not really. For one, that could be unrolled, as other folks have
    mentioned. For another, since this is a Mac newsgroup, we only need to
    worry about PowerPCs, and all PowerPCs have a 'dcbz' instruction that
    zeros out a 32-byte cache line. (On the G5 a cache line is 128 bytes,
    so you should use 'dcbzl' there. See TN 2087.)

    The combination of those two ensures that a carefully hand-rolled bzero
    function will be significantly faster than your for loop for large data
    sizes.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  11. #11

    Default Re: filling a block of mem with zeroes

    In article <cs.unm.edu>, Keith
    Wiley <unm.edu> wrote:
     

    What about BlockZero()? It works for me.


    -=-=-=-=-
    Carl Guest

  12. #12

    Default Re: filling a block of mem with zeroes

    In article <stanford.edu>,
    Eric Albert <stanford.edu> wrote:
     

    I am not aware of any performance difference among memmove/memcpy/BlockMoveData
    and bzero/memset/BlockZero (besides perhaps of the O(1) overhead of calling
    through to another function)... are you?

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  13. #13

    Default Re: filling a block of mem with zeroes

    In article <mit.edu>,
    Miro Jurisic <org> wrote:
     
    >
    > I am not aware of any performance difference among
    > memmove/memcpy/BlockMoveData
    > and bzero/memset/BlockZero (besides perhaps of the O(1) overhead of calling
    > through to another function)... are you?[/ref]

    Just the overhead of a comparison or two, a branch, and potentially a
    call to another function. For a very large data set it's unlikely that
    you'll see any impact from that. The only part that might make a
    difference would be the cost of a potentially mispredicted branch when
    memset checks for 0 to determine whether to invoke bzero.

    For small data sizes, though, the difference might be measurable if
    you're invoking the functions repeatedly.

    Of course, as always, there's no point in rewriting your code to zero
    blocks faster unless you have determined that you spend too much time
    zeroing blocks. :)

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  14. #14

    Default Re: filling a block of mem with zeroes

    Thanks guys. Wow, that's a lot of stuff I wasn't familiar with. Need to
    learn about memset in particular, plus the other "lowercase" function
    names you mentioned.

    Thanks.

    __________________________________________________ ______________________
    Keith Wiley unm.edu
    http://www.unm.edu/~keithw http://www.mp3.com/KeithWiley

    "Yet mark his perfect self-contentment, and hence learn his lesson,
    that to be self-contented is to be vile and ignorant, and that to
    aspire is better than to be blindly and impotently happy."
    -- Edwin A. Abbott, Flatland
    __________________________________________________ ______________________
    Keith Guest

Similar Threads

  1. #40020 [NEW]: int values with leading zeroes
    By pav at oook dot cz in forum PHP Bugs
    Replies: 1
    Last Post: January 4th, 01:24 PM
  2. Leading zeroes
    By Olivier Wirz in forum PERL Beginners
    Replies: 3
    Last Post: January 10th, 09:35 PM
  3. leading zeroes in day, month
    By Mitja Udovc in forum Informix
    Replies: 10
    Last Post: November 4th, 10:09 AM
  4. Replies: 0
    Last Post: January 8th, 08:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139