Definition of a flat string and example

Posted on
  • Thr 2018.10.11

    'You can even access data stored in flat strings:'

    What is a 'flat string' and how is it different? Definition, documentation please?
    Is it just a contiguous block, null terminated? Is there a size limit?

    Skimmed over­316409/#comment14077573

    It appears that 'flat string' may be specific, as a quick search only uncovered one reference, other than­source/js/src/jsapi.h#4035
    'Flat strings and interned strings are always null-terminated,'

    Having issues with var addrsrc = E.getAddressOf(src,true);
    if(!addrsrc) throw new Error("Not a Flat String");

    irratically returning 'Not a flat string', loading same data each run.

    Working with

    var buffer = new ArrayBuffer(8);
    var n = new Uint8ClampedArray(buffer, 0, 4);

    and E.compiledC

    Is a flat string somehow related to Uint8ClampedArray
    Not mentioned however in­t8ClampedArray

    Is flatAddress just a T/F flag and not the address of the variable? This implies that param should always be true, e.g. why would you supply 'false' if you are attempting to get the address of (E.getAddressOf) the variable supplied? Seems redundant.­etAddressOf
    flatAddress - If a flat String or flat ArrayBuffer is supplied, return the address of the data inside it - otherwise 0

    I have a hunch that the size of the referenced address passed or returned is causing pointer (my code) offset errors.

  • I've made a note to update the Performance/Internals pages, but basically:

    • In Espruino, ArrayBuffers actually store their data in a String behind the scenes
    • Normal Strings are 'chained' - this is mentioned in the performance/internals pages, but basically it means a String can be fragmented all over memory. When you write "Hello" that's what you get, because Espruino didn't know the size of the string up front as it was parsing it as it went along.
    • If Espruino knows the size of the string up front, there is enough contiguous free space, *and the String is long enough that it is more efficient to use a Flat String** then Espruino will allocate a flat string - which is basically a single JsVar as a header, followed by N other JsVars that store the raw data.
    • Flat Strings can take a while to allocate, and because of the fixed JsVar header they're not as efficient for small arrays. There's also no speed advantage even when accessing a small Flat String vs a small string - which is why they're allocated only when required.

    So in your case, your arraybuffer just isn't big enough that it makes sense to allocate it as a flat string. Make it size 32 or something and it'll work.

    You can however use E.toString to make Espruino create a flat string specifically for your data - for example E.toString(new Uint8Array(8)). You could also then use E.toArrayBuffer on that String to create an ArrayBuffer that actually references the Flat String itself.

  • Just to add, what I suggest (E.toString) is actually what's in the example right next to the 'you can even use flat strings' post that you linked right at the start of your post - so that would be a good starting point.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Definition of a flat string and example

Posted by Avatar for Robin @Robin