简体   繁体   English

编译器如何在恒定时间内从对象数组中获取元素?

[英]How does compiler fetch elements from array of objects in constant time?

Whenever we want to fetch elements based on index from an array, I learned that the compiler just does something like this to fetch the element in constant time:每当我们想根据索引从数组中获取元素时,我了解到编译器只是做这样的事情来在恒定时间内获取元素:

array_addr + ele_size * (i - first_index) (where the ele_size depends on the type of the elements present in an array, eg for array[int] the ele_size will be 4 bytes) array_addr + ele_size * (i - first_index) (其中ele_size取决于数组中存在的元素的类型,例如对于array[int] ele_size将是 4 个字节)

So how does compiler fetch elements from array[object] in constant time, when each object could have a different size (in memory)?那么,当每个object可能具有不同的大小(在内存中)时,编译器如何在恒定时间内从array[object]获取元素?

PS: Question is not specific to any language PS:问题不针对任何语言

So how does compiler fetch elements from array[object] in constant time (as the size of the objects could vary)?那么编译器如何在恒定时间内从array[object]获取元素(因为对象的大小可能会有所不同)?

Well, the expression that is being evaluated is:好吧,正在评估的表达式是:

array_addr + ele_size * (i - first_index)

where ele_size is a constant that depends on the (compile time) type of the array, and first_index is also typically a constant;其中ele_size是一个常数,它取决于数组的(编译时)类型,而first_index通常也是一个常数; eg zero for C, C++, Java, etcetera.例如,C、C++、Java 等为零。

That is one subtraction (when first_index is not the constant zero), one multiplication, one addition, and possibly a memory fetch or two.那是一个减法(当first_index不是常数零时),一个乘法,一个加法,可能还有一个 memory 提取或两个。

Either way, the number of machine instructions required to perform the calculation is a constant.无论哪种方式,执行计算所需的机器指令数都是一个常数。 Hence (modulo quibbling about variable memory fetch times, pipeline effects, etc) the time taken for one such computation will be a constant... irrespective of the values of the notional expression parameters.因此(关于变量 memory 获取时间、流水线效果等的模数争论)一次这样的计算所花费的时间将是一个常数……与名义表达式参数的值无关。


Quibble: The compiler doesn't fetch the elements. Quibble:编译器不获取元素。 It generates code that will fetch elements... when the code is executed.生成将获取元素的代码......当代码执行时。

array[object] and array[int] are actually very similar, since an array of objects doesn't usually store the actual objects directly next to each other like this: array[object]array[int]实际上非常相似,因为对象数组通常不会像这样将实际对象直接存储在一起:

// [ _______Person________,  _____Pet_____,  _______Person_______ ]
   [ Name,    Age, Country,  Name,    Type,  Name,   Age, Country ]
   [ "Lucas", 37,  FRANCE,   "Misty", CAT,   "Emma", 22,  SWEDEN  ]    

Instead, the array just stores references (pointers) to the objects, which are all of the same size (32 or 64-bit int):相反,该数组只存储对对象的引用(指针),这些对象的大小都相同(32 位或 64 位 int):

// [ ______Person______,  ________Pet_______,  ______Person______ ]
   [ address of Person,   address of Pet,      address of Person  ]
   [ 0xaba11b8ae55fa4d2,  0xb8e4f4adea6be036,  0x5ce415a69ca50222 ]

Then the data for each object is found at the specific memory address and can occupy however many bytes is needed.然后在特定的 memory 地址中找到每个 object 的数据,并且可以占用任意多个字节。

You might find this question about how object[] is stored in memory (in C#) useful.您可能会发现这个关于object[]如何存储在 memory(在 C# 中)中的问题很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM