Home > OS >  What is the memory footprint of a character in a byte string when compared to a character in a strin
What is the memory footprint of a character in a byte string when compared to a character in a strin

Time:07-12

What is the memory footprint of a character in a byte string when compared to a character in a string in Racket? I was trying to look it up but cannot find it anywhere.

Is there any way I could compute such value?

CodePudding user response:

That's sorta the wrong question to ask. An entry in a bytestring is a byte, and takes up one byte of space. An entry in a string is a character, which takes up variable space depending on the character in question. You can ask how many bytes it would take to encode a character in UTF-8 with such functions as char-utf-8-length, but I can't tell for sure that the documentation promises to actually use that encoding internally.

CodePudding user response:

Racket(CS) uses Chez Scheme as its backend/runtime, and Chez has compute-size:

> (compute-size "a")
16
> (compute-size "ab")
16
> (compute-size "abc")
32
> (compute-size "abcdef")
32
> (compute-size "abcdefg")
48
> (compute-size "abcdefghij")
48
> (compute-size "abcdefghijk")
64
> (compute-size #vu8(1))
16
> (compute-size #vu8(1 2 3 4 5 6 7 8))
16
> (compute-size #vu8(1 2 3 4 5 6 7 8 9))
32
> 

CodePudding user response:

Racket (CS) internally represents strings as UTF-32, so each character occupies 4 bytes. On a 64-bit platform, a string has an 8 byte header and is 16-byte aligned.

A byte string is an array of bytes by definition.

CodePudding user response:

Peter Winton's answer is the right one. But here is a way of guestimating this:

(require racket/sandbox)

(define (ts mb sl)
  (with-limits #f mb
    (let ((s (make-string sl #\s)))
      (collect-garbage)
      (string-length s))))

Now, for instance (ts 4 1000000) will succeed, but (ts 3 1000000) will fail.

  • Related