What is more (memory) efficient lazarus / 21-General ---------------------------------------------------------------------- JdeHaan · 2025-02-05 12:40 Using this example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TValueType = (vtBoolean, vtInteger, vtReal);  TValue =  record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end;   TValueArray = array [1..10] of TValue; The size of the record is 16 bytes, and the array occupies 160 bytes. But if I use 'packed', like so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TValue = packed record  etc Then the record size is 9 bytes and the array is 90 bytes. But the question is, what is more efficient in a 64 bit CPU, considering aligning and processing speed? How does the FPC compiler handle this? ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 12:56 Strange, on my machine it's 12 bytes and 120 bytes FPC[main] Win x64 And I have a question - where did 4 of those 12 bytes go?   I've never used union tightly, and I always thought that its size is equal to the largest in union In D12CE it's 9 and 90. UPD: Yep, after I switched to {$mode delphi} - I got 9 and 90 too but why 9 ? one byte occupies a type ? UPD2: Yep, that's exactly what it is. ---------------------------------------------------------------------- alpine · 2025-02-05 13:00 Quote from: ALLIGATOR on February 05, 2025, 01:56:34 pmI've never used union tightly, and I always thought that its size is equal to the largest in union + For the Typ field. ---------------------------------------------------------------------- JdeHaan · 2025-02-05 13:03 Indeed strange. I think it's automatically padding the typ field to 8 bytes. Forgot to mention I'm using MacOS Sequoia 15.3 on an Intel Macbook Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin- cocoa ---------------------------------------------------------------------- Martin_fr · 2025-02-05 13:09 Quote from: JdeHaan on February 05, 2025, 01:40:47 pmBut the question is, what is more efficient in a 64 bit CPU, considering aligning and processing speed? How does the FPC compiler handle this? I don't know the answer... But, when you say "64 bit CPU" => do you mean any 64 bit CPU? I figure that includes Apple M CPUs, and other RISC... Or do you mean strictly Intel/AMD x86 CPUs? From what I recall to have heard (and really not sure) RISC CPU may either be slow to access badly aligned data, or even throw an exception and not read the data at all. Strictly INTEL/AMD, I don't know (and therefore also don't know if there would be a different by more recent vs older). I would suspect that maybe the double value could be affected. But again, I don't know. How badly do you need to save that one byte? (Understanding that your real array is probably a lot bigger, but still...) It isn't like you get much more data into a cache line, so you don't really gain speed by reducing cache misses. ---------------------------------------------------------------------- 440bx · 2025-02-05 13:16 Quote from: JdeHaan on February 05, 2025, 01:40:47 pmBut the question is, what is more efficient in a 64 bit CPU, considering aligning and processing speed? For speed, the unpacked record will be aligned therefore it will save the CPU having to align the data. Quote from: JdeHaan on February 05, 2025, 01:40:47 pmHow does the FPC compiler handle this? Depends, if the record is packed then the compiler "squeezes" all the fields so there are no unused bytes between them.  If it's not packed then the int64 and double cause 7 filler bytes to align those fields. As far as "efficient", speed-wise the unpacked (i.e., aligned) record is more efficient but storage-wise it is not efficient. It's the exact opposite for the packed record (sort of "the other side of the coin".) I don't know the details of the ARM architecture but, the vast majority of CPUs "prefer" the data types to be aligned on their natural boundaries. ---------------------------------------------------------------------- Warfley · 2025-02-05 13:16 The Compiler aligns memory to be most efficient for processing. If you use packed, packrecords or alignment directives you overwrite the behavior to have a specific memory layout, but potentially at the cost of execution speed. Generally speaking compilers usually prefer execution speed over memory consumption because memory is cheap (32 gigs of ram cost like 50€) while processing power is expensive (single core processing speed hasn't increased in decades). That said, it always depends on the specific code. If there is really a performance difference depends on the exact data and access pattern and so on. What I have outlined above is just the general trend and intention behind alignment and packed ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 13:43 my bench: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;{$mode delphi}{$optimization on} uses SysUtils; type  TValueType = (vtBoolean, vtInteger, vtReal);  PValueNotPacked = ^TValueNotPacked;  TValueNotPacked = record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end;  PValuePacked = ^TValuePacked;  TValuePacked = packed record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end; const  size = 512*1024*1024; procedure bench_notpacked1;var  arr_notpacked: array of TValueNotPacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr_notpacked, size);  t:=Now;  begin    for i:=0 to size-1 do      s+=arr_notpacked[i].IntVal;  end;  WriteLn('Not packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_notpacked, 0);end; procedure bench_notpacked2;var  arr_notpacked: array of TValueNotPacked;  s: NativeInt;  t: TDateTime;  p, pend: PValueNotPacked;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr_notpacked, size);  t:=Now;  begin    p:=@arr_notpacked[0];    pend:=@arr_notpacked[size-1];    while p<=pend do    begin      s+=p.IntVal;      inc(p);    end;  end;  WriteLn('Not packed 2: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_notpacked, 0);end; procedure bench_packed1;var  arr_packed: array of TValuePacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr_packed, size);  t:=Now;  begin    for i:=0 to size-1 do      s+=arr_packed[i].IntVal;  end;  WriteLn('Packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_packed, 0);end; procedure bench_packed2;var  arr_packed: array of TValuePacked;  s: NativeInt;  t: TDateTime;  p, pend: PValuePacked;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr_packed, size);  t:=Now;  p:=@arr_packed[0];  pend:=@arr_packed[size-1];  while p<=pend do  begin    s+=p.IntVal;  inc(p);  end;  WriteLn('Packed 2: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_packed, 0);end; begin  bench_notpacked1;  bench_notpacked2;  bench_packed1;  bench_packed2;  ReadLn;end.  i7-8750H laptop, DDR4 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 16, Not packed 1: 558 ms.16, Not packed 2: 467 ms.9, Packed 1: 425 ms.9, Packed 2: 310 ms. ---------------------------------------------------------------------- JdeHaan · 2025-02-05 14:29 On my MacBookPro from 2019 (2,6 GHz 6-Core Intel Core i7; 16 GB 2667 MHz DDR4): 16, Not packed 1: 2003 ms. 16, Not packed 2: 609 ms. 9, Packed 1: 461 ms. 9, Packed 2: 373 ms. So, conclusion is to use packed and pointer arithmetic? Note, after the 3rd run the first Not Packed drops to around 700ms - probably due to caching? ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 14:37 Quote from: Warfley on February 05, 2025, 02:16:36 pmbecause memory is cheap (32 gigs of ram cost like 50€) cheap per gigabyte, but expensive for access time - so all algorithms for processing large amounts of data should primarily look at ways to optimize the location of data in memory, e.g. AoS and SoA But it can also be the case that the computation is more expensive (time consuming) per “byte” than the reading of that “byte” from external memory itself ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 14:41 Quote from: JdeHaan on February 05, 2025, 03:29:30 pmSo, conclusion is to use packed and pointer arithmetic? The best answer is to test on your data, on your algorithm, and on all possible hardware where your algorithm will run... Even on x86_64 the performance of the same code may vary from generation to generation Use SIMD and proven algorithms that big minds have already worked on ---------------------------------------------------------------------- Warfley · 2025-02-05 14:47 Quote from: ALLIGATOR on February 05, 2025, 03:37:56 pmcheap per gigabyte, but expensive for access time - so all algorithms for processing large amounts of data should primarily look at ways to optimize the location of data in memory, e.g. AoS and SoA But it can also be the case that the computation is more expensive (time consuming) per “byte” than the reading of that “byte” from external memory itself As I said, depends on the access pattern. In your benchmark the most obvious optimization would be to use an array of Integer and ditch records completely, because you only ever access the int value field. But I'd argue that this hardly represents real world applications Record alignment is best for random access to fields, which is a pretty good assumption in most high level code, even tho not always true ---------------------------------------------------------------------- BrunoK · 2025-02-05 15:03 Quote from: 440bx on February 05, 2025, 02:16:11 pmQuote from: JdeHaan on February 05, 2025, 01:40:47 pmBut the question is, what is more efficient in a 64 bit CPU, considering aligning and processing speed? For speed, the unpacked record will be aligned therefore it will save the CPU having to align the data. Quote from: JdeHaan on February 05, 2025, 01:40:47 pmHow does the FPC compiler handle this? Depends, if the record is packed then the compiler "squeezes" all the fields so there are no unused bytes between them.  If it's not packed then the int64 and double cause 7 filler bytes to align those fields. As far as "efficient", speed-wise the unpacked (i.e., aligned) record is more efficient but storage-wise it is not efficient. It's the exact opposite for the packed record (sort of "the other side of the coin".) I don't know the details of the ARM architecture but, the vast majority of CPUs "prefer" the data types to be aligned on their natural boundaries. All the above affirmations do not match the results of ALLIGATOR's benchmark on my W10 Intel system. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 15:18 Quote from: Warfley on February 05, 2025, 03:47:26 pmRecord alignment is best for random access to fields, which is a pretty good assumption in most high level code, even tho not always true Ok, now with random access  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;{$mode delphi}{$optimization on} uses SysUtils; type  TValueType = (vtBoolean, vtInteger, vtReal);  PValueNotPacked = ^TValueNotPacked;  TValueNotPacked = record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end;  PValuePacked = ^TValuePacked;  TValuePacked = packed record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end; const  size = 128*1024*1024; function next(x: NativeUInt): NativeUInt; inline;begin  Result := (x * 1103515245 + 12345) mod size;end;  procedure bench_notpacked1;var  arr_notpacked: array of TValueNotPacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr_notpacked, size);  t:=Now;  begin    for i:=0 to size-1 do      s+=arr_notpacked[next(i)].IntVal;  end;  WriteLn('Not packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_notpacked, 0);end; procedure bench_packed1;var  arr_packed: array of TValuePacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr_packed, size);  t:=Now;  begin    for i:=0 to size-1 do      s+=arr_packed[next(i)].IntVal;  end;  WriteLn('Packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_packed, 0);end; begin  bench_notpacked1;  bench_packed1;  ReadLn;end.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 16, Not packed 1: 2783 ms.9, Packed 1: 1619 ms.  i7-8750H, laptop, ddr4 ---------------------------------------------------------------------- 440bx · 2025-02-05 15:26 Quote from: BrunoK on February 05, 2025, 04:03:05 pmAll the above affirmations do not match the results of ALLIGATOR's benchmark on my W10 Intel system. I noticed that too.  Something is wrong with that program because the aligned structure should yield better times. I'm guessing that the problem is in the memory allocation time consumed by SetLength.  Anyway, since there is obviously something wrong with that program, I created my own which gives the expected results.  Here is the program for anyone interested to try it out:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE}  program _Alignment; uses sysutils; type  TValueType = (vtBoolean, vtInteger, vtReal);   TValue1 =  record    case &Type: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal : Int64);      vtReal:     (RealVal: Double);  end;   TValue2 =  packed record    case &Type: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal : Int64);      vtReal:   (RealVal: Double);  end; type  RANGE       = 0..512 * 1024 - 1;   REPETITIONS = 0..499; var  ValueArray1 : array[RANGE] of TValue1;  ValueArray2 : array[RANGE] of TValue2;  procedure bench_notpacked;var  r   : REPETITIONS;  i   : intptr;   t   : TDateTime;begin  t := Now;   for r in REPETITIONS do  for i in RANGE do  with ValueArray1[i] do  begin   &Type  := vtInteger;   IntVal := 0;  end;   for r in REPETITIONS do  for i in RANGE do  begin    ValueArray1[i].IntVal += i;  end;   writeln('NOT packed time elapsed: ', (Now - t) * msecsperday:0:0, ' ms');end;  procedure bench_packed;var  r   : REPETITIONS;  i   : intptr;   t   : TDateTime;begin  t := Now;   for r in REPETITIONS do  for i in RANGE do  with ValueArray2[i] do  begin   &Type  := vtInteger;   IntVal := 0;  end;   for r in REPETITIONS do  for i in RANGE do  begin    ValueArray2[i].IntVal += i;  end;   writeln('    packed time elapsed: ', (Now - t) * msecsperday:0:0, ' ms');end;    begin   writeln('sizeof TValueType: ', sizeof(TValueType):2);   writeln('sizeof TValue1   : ', sizeof(TValue1):2);   writeln('sizeof TValue2   : ', sizeof(TValue2):2);   writeln('sizeof ValueArray1: ', sizeof(ValueArray1));   writeln('sizeof ValueArray2: ', sizeof(ValueArray2));   writeln;    bench_packed;   bench_notpacked;    writeln;   writeln('Press ENTER/RETURN to end this program');   readln; end. On my machine, the packed record is consistently about 10% slower. ---------------------------------------------------------------------- Martin_fr · 2025-02-05 15:28 Quote from: ALLIGATOR on February 05, 2025, 02:43:58 pmmy bench: See my post at https://forum.lazarus.freepascal.org/index.php/topic,70 056.msg545622.html#msg545622 (and a few posts later, how it actually applied to benchmarks in that thread) Benchmarks like this often have "arbitrary" speed changes. I have not checked (as far as it would be possible) if that is the case with the benchmark here. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 15:45 Quote from: 440bx on February 05, 2025, 04:26:35 pmOn my machine, the packed record is consistently about 10% slower. {$mode objfpc}{$optimization on} Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sizeof TValueType:  4sizeof TValue1   : 16sizeof TValue2   : 12sizeof ValueArray1: 8388608sizeof ValueArray2: 6291456     packed time elapsed: 414 msNOT packed time elapsed: 512 ms Press ENTER/RETURN to end this program {$mode delphi}{$optimization on} Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sizeof TValueType:  1sizeof TValue1   : 16sizeof TValue2   :  9sizeof ValueArray1: 8388608sizeof ValueArray2: 4718592     packed time elapsed: 347 msNOT packed time elapsed: 487 ms Press ENTER/RETURN to end this program  Have you enabled optimization? I did, and here's my result ---------------------------------------------------------------------- BrunoK · 2025-02-05 15:56 Quote from: 440bx on February 05, 2025, 04:26:35 pmQuote from: BrunoK on February 05, 2025, 04:03:05 pmAll the above affirmations do not match the results of ALLIGATOR's benchmark on my W10 Intel system. I noticed that too.  Something is wrong with that program because the aligned structure should yield better times. I'm guessing that the problem is in the memory allocation time consumed by SetLength.  Anyway, since there is obviously something wrong with that program, I created my own which gives the expected results.  Here is the program for anyone interested to try it out: ... On my machine, the packed record is consistently about 10% slower. Try this : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program _Alignment_BK;{$APPTYPE CONSOLE} uses  SysUtils; type  TValueType = (vtBoolean, vtInteger, vtReal);   TValue1 = record    case &Type: TValueType of      vtBoolean: (BoolVal: boolean);      vtInteger: (IntVal: int64);      vtReal: (RealVal: double);  end;   {$PUSH}  {$Z1}  TValueType2 = (vt2Boolean, vt2Integer, vt2Real);  {$POP}    TValue2 = packed record    case &Type: TValueType2 of      vt2Boolean: (BoolVal: boolean);      vt2Integer: (IntVal: int64);      vt2Real: (RealVal: double);  end; type  RANGE = 0..512 * 1024 - 1;   REPETITIONS = 0..499; var  ValueArray1: array[RANGE] of TValue1;  ValueArray2: array[RANGE] of TValue2;    procedure bench_notpacked;  var    r: REPETITIONS;    i: intptr;     t: TDateTime;  begin    t := Now;     for r in REPETITIONS do      for i in RANGE do        with ValueArray1[i] do        begin          &Type := vtInteger;          IntVal := 0;        end;     for r in REPETITIONS do      for i in RANGE do      begin        ValueArray1[i].IntVal += i;      end;     writeln('NOT packed time elapsed: ', (Now - t) * msecsperday: 0: 0, ' ms',      ' SizeOf(ValueArray1)=', SizeOf(ValueArray1));  end;    procedure bench_packed;  var    r: REPETITIONS;    i: intptr;     t: TDateTime;  begin    t := Now;     for r in REPETITIONS do      for i in RANGE do        with ValueArray2[i] do        begin          &Type := vt2Integer;          IntVal := 0;        end;     for r in REPETITIONS do      for i in RANGE do      begin        ValueArray2[i].IntVal += i;      end;     writeln('    packed time elapsed: ', (Now - t) * msecsperday: 0: 0, ' ms',      ' SizeOf(ValueArray2)=', SizeOf(ValueArray2));  end;   begin  writeln('sizeof TValueType: ', sizeof(TValueType): 2);  writeln('sizeof TValue1   : ', sizeof(TValue1): 2);  writeln('sizeof TValue2   : ', sizeof(TValue2): 2);  writeln('sizeof ValueArray1: ', sizeof(ValueArray1));  writeln('sizeof ValueArray2: ', sizeof(ValueArray2));  writeln;   bench_notpacked;  bench_packed;   writeln;  writeln('Press ENTER/RETURN to end this program');  readln;end. On my machine, -O1 -OoREGVAR : sizeof TValueType:  4 sizeof TValue1   : 16 sizeof TValue2   :  9 sizeof ValueArray1: 8388608 sizeof ValueArray2: 4718592 NOT packed time elapsed: 466 ms SizeOf(ValueArray1)=8388608     packed time elapsed: 276 ms SizeOf(ValueArray2)=4718592 Press ENTER/RETURN to end this program ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 16:05 Quote from: Martin_fr on February 05, 2025, 04:28:25 pmSee my post at https://forum.lazarus.freepascal.org/index.php/topic,70056.msg545622.h tml#msg545622 Yeah, I saw your post back in that thread. (thanks 👍, by the way) Your main point is about code alignment, I understand I accept it Yes, it does affect very much, but I want to consider the ideal case and discard variants with non-ideal code alignment in memory, exclude the part with code alignment (at the expense of any options to help the compiler, $codealign) and test only data handling ---------------------------------------------------------------------- 440bx · 2025-02-05 16:07 with Optimization level 1 and 4, the _unpacked_ is always faster than the packed (which is what it should be).  The same thing is true with optimization level 0. with Optimization levels 2 and 3, packed yields faster results than unpacked.  That makes NO sense whatsoever because the majority (if not all) CPUs are optimized to access aligned data. I don't know what FPC is doing but, it's the opposite of what happens with any other compiler (at least for optimization modes 2 and 3.) Too weird. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 16:19 Quote from: 440bx on February 05, 2025, 04:26:35 pmI created my own which gives the expected results.  Here is the program for anyone interested to try it out: ... On my machine, the packed record is consistently about 10% slower. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sizeof TValueType:  4sizeof TValue1   : 16sizeof TValue2   : 12sizeof ValueArray1: 8388608sizeof ValueArray2: 6291456  O0:    packed time elapsed: 995 msNOT packed time elapsed: 1056 ms O1:    packed time elapsed: 993 msNOT packed time elapsed: 1040 ms O2:    packed time elapsed: 390 msNOT packed time elapsed: 491 ms O3:    packed time elapsed: 361 msNOT packed time elapsed: 498 ms O4:    packed time elapsed: 377 msNOT packed time elapsed: 500 ms i7-8750H, laptop, ddr4 FPC[main] What's your hardware like? And the version of the compiler? ---------------------------------------------------------------------- BrunoK · 2025-02-05 16:26 Quote from: 440bx on February 05, 2025, 05:07:45 pmwith Optimization level 1 and 4, the _unpacked_ is always faster than the packed (which is what it should be).  The same thing is true with optimization level 0. with Optimization levels 2 and 3, packed yields faster results than unpacked.  That makes NO sense whatsoever because the majority (if not all) CPUs are optimized to access aligned data. I don't know what FPC is doing but, it's the opposite of what happens with any other compiler (at least for optimization modes 2 and 3.) Too weird. You have to consider that there is nearly 2 times more processed structures in the processor cache when the records are contiguous and packed. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 16:31 @440bx Could you please show me the results of this test? https://forum.lazarus.freepascal.org/index.php/topic,70108.msg546082.h tml#msg546082 ---------------------------------------------------------------------- 440bx · 2025-02-05 16:45 @ALLIGATOR, I find your results truly baffling. Anyway, I'm using FPC v3.2.2 (the stable release version) on Win7 on an i860 (a fairly old CPU but that doesn't matter in this case.) These are the results I get from the program you linked to (program untouched, run as-is): 16, Not packed 1: 56895 ms. 9, Packed 1: 13330 ms. results with O4: 16, Not packed 1: 15464 ms. 9, Packed 1: 13147 ms. It's quite telling that with O4 the packed time is basically unchanged whereas the unpacked one is over almost 4 times faster. There's something fishy in there. @BrunoK, I understand what you're saying about the packing and the cache space but, this is the first time _ever_ that I see unaligned data be processed faster than aligned data.  I cannot argue the timing results, they are what they are but, I believe there is something "not quite right" someplace.  I just don't know where. I believe that in the worst possible case, aligned data would take as long as unaligned data.  IOW, FPC has got the world upside down. ETA: I wonder what the results from Delphi are... It would be nice if someone with a copy of Delphi would run the programs and post the results. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 17:06 Quote from: 440bx on February 05, 2025, 05:45:41 pmThese are the results I get from the program you linked to (program untouched, run as-is): Thank you! 🙏 Quote from: 440bx on February 05, 2025, 05:45:41 pmI wonder what the results from Delphi are... It would be nice if someone with a copy of Delphi would run the programs and post the results. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i D12CE_x64_Release_test_v1_sequential_access:16, Not packed 1: 862 ms.16, Not packed 2: 927 ms.9, Packed 1: 813 ms.9, Packed 2: 849 ms. D12CE_x64_Release_test_v2_random_access:16, Not packed 1: 2854 ms.9, Packed 1: 1626 ms. Attached the executables, (if you're not afraid  ) ---------------------------------------------------------------------- BildatBoffin · 2025-02-05 17:09 The packed version requires an LEA instruction to access array elem, while the non-packed can just access the base ptr + some offset. See https://godbolt.org/z/x1M9s3ndY. So packed here saves the heap space, not the cpu time. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 17:25 Quote from: BildatBoffin on February 05, 2025, 06:09:34 pmThe packed version requires an LEA instruction to access array elem, while the non-packed can just access the base ptr + some offset. See https://godbolt.org/z/x1M9s3ndY. So packed here saves the heap space, not the cpu time. And you run a benchmark and see  Besides, what's so scary about the LEA instructions ?  difference: Packed: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i leaq    (%rdx,%rdx,8),%rcx  Not Packed: Code: ASM  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i movq    %rdx,%rcxshlq    $4,%rcx  lea will be just as fast as mov+shl ---------------------------------------------------------------------- 440bx · 2025-02-05 17:30 @ALLIGATOR, Thank you for trying it in Delphi.  I appreciate that. ---------------------------------------------------------------------- Zoran · 2025-02-05 17:47 Quote from: JdeHaan on February 05, 2025, 01:40:47 pmUsing this example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TValueType = (vtBoolean, vtInteger, vtReal);  TValue =  record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end;   TValueArray = array [1..10] of TValue;  Quote from: ALLIGATOR on February 05, 2025, 01:56:34 pmStrange, on my machine it's 12 bytes and 120 bytes FPC[main] Win x64 And I have a question - where did 4 of those 12 bytes go?   I've never used union tightly, and I always thought that its size is equal to the largest in union In D12CE it's 9 and 90. UPD: Yep, after I switched to {$mode delphi} - I got 9 and 90 too but why 9 ? one byte occupies a type ? UPD2: Yep, that's exactly what it is. Just note that you probably don't need the "case selector" variable. If you use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TValueType = (vtBoolean, vtInteger, vtReal);  TValue = record    case TValueType of  // note - variable Typ removed      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:   (RealVal: Double);  end;  Then the size I get (x86_64 and i386) is 8 bytes. ---------------------------------------------------------------------- BrunoK · 2025-02-05 18:06 Quote from: Zoran on February 05, 2025, 06:47:57 pm Just note that you probably don't need the "case selector" variable. If you use: ... Then the size I get (x86_64 and i386) is 8 bytes. Didn't know that the case selector field could be stored this way. Might be quite useful for variable records mapping; learned something new today. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 18:09 Quote from: Zoran on February 05, 2025, 06:47:57 pmThen the size I get (x86_64 and i386) is 8 bytes. Wow! Cool! (I suspected it should be possible  but I don't work with unions) ---------------------------------------------------------------------- JdeHaan · 2025-02-05 18:37 Thanks @Zoran, I'll explore that option. ---------------------------------------------------------------------- Warfley · 2025-02-05 19:02 Quote from: ALLIGATOR on February 05, 2025, 04:18:50 pmOk, now with random access  Not random array access random field access. E.g. you got a record with 10 fields, and you access them "randomly" (of course not like random index wise, but with complex code that accesses the fields in no particular order). Your code only ever accesses the IntVal field, not even the Typ field. If I just change your code slighty, to also consider other the Typ field (and initializing the data to avoid predictive branching): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;{$mode delphi}{$optimization on} uses SysUtils; type  TValueType = (vtBoolean, vtInteger, vtReal);  PValueNotPacked = ^TValueNotPacked;  TValueNotPacked = record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end;  PValuePacked = ^TValuePacked;  TValuePacked = packed record    case Typ: TValueType of      vtBoolean:  (BoolVal: Boolean);      vtInteger:  (IntVal: Int64);      vtReal:     (RealVal: Double);  end; const  size = 512*1024*1024; procedure bench_notpacked1;var  arr_notpacked: array of TValueNotPacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr_notpacked, size);  for i:=0 to size-1 do  begin    arr_notpacked[i].Typ:=TValueType(Random(ord(High(TValueType)))); // Avoids Real, because Doubles have more overhead than ordinal types    arr_notpacked[i].RealVal:=Random;  end;  t:=Now;  begin    for i:=0 to size-1 do      case arr_notpacked[i].Typ of      vtBoolean: s += ord(arr_notpacked[i].BoolVal);      vtInteger: s += arr_notpacked[i].IntVal;      vtReal: s += Trunc(arr_notpacked[i].RealVal);      end;  end;  WriteLn('Not packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_notpacked, 0);end; procedure bench_notpacked2;var  arr_notpacked: array of TValueNotPacked;  s: NativeInt;  t: TDateTime;  p, pend: PValueNotPacked;  i: NativeInt;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr_notpacked, size);  for i:=0 to size-1 do  begin    arr_notpacked[i].Typ:=TValueType(Random(ord(High(TValueType)))); // Avoids Real, because Doubles have more overhead than ordinal types    arr_notpacked[i].RealVal:=Random;  end;  t:=Now;  begin    p:=@arr_notpacked[0];    pend:=@arr_notpacked[size-1];    while p<=pend do    begin      case p^.Typ of      vtBoolean: s += ord(p^.BoolVal);      vtInteger: s += p^.IntVal;      vtReal: s += Trunc(p^.RealVal);      end;      inc(p);    end;  end;  WriteLn('Not packed 2: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_notpacked, 0);end; procedure bench_packed1;var  arr_packed: array of TValuePacked;  i, s: NativeInt;  t: TDateTime;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr_packed, size);  for i:=0 to size-1 do  begin    arr_packed[i].Typ:=TValueType(Random(ord(High(TValueType)))); // Avoids Real, because Doubles have more overhead than ordinal types    arr_packed[i].RealVal:=Random;  end;  t:=Now;  begin    for i:=0 to size-1 do      case arr_packed[i].Typ of      vtBoolean: s += ord(arr_packed[i].BoolVal);      vtInteger: s += arr_packed[i].IntVal;      vtReal: s += Trunc(arr_packed[i].RealVal);      end;  end;  WriteLn('Packed 1: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_packed, 0);end; procedure bench_packed2;var  arr_packed: array of TValuePacked;  s: NativeInt;  t: TDateTime;  p, pend: PValuePacked;  i: NativeInt;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr_packed, size);  for i:=0 to size-1 do  begin    arr_packed[i].Typ:=TValueType(Random(ord(High(TValueType)))); // Avoids Real, because Doubles have more overhead than ordinal types    arr_packed[i].RealVal:=Random;  end;  t:=Now;  p:=@arr_packed[0];  pend:=@arr_packed[size-1];  while p<=pend do  begin    case p^.Typ of    vtBoolean: s += ord(p^.BoolVal);    vtInteger: s += p^.IntVal;    vtReal: s += Trunc(p^.RealVal);    end;    inc(p);  end;  WriteLn('Packed 2: ',(Now-t)*MSecsPerDay:0:0, ' ms.');  SetLength(arr_packed, 0);end; begin  bench_notpacked1;  bench_notpacked2;  bench_packed1;  bench_packed2;  ReadLn;end.The difference in timing shrinks already:: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 16, Not packed 1: 2656 ms.16, Not packed 2: 2377 ms.9, Packed 1: 2516 ms.9, Packed 2: 2312 ms.While a change in relative speed difference was to be expected, because the code does now much more, even the absolute speed difference is now much smaller, where before it was consistently between 100-200ms on my system, it's now more around 50ms, with pointer arithmetic on not packed always outperforming array access on packed. Adding more fields with different alignment, which are accessed in the for loop will most likely change the results further to the point where the performance advantage of packed will have swapped A benchmark is only as good as it's transferability to real world code. Sure you can show a big difference in your benchmark, but how realistically is it that you create a record with multiple fields but only ever access a single one of these fields in a deterministic manner? ---------------------------------------------------------------------- Martin_fr · 2025-02-05 19:12 It may be that Intel does not suffer much from non-alignment. And/Or that the CPU pre-fetching is faster than the execution of the code. So that the data is always retrieved that early. In that case having to load less memory into the cache will be helpful. I tried one of the benchmarks https://forum.lazarus.freepascal.org/ind ex.php/topic,70108.msg546065.html#msg546065 but changed it from using the int value to using the double value. In that case packed and unpacked were at the same speed for me. So likely the floating point unit benefits from aligned data, and took more time fetching the non aligned data. But the extra time taken equalled out with the savings of less memory to be loaded into the cache. ---------------------------------------------------------------------- Martin_fr · 2025-02-05 19:19 And, btw, if I make both records equal size (by removing "typ") "case  TValueType of" and removing "packed" => the packed run is still faster by a tiny bit (550 <> 520). Though -despite showing up on all of many runs - this is within error margin. But that just goes to show, never trust a benchmark. (not that I follow my own advise) ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 19:37 Quote from: Warfley on February 05, 2025, 08:02:29 pmA benchmark is only as good as it's transferability to real world code. Sure you can show a big difference in your benchmark, but how realistically is it that you create a record with multiple fields but only ever access a single one of these fields in a deterministic manner? That's why I answered the topic starter in one of the first replies, that he should test on his data and algorithm and his range of possible hardware 💁‍♂️ ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 19:53 Quote from: Warfley on February 05, 2025, 08:02:29 pmCode: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 16, Not packed 1: 2656 ms.16, Not packed 2: 2377 ms.9, Packed 1: 2516 ms.9, Packed 2: 2312 ms.And still packed a little yes won in this particular test ) But it implies data processing - it affects of course, but as they say, while the processor goes to RAM for data, 100 cycles will pass, during this time you can calculate and make a conditional jump ) The processor is much faster than RAM in the current times --- In fact, this whole task comes down to cache operation, you don't have to declare any packed structures, but just declare one array and iterate over it with different steps but the same number of iterations. For example 1,3,4,7,8,8,12,31,32,33,36,63,64,65,100,160 and so on. And you can also split this range into two: into aligned steps (for example 4 or 8 or 16) and unaligned steps (3,7,15 or 5,9,17). Here you go... for now ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 20:18 Quote from: Warfley on February 05, 2025, 08:02:29 pmNot random array access random field access. E.g. you got a record with 10 fields, and you access them "randomly" (of course not like random index wise, but with complex code that accesses the fields in no particular order). Another benchmark where everything is randomized (as you like  ) and access to fields and access to array elements ) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;{$mode delphi}{$optimization on} uses SysUtils; type  TValueType = 0..7;  PValueNotPacked = ^TValueNotPacked;  TValueNotPacked = record    case Typ: TValueType of      0:  (Bool_: Boolean);      1:  (Int32_: Int64);      2:  (Int64_: Int64);      3:  (Int8_: Int8);    4:  (Int16: Int16);      5:  (Real_: Real);      6:  (Double_: Double);      7:  (UInt8_: UInt8);  end;  PValuePacked = ^TValuePacked;  TValuePacked = packed record    case Typ: TValueType of      0:  (Bool_: Boolean);      1:  (Int32_: Int64);      2:  (Int64_: Int64);      3:  (Int8_: Int8);      4:  (Int16: Int16);    5:  (Real_: Real);      6:  (Double_: Double);      7:  (UInt8_: UInt8);  end; const  size = 128*1024*1024; function next_arr(x: NativeUInt): NativeUInt; inline;begin  Result := (x * 1103515245 + 12345) mod size;end; function next_typ(x: NativeUInt): NativeUInt; inline;begin  Result := (x * 1103515245 + 12345) mod 8;end;  procedure bench_notpacked;var  arr: array of TValueNotPacked;  i: NativeInt;  t: TDateTime;  sb: Boolean;  si32: Int32;  si64: Int64;  si8: Int8;  si16: Int16;  sr: Real;  sd: Double;  su8: UInt8;begin  Write(SizeOf(TValueNotPacked), ', ');  SetLength(arr, size);  t:=Now;  for i:=0 to size-1 do    case next_typ(i) of      0: sb   := arr[next_arr(i)].Bool_  or sb;      1: si32 := arr[next_arr(i)].Int32_  + si32;      2: si64 := arr[next_arr(i)].Int64_  + si64;      3: si8  := arr[next_arr(i)].Int8_   + si8;      4: si16 := arr[next_arr(i)].Int16   + si16;      5: sr   := arr[next_arr(i)].Real_   + sr;      6: sd   := arr[next_arr(i)].Double_ + sd;      7: su8  := arr[next_arr(i)].UInt8_  + su8;    end;  WriteLn('Not packed: ',(Now-t)*MSecsPerDay:0:0, ' ms.');end; procedure bench_packed;var  arr: array of TValuePacked;  i: NativeInt;  t: TDateTime;  sb: Boolean;  si32: Int32;  si64: Int64;  si8: Int8;  si16: Int16;  sr: Real;  sd: Double;  su8: UInt8;begin  Write(SizeOf(TValuePacked), ', ');  SetLength(arr, size);  t:=Now;  for i:=0 to size-1 do    case next_typ(i) of      0: sb   := arr[next_arr(i)].Bool_  or sb;      1: si32 := arr[next_arr(i)].Int32_  + si32;      2: si64 := arr[next_arr(i)].Int64_  + si64;      3: si8  := arr[next_arr(i)].Int8_   + si8;      4: si16 := arr[next_arr(i)].Int16   + si16;      5: sr   := arr[next_arr(i)].Real_   + sr;      6: sd   := arr[next_arr(i)].Double_ + sd;      7: su8  := arr[next_arr(i)].UInt8_  + su8;    end;  WriteLn('Packed: ',(Now-t)*MSecsPerDay:0:0, ' ms.');end; begin  bench_notpacked;  bench_packed;  ReadLn;end. i7-8750H, ddr4 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 16, Not packed: 2555 ms.9, Packed: 1733 ms. ---------------------------------------------------------------------- Warfley · 2025-02-05 20:48 Quote from: ALLIGATOR on February 05, 2025, 09:18:55 pmAnother benchmark where everything is randomized (as you like  ) and access to fields and access to array elements ) Because it's a variant record, you access the same field, just under different names. Let's change this to actually be different fields with different alignment: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TValue[Not]Packed = [packed] record    Typ: TValueType;    Bool_: Boolean;    Int32_: Int64;    Int64_: Int64;    Int8_: Int8;    Int16: Int16;    Real_: Real;    Double_: Double;    UInt8_: UInt8;  end; Now packed is slower: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 56, Not packed: 6453 ms.38, Packed: 6518 ms.  There are certainly cases where you don't need alignment, especially on an x64 (on ARM it's a whole different story tho). But Compilers try to optimize the general case, and generally speaking you have records that contain more than just one field ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 21:18 Quote from: Warfley on February 05, 2025, 09:48:36 pmNow packed is slower: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 56, Not packed: 6453 ms.38, Packed: 6518 ms.  On my hardware) : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 56, Not packed: 4119 ms.38, Packed: 3785 ms. ---------------------------------------------------------------------- 440bx · 2025-02-06 05:14 I believe it's important to keep in mind that the benchmark results may _not_ be an indication of the effects of aligning data but of how well (or poorly) the compiler optimizes a specific case. ---------------------------------------------------------------------- TRon · 2025-02-06 05:58 Besides that, not everyone has the same processor and the benchmarks as shown so far are not real world situations but optimized benchmarks tailored to do one thing its good at. Such small benchmarks on modern processor tells absolutely nothing, irl they act more like a steam machine and reach an optimal performance after a certain amount of time. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 06:37 I tried to implement an algorithm to test the processor cache performance depending on the “record size”, or in other words the data step/size But the test results do not satisfy me, earlier on the research charts I saw a completely different picture, there were certain drawdowns at the boundaries of cache sizes of different levels, I don't see this in myself... I am posting the code, maybe someone will be interested (and I am also interested if someone will find a possible error in the code, why my expectation does not coincide with reality, it is desirable, of course, to confirm your words with references/test programs and not to rely on “my inner feeling”  ). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;{$mode objfpc}{$optimization on} uses SysUtils; type  TMyArray = array of NativeInt;  TBenchProc = procedure (const arr: TMyArray; step: NativeInt); const  step_over = 10;  max_step = 128*1024 + step_over;  steps_count = 16*1024*1024;  size = 2*1024*1024*1024 div sizeof(TMyArray[0]); procedure bench_read(const arr: TMyArray; step: NativeInt);var  i, dummy: NativeInt;  c: NativeInt = 0;  timer: TDateTime;begin  if step>max_step then Halt(1);  timer:=Now;  while c < steps_count do  begin    if (size div step)>(steps_count-c) then    i:=size - (steps_count-c)*step    else      i:=0;    while (i < size) do    begin      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);    dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);    dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);    dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      dummy := arr[i]; inc(i, step);      inc(c, 16);    end;  end;  WriteLn('step: ', step: 8, ', time: ', (Now-timer)*MSecsPerDay:0:0, ' ms');end; procedure bench_write(const arr: TMyArray; step: NativeInt);var  i: NativeInt;  c: NativeInt = 0;  timer: TDateTime;  zero: NativeInt = 0;begin  if step>max_step then Halt(1);  timer:=Now;  while c < steps_count do  begin    if (size div step)>(steps_count-c) then      i:=size - (steps_count-c)*step    else      i:=0;    while (i < size) do    begin      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      arr[i]:=zero; inc(i, step);      inc(c, 16);    end;  end;  WriteLn('step: ', step: 8, ', time: ', (Now- timer)*MSecsPerDay:0:0, ' ms');end; procedure bench_readwrite(const arr: TMyArray; step: NativeInt);var  i: NativeInt;  c: NativeInt = 0;  timer: TDateTime;  zero: NativeInt = 0;begin  if step>max_step then Halt(1);  timer:=Now;  while c < steps_count do  begin    if (size div step)>(steps_count-c) then      i:=size - (steps_count-c)*step    else      i:=0;    while (i < size) do    begin      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      arr[i]:=zero; inc(i, step);      zero:=arr[i]; inc(i, step);      inc(c, 16);    end;  end;  WriteLn('step: ', step: 8, ', time: ', (Now- timer)*MSecsPerDay:0:0, ' ms');end; var  bench_procs: array of TBenchProc = (@bench_read, @bench_write, @bench_readwrite);  steps: array of integer = (1,2,3,4,5,6,7,8,9,10,15,16,17,23,24,25,30,31,32,33 ,34,47,48,49,62,63,64,65,66,95,96,97,126,127,128,129,                           254,255,256,257,510,511,512,513,1022,1023,1024,1025,1026,20 46,2047,2048,2049,                             16382,16383,16384,16385,16386,                             32766,32767,32768,32769,32770,                             65534,65535,65536,65537,65538,                             131070,131071,131072,131073,131074                             );  arr: TMyArray;  st: NativeInt;  bench: TBenchProc; begin  SetLength(arr, size + max_step*16);   WriteLn('steps count: ', steps_count);   for bench in bench_procs do    for st in steps do    bench(arr, st);   ReadLn;end. ---------------------------------------------------------------------- DragoRosso · 2025-02-06 07:05 Quote from: ALLIGATOR on February 06, 2025, 07:37:55 amI tried to implement an algorithm to test the processor cache performance depending on the “record size”, or in other words the data step/size What makes you believe that with the proposed code you are analyzing the performance of the processor cache? ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 07:13 Oh, found a good article http://igoro.com/archive/gallery-of-processor-cache-effects/ ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 07:14 Quote from: DragoRosso on February 06, 2025, 08:05:22 amWhat makes you believe that with the proposed code you are analyzing the performance of the processor cache? Different step size for accessing data in memory ---------------------------------------------------------------------- DragoRosso · 2025-02-06 08:01 Yes, that was a good article to understand how the cache works. But, you don't know how the compiler optimize or simply "translate" your code. May be in assembler view should be the better way to analyze the fact. And you are sure about your interpretation of memory cache alghoritm ? I don't think that someone know it to determine exactly the function in this way (multi-level N-way set-associative caches questions). Other important things is that you are running a simple "routine" in a user level, with lower priority than the other "routines" of your system like drivers or others software that run at kernel mode or at other priority level. So you are "measure" the performance of your system, not cache processor, 'cause your routine wil be interrupt a lot of time, and you don't know if your code is running in more than one thread (also inside the same core). Other things are about all new "innovation" with new architecture like hybrid core, ITD (Intel thread director) and others ... ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 08:46 Quote from: DragoRosso on February 06, 2025, 09:01:01 amBut, you don't know how the compiler optimize or simply "translate" your code. May be in assembler view should be the better way to analyze the fact. I'm well aware of how this code optimized the compiler - since I'm looking at it, you're hopefully aware of this feature? --- Quote from: DragoRosso on February 06, 2025, 09:01:01 amOther important things is that you are running a simple "routine" in a user level, with lower priority than the other "routines" of your system like drivers or others software that run at kernel mode or at other priority level. So you are "measure" the performance of your system, not cache processor, 'cause your routine wil be interrupt a lot of time, and you don't know if your code is running in more than one thread (also inside the same core). Well, come on, because you yourself said that the article is good - so read it ) it says in black and white how you can determine the size of caches and their latency, at a fairly accurate level being in the third ring under the operating system. Again, it is not a problem for those who know what they are doing. For those who do not understand - yes, it may seem like magic! Quote from: DragoRosso on February 06, 2025, 09:01:01 amYes, that was a good article to understand how the cache works. --- Quote from: DragoRosso on February 06, 2025, 09:01:01 amOther things are about all new "innovation" with new architecture like hybrid core, ITD (Intel thread director) and others ... The technologies you have listed are easily configurable from the user mode, and even a simple setting of the correspondence to the processor core will level their influence up to 0 ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 08:50 Quote from: DragoRosso on February 06, 2025, 08:05:22 amWhat makes you believe that with the proposed code you are analyzing the performance of the processor cache? In turn, then I have to ask you - what makes you doubt that my code is not related to cache access performance analysis? ---------------------------------------------------------------------- Mr.Madguy · 2025-02-06 09:25 If memory isn't issue, then it's better to rely on default alignment, as it's better for performance. ---------------------------------------------------------------------- 440bx · 2025-02-06 10:41 Quote from: Mr.Madguy on February 06, 2025, 10:25:05 amIf memory isn't issue, then it's better to rely on default alignment, as it's better for performance. Actually, the measurements shown in this thread give a strong impression of the opposite being true. The default is unpacked, which means aligned.  The measurements shown in this thread show that accessing fields in a packed structure is faster. Honestly, in spite of the results that have been posted in this thread, I still believe that aligned data is accessed faster.  I suspect/guess that the results seen here are more likely to be a result of the compiler's ability to optimize some cases better than others, not really due to differences in access speed due to alignment.  That said, I'd be really hard pressed to prove this belief. ---------------------------------------------------------------------- DragoRosso · 2025-02-06 10:51 Quote from: ALLIGATOR on February 06, 2025, 09:50:35 amQuote from: DragoRosso on February 06, 2025, 08:05:22 amWhat makes you believe that with the proposed code you are analyzing the performance of the processor cache? In turn, then I have to ask you - what makes you doubt that my code is not related to cache access performance analysis? I don't have any doubts, I'm certain that your code is not related to cache (EDIT: TO MEASURE THE PERFORMANCE OF), I hoped that you explain why you think so, but you only explain about facts that are not in your code. I don't want to continue useless polemics. So for me the discussion ends here. Sorry if I questioned what you proposed.