Popup YouTube Video
Sheerpower Logo

Strings: How Sheerpower Stores Them


String Attributes for Efficient Processing


This page describes how Sheerpower stores strings and the work its runtime saves when a program assigns, compares and joins them.

  1. Length-Aware Strings
    Sheerpower STRING variables store their length internally, allowing immediate length access without scanning for a terminator.
  2. Binary-Safe Content
    Since the length is tracked internally, a STRING can include binary data, including embedded null characters.
    name$ = "A" + chr$(0) + "B"
  3. External Compatibility
    When passed to routines in other languages, Sheerpower automatically appends a zero terminator for compatibility.
    call "c_function" (name$) ! name$ is zero-terminated

Optimized Memory Management

Efficient Buffer Reuse

Sheerpower manages strings by reusing memory buffers, which significantly reduces the overhead of memory allocations and deallocations. For example, if a string holds "The rain in Spain" and is later changed to "Enjoy life", Sheerpower repurposes the existing buffer. In most applications, over 99% of string buffers are reused.

This approach avoids the frequent memory operations that cause performance bottlenecks in other languages.
All strings are over-allocated and virtually mutable, making assignments O(1) operations with no allocation/deallocation cycles.

text$ = "The rain in Spain" // Allocates a buffer text$ = "Hello world" // Reuses same buffer, updates length text$ = "X" // Still uses same buffer
Performance Impact: String assignments become fast, with no memory fragmentation. Sheerpower has no need for a garbage collector.

Comparison with Other Languages

In many languages, every string modification requires a new memory allocation, and the old memory must be deallocated. This cycle introduces overhead, especially in loops.

for i = 1 to 10_000
num$ = str$(i)
next i
Note: In Sheerpower, only one memory allocation is needed for num$. The buffer is reused in each iteration, a stark contrast to languages that could perform 10,000 allocations and deallocations, slowing down performance.

The Chunked Fixed Data Area (FDA) Optimized Memory Manager

Based on research showing 85% of business strings are under 128 bytes, Sheerpower pre-allocates thousands of fixed-size buffers on startup.

Three-Tier Memory Strategy:
  1. Strings <= 24 bytes: Inline storage in descriptor
  2. Strings 25-128 bytes: Pre-allocated FDA chunk (eliminates 85% of malloc/free)
  3. Seven additional optimized string length ranges
  4. Strings > 65536 bytes: Traditional heap allocation

Performance Optimizations Internals

String Interning with Shared String IDs (SIDs)

Sheerpower assigns every unique string value a 64-bit sequential String ID (SID). When a string variable is first created, the runtime generates a new SID for its content. When another variable is assigned the same content—either by direct assignment or by copying—the SID from the source descriptor is copied into the destination descriptor along with the data. In other words, the SID travels with the data.

The SID is stored directly inside the string's descriptor, alongside the string's length, the pointer to its character buffer, and the buffer's current allocation size. Because the SID lives inside the descriptor rather than in a centralized lookup table, Sheerpower avoids global SID-table lookups and enables several powerful runtime optimizations:

String Attributes for Efficient Processing
Descriptor holds length, pointer, current allocation, 24-byte inline buffer, and SID.
String DescriptorA$ stored locally
Value: "Hello"
Length: 5
Pointer: 0x00F3...
Allocation: 24 bytes current buffer size
SID: 982734 (in descriptor)
24-byte inline buffer
H
e
l
l
o
Strings up to 24 bytes stay entirely inside the descriptor. No heap allocation needed.
SID 982734 lives in the descriptor with length, pointer, and allocation, so no SID lookup table is required.
Buffer and Interning Behavior
For "Hello" (5 bytes):
inline buffer is used, no heap allocation.
  • Length <= 24 bytes uses inline buffer.
  • Allocation tracks current buffer size (here 24 bytes).
  • SID 982734 marks the content "Hello".
  • Equality check: compare SIDs, not characters.
  • Reassigning A$ reuses the same buffer until it outgrows 24 bytes.
  • Fast equality checks: compare SIDs instead of scanning characters. Equal SIDs mean the strings are identical, so the scan is skipped. If the SIDs differ, Sheerpower scans once to compare content; when the content matches, the higher (newer) SID is replaced with the lower (older) one, so every later comparison of those values takes the fast SID path again.
  • Copy elimination: Skip copying when source and target already share SIDs
  • Hints can be stored along with their SID to avoid re-parsing of previously parsed strings
a$ = "Microsoft" // Gets a new SID, no runtime cost company_b$ = a$ // Inherits SID, no cost after first run company_c$ = company_b$ // Contains what a$ contains and a$'s SID

Copy-on-Write for String Literals

String literal assignments cost almost nothing, as the descriptor simply points to the literal in the compiled code. A copy is only triggered upon modification.

message$ = 'System ready' // Points to literal, no allocation backup$ = message$ // Shares same literal pointer's SID message$ = message$ + ' - OK' // NOW triggers a copy on first iteration only

Intelligent Caching and Hinting System

Sheerpower uses an internal hinting mechanism, using SIDs to remember recent operations and cache results. This enhances efficiency by avoiding redundant processing.

Concatenation Hinting: A cache-sized hint array remembers recent concatenation operations by SID combinations. For repetitive tasks like building SQL queries, subsequent operations become simple cache lookups.

for i = 1 to 1000 sql$ = "SELECT * FROM orders WHERE id = " + customer_id$ // After first iteration, this becomes a cache lookup next i

Function Parameter Parsing Cache: Functions like getword$() and geodistance() automatically cache parsing results by string content. If you call getword$(bible$, 1000) and then getword$(bible$, 1001), Sheerpower uses the hint to quickly find the next word without re-reading from the start.

here$ = "40.7128,-74.0060" dist1 = geodistance(here$, "34.0522,-118.2437") dist2 = geodistance(here$, "41.8781,-87.6298") // The 2nd call recognizes cached 'here$' coordinates and avoids the parsing // and numeric conversion

Practical Benefits and Use Cases

These transparent optimizations provide the most benefit for typical business application patterns:

  • Repetitive content (company names, status codes)
  • SQL query building
  • Report generation and template formatting
  • Parsing repeated data formats

The savings grow with the amount of text a program handles: real-time data analysis and large batch runs gain the most.

Key Takeaway: None of this asks anything of the program. Ordinary assignments, comparisons and joins get the reused buffers, the SID shortcuts and the parsing cache on their own.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.