Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do scalar variables perform in memory?

Tags:

perl

In other languages besides Perl when you declare an integer it has minimum and maximum values based on the amount of space in memory the variable is taking up. When you declare a scalar variable in Perl, whether it be a number or string, does the language only allocate enough for the variable value and then increase the space if necessary later or does Perl allocate a large amount of memory initially?

like image 870
Azreal Avatar asked Oct 11 '13 01:10

Azreal


People also ask

What do you mean by scalar variable?

Scalar variables are used to represent individual fixed-size data objects, such as integers and pointers. Scalar variables can also be used for fixed-size objects that are composed of one or more primitive or composite types. D provides the ability to create both arrays of objects as well as composite structures.

What are scalar variables in Python?

A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an list variable.

What is a scalar variable in SQL?

A scalar variable stores a value with no internal components. The value can change. A scalar variable declaration specifies the name and data type of the variable and allocates storage for it. The declaration can also assign an initial value and impose the NOT NULL constraint.


1 Answers

In Perl, a scalar variable is a pointer to a C struct called an SV. This includes various fields for metadata like the reference count, a bitfield that determines the exact type, and a pointer to additional (meta-)data.

  • If you use a scalar as an integer, it is called an IV and contains an integer. The size of this integer is fixed at compilation of perl. You can look at the perl -V output to view the size of various data types. I have ivsize=8. The representable values are the same as for the C integer of that size.

  • If you use a scalar as a decimal, it is called an NV (numerical value) and contains a double, usually. Again, the exact size is determined at compile time.

  • If you use a scalar as a string, it is called a PV and contains a pointer to a C string, plus some additional metadata like length. The C string is reallocated if it grows.

  • If you use a scalar as a string and as a number, it is a PVIV or PVNV resp. and includes the data of both types.

  • There are additional types like references (RV) or unsigned integers (UV).

For the IV and NV, Perl does not automatically promote the numbers to bignums when they grow large enough.

Then there are hashes HV and arrays AV. These use the SV header for things like reference counting but point to more complicated data structures.

  • Arrays contain a C array of pointers to SVs. If the array grows, it is reallocated.

  • Hashes are far more complex. Basically, they are an array as well, but contain hash entries instead of SVs. The elements in this hash are called buckets. If the entries-to-buckets ratio is too high, the array is reallocated (usually to double size) and the entries newly distributed across these buckets. This isn't strictly neccessary, but if this isn't done then lookup is O(n) instead of O(1) (i.e. slow).


  • Variable sized data structures like strings, arrays, hashes are initially allocated conservatively. If more space is required, then a larger piece of memory is allocated, and the data copied over.

  • Scalars have a constant-sized header. Additional memory for additional metadata is allocated when the type changes (e.g. through stringification).

For more information and confusing pointer diagrams read the Illustrated Perl Guts.

like image 181
amon Avatar answered Sep 25 '22 15:09

amon