Fast resizing of a string in ML - Hewlett Packard

This is a discussion on Fast resizing of a string in ML - Hewlett Packard ; Hello, I am using a string for data storage which I currently keep in TEMPOB. As this string grows or shrinks I have to resize it which I do currently this way: 1. ensure that string is at TEMPTOP, otherwise ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Fast resizing of a string in ML

  1. Fast resizing of a string in ML

    Hello,

    I am using a string for data storage which I currently keep in TEMPOB.

    As this string grows or shrinks I have to resize it which I do
    currently this way:

    1. ensure that string is at TEMPTOP, otherwise move it to TEMPTOP
    2. Shrink$ or Stretch$ the string

    Now, if the string is at TEMPTOP resizing it this way is very fast,
    but to copy it there is not.

    I know I could use Shrink$Any/Stretch$Any to avoid the copying to
    TEMPTOP but this is also not very fast.

    Would it be faster to keep the string in USEROB and work from there ?
    Would if be faster instead of resizing the string to create a new one
    and copy the data needed there ?
    What would be the fastest way to work with strings from ML ?

    TIA and greetings
    Andreas

  2. Re: Fast resizing of a string in ML

    On Mar 8, 7:39*am, Andreas Möller
    wrote:
    > Hello,
    >
    > I am using a string for data storage which I currently keep in TEMPOB.
    >
    > As this string grows or shrinks I have to resize it which I do
    > currently this way:
    >
    > 1. ensure that string is at TEMPTOP, otherwise move it to TEMPTOP
    > 2. Shrink$ or Stretch$ the string
    >
    > Now, if the string is at TEMPTOP resizing it this way is very fast,
    > but to copy it there is not.
    >
    > I know I could use Shrink$Any/Stretch$Any to avoid the copying to
    > TEMPTOP but this is also not very fast.
    >
    > Would it be faster to keep the string in USEROB and work from there ?
    > Would if be faster instead of resizing the string to create a new one
    > and copy the data needed there ?
    > What would be the fastest way to work with strings from ML ?
    >
    > TIA and greetings
    > Andreas


    Resizing at TEMPTOP is probably faster than at USEROB, since it only
    needs to move the return stack, rather than all the temporary
    environments plus the data stack.
    In my opinion you already have the fastest method, one way to optimize
    it is to preallocate your string in large chunks (like 1 kbyte or so),
    this way you only need to resize it from time to time.
    If you can't make resizing faster, try to resize less.

    Claudio

  3. Re: Fast resizing of a string in ML

    Hello Claudio,

    > In my opinion you already have the fastest method, one way to

    optimize
    > it is to preallocate your string in large chunks (like 1 kbyte or

    so),
    > this way you only need to resize it from time to time.
    > If you can't make resizing faster, try to resize less.


    This is an interesting idea. Surely it will create some overhead
    because it will be necessary to check if the string has to be
    expanded. If not it is just the overhead compared to the solution I
    have now, otherwise it is the overhead + copying to TEMPTOP plus
    expanding the string.

    Thanks for your suggestions.
    Greetings,
    Andreas



  4. Re: Fast resizing of a string in ML

    Hello Claudio,

    a faster way I discovered is the following:
    If the string has been created in TEMPOB with =MAKE$N it resides in
    its own tempob slot.
    Now instead of copying it to TEMPTOB and using Shrink$ or Stretch$ to
    change the string this can be done with =MOVERSU and =MOVERSD.
    Additionally you have to correct the size field of the string and you
    have to correct the offset to the tempob slot before the current one.
    The offset to the previous tempob slot is behind the current one,
    otherwise GC will fail resulting in a crash.

    Now, do I miss something or will this method be safe from anywhere
    (covered memory and other pitfalls) to resize a string in its own
    tempob slot ?

    Greetings,
    Andreas





  5. Re: Fast resizing of a string in ML

    On Mar 14, 6:18*am, Andreas Möller
    wrote:
    > Hello Claudio,
    >
    > a faster way I discovered is the following:
    > If the string has been created in TEMPOB with =MAKE$N it resides in
    > its own tempob slot.
    > Now instead of copying it to TEMPTOB and using Shrink$ or Stretch$ to
    > change the string this can be done with =MOVERSU and =MOVERSD.
    > Additionally you have to correct the size field of the string and you
    > have to correct the offset to the tempob slot before the current one.
    > The offset to the previous tempob slot is behind the current one,
    > otherwise GC will fail resulting in a crash.
    >
    > Now, do I miss something or will this method be safe from anywhere
    > (covered memory and other pitfalls) to resize a string in its own
    > tempob slot ?
    >
    > Greetings,
    > Andreas


    You can only use it if the string is *already* in TEMPTOP, and it's
    essentially the same you were doing before (I didn't disassemble the
    Shrink$ routine, but it probably does exactly that: call MOVERSx and
    fix the string length and the TEMPOB block chain). Before you were
    copying to TEMPTOP to guarantee this, now you think your object will
    always be at TEMPTOP?
    If your string is not at TEMPTOP, any references to objects above
    yours would need to be updated too.

    How about using multiple separate strings? You can keep a vector of
    pointers to different string objects. When you need more space, you
    allocate more in a separate block, then add its pointer to the vector.
    When you need less, simply abandon the chunk as it becomes empty.
    Reading/writing to the string would be more work, since you need to
    span across blocks, but you would never need to move large amounts of
    memory.

    Claudio

+ Reply to Thread