Difference between revisions of "Packed"

From Lazarus wiki
Jump to navigationJump to search
m (→‎circumvention: grammar)
m (replace legacy syntaxhighlight syntax)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{packed}}
 
{{packed}}
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">packed</syntaxhighlight> tells the compiler to use as little memory as possible for a particular complex data type.
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight> tells the compiler to use as little memory as possible for a particular complex data type.
Without specifying <syntaxhighlight lang="pascal" enclose="none">packed</syntaxhighlight>, the compiler may insert extra unused bytes between members in order to align the data on full word boundaries for faster access by the CPU.
+
Without specifying <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight>, the compiler may insert extra unused bytes between members in order to align the data on full word boundaries for faster access by the CPU.
  
 
<syntaxhighlight lang="pascal" line highlight="9">
 
<syntaxhighlight lang="pascal" line highlight="9">
Line 24: Line 24:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
So <syntaxhighlight lang="pascal" enclose="none">packed</syntaxhighlight> sacrifices some speed while reducing the memory used.
+
So <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight> sacrifices some speed while reducing the memory used.
 +
 
 +
== Circumvention ==
  
== circumvention ==
 
 
Smart ordering of record elements can mitigate the issue.
 
Smart ordering of record elements can mitigate the issue.
In the example above <syntaxhighlight lang="pascal" enclose="none">signedQword</syntaxhighlight> as well <syntaxhighlight lang="pascal" enclose="none">signedQwordPacked</syntaxhighlight> have <syntaxhighlight lang="pascal" enclose="none">signum</syntaxhighlight> located at an offset of&nbsp;<syntaxhighlight lang="pascal" enclose="none">8</syntaxhighlight>.
+
In the example above <syntaxhighlight lang="pascal" inline>signedQword</syntaxhighlight> as well <syntaxhighlight lang="pascal" inline>signedQwordPacked</syntaxhighlight> have <syntaxhighlight lang="pascal" inline>signum</syntaxhighlight> located at an offset of&nbsp;<syntaxhighlight lang="pascal" inline>8</syntaxhighlight>.
 
That is OK.
 
That is OK.
If for any reason <syntaxhighlight lang="pascal" enclose="none">value</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">signum</syntaxhighlight> were listed in reverse order, <syntaxhighlight lang="pascal" enclose="none">signum</syntaxhighlight> would appear in both versions at an offset of&nbsp;<syntaxhighlight lang="pascal" enclose="none">0</syntaxhighlight>, but in the <syntaxhighlight lang="pascal" enclose="none">packed</syntaxhighlight> version <syntaxhighlight lang="pascal" enclose="none">value</syntaxhighlight> has an offset of&nbsp;<syntaxhighlight lang="pascal" enclose="none">1</syntaxhighlight>&nbsp;Byte.
+
If for any reason <syntaxhighlight lang="pascal" inline>value</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>signum</syntaxhighlight> were listed in reverse order, <syntaxhighlight lang="pascal" inline>signum</syntaxhighlight> would appear in both versions at an offset of&nbsp;<syntaxhighlight lang="pascal" inline>0</syntaxhighlight>, but in the <syntaxhighlight lang="pascal" inline>packed</syntaxhighlight> version <syntaxhighlight lang="pascal" inline>value</syntaxhighlight> has an offset of&nbsp;<syntaxhighlight lang="pascal" inline>1</syntaxhighlight>&nbsp;Byte.
This might cause a potentially slower access to the <syntaxhighlight lang="pascal" enclose="none">value</syntaxhighlight> field.
+
This might cause a potentially slower access to the <syntaxhighlight lang="pascal" inline>value</syntaxhighlight> field.
 +
 
 +
== See also ==
  
== see also ==
+
* [[sA|<syntaxhighlight lang="pascal" inline>{$align}</syntaxhighlight>]]
* [[sAlign|<syntaxhighlight lang="pascal" enclose="none">{$align}</syntaxhighlight>]]
+
* [[sPack|<syntaxhighlight lang="pascal" inline>{$pack}</syntaxhighlight>]]
* [[sPack|<syntaxhighlight lang="pascal" enclose="none">{$pack}</syntaxhighlight>]]
+
* procedures {{Doc|package=RTL|unit=system|identifier=pack|text=<syntaxhighlight lang="pascal" inline>pack</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=pack|text=<syntaxhighlight lang="pascal" inline>unpack</syntaxhighlight>}}
* procedures {{Doc|package=RTL|unit=system|identifier=pack|text=<syntaxhighlight lang="pascal" enclose="none">pack</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=pack|text=<syntaxhighlight lang="pascal" enclose="none">unpack</syntaxhighlight>}}
 
 
* [https://www.freepascal.org/docs-html/ref/refsu14.html “Packing and unpacking an array” in § “Arrays” of the Reference Guide]
 
* [https://www.freepascal.org/docs-html/ref/refsu14.html “Packing and unpacking an array” in § “Arrays” of the Reference Guide]
* [[Bitpacked|<syntaxhighlight lang="pascal" enclose="none">bitpacked</syntaxhighlight>]]
+
* [[Bitpacked|<syntaxhighlight lang="pascal" inline>bitpacked</syntaxhighlight>]]

Latest revision as of 00:00, 17 May 2020

Deutsch (de) English (en) русский (ru)

The reserved word packed tells the compiler to use as little memory as possible for a particular complex data type. Without specifying packed, the compiler may insert extra unused bytes between members in order to align the data on full word boundaries for faster access by the CPU.

 1program packedDemo(input, output, stderr);
 2
 3type
 4	signedQword = record
 5		value: qword;
 6		signum: -1..1;
 7	end;
 8	
 9	signedQwordPacked = packed record
10		value: qword;
11		signum: -1..1;
12	end;
13
14begin
15	writeLn('      signedQword: ', sizeOf(signedQword), 'B');
16	writeLn('signedQwordPacked: ', sizeOf(signedQwordPacked), 'B');
17end.

So packed sacrifices some speed while reducing the memory used.

Circumvention

Smart ordering of record elements can mitigate the issue. In the example above signedQword as well signedQwordPacked have signum located at an offset of 8. That is OK. If for any reason value and signum were listed in reverse order, signum would appear in both versions at an offset of 0, but in the packed version value has an offset of 1 Byte. This might cause a potentially slower access to the value field.

See also