#include <iostream>
#include <stdint.h>
using namespace std;
struct UIContainer {
uint16_t x, y; //Position on the screen
uint16_t h, w; //Height and width of the UIContainer
uint16_t color; //Color, rgba such as 0xFF000000 & color is red, 0x00FF0000 is green, 0x0000FF00 is blue, 0x000000FF is alpha
uint16_t ID; //Unique ID of the ui container
}; //16 bytes big
void drawUI(UIContainer _container, SDL_Renderer* _renderer) {
SDL_Rect rect {.x = _container.x, .y = _container.y, .h = _container.h, .w = _container.w }
uint8_t r = color & 0xFF000000;
uint8_t g = color & 0x00FF0000;
uint8_t b = color & 0x0000FF00;
uint8_t a = color & 0x000000FF;
SDL_SetRenderDrawColor(_renderer, r, g, b, a);
SDL_RenderFillRect(_renderer, &rect);
}
int main()
{
UIContainer UIContainers[1024]; //16 * 1024 is 16384 bytes = 16 kilobytes
SDL_Renderer* renderer; //Pretend it is initialized
//Draw all the UI
int i = 0;
for(i; i < 1024; ++i) {
drawUI(_container, renderer);
}
return 0;
}
我決定嘗試瞭解數據本地化以及如何提高緩存的利用率。假設L1緩存爲64 KB,我認爲整個UIContainer陣列將被加載到緩存中是正確的,因爲16KB小於64KB?如果緩存行是128字節,那麼每行8個UIContainer塊?在緩存行中存儲了多少數組?
據我所知,當緩存未命中時,會發生緩存未命中。這是否也適用於緩存行?例如,我在容器[3]上運行,然後我想跳到容器[100],這會導致緩存未命中,因爲它必須跳至緩存行容器[100]所在的位置。
最後,假設我exctracted所有UIContainer的內部零件到自己單獨的陣列,這樣的代碼現在的樣子:
#include <iostream>
#include <stdint.h>
using namespace std;
struct location {
uint16_t x, y; //Position on the screen
}; //4 bytes
struct size {
uint16_t h, w; //Height and width of the UIContainer
}; //4 bytes
struct color {
uint32_t color; //Color, rgba such as 0xFF000000 & color is red, 0x00FF0000 is green, 0x0000FF00 is blue, 0x000000FF is alpha
} //4 bytes
struct UIContainer {
uint32_t ID; //Unique ID of the ui container
}; //4 bytes
void drawUI(location l, size s, color c, SDL_Renderer* _renderer) {
SDL_Rect rect {.x = l.x, .y = l.y, .h = s.h, .w = s.w }
uint8_t r = c & 0xFF000000;
uint8_t g = c & 0x00FF0000;
uint8_t b = c & 0x0000FF00;
uint8_t a = c & 0x000000FF;
SDL_SetRenderDrawColor(_renderer, r, g, b, a);
SDL_RenderFillRect(_renderer, &rect);
}
int main()
{
UIContainer UIContainers[1024]; //4 * 1024 is 4048 bytes = 4 kilobytes
location _location[1024]; //4 KB
size _size[1024]; //4KB
color _color[1024]; //4KB
//////////////////////////////////////// 16 KB Total
SDL_Renderer* renderer; //Pretend it is initialized
//Draw all the UI
int i = 0;
for(i; i < 1024; ++i) {
drawUI(_location[i], _size[i], _color[i], renderer);
}
return 0;
}
這會導致高速緩存未命中?我不這麼認爲,因爲_location [],_size []和_color []都在緩存中,並且線性訪問?或者我錯過了什麼?
你正在實現一個編譯器嗎?如果沒有,只要數據連續佈置,直到您通過測量證明問題,您應該不會在意。 – 2014-09-23 23:06:49
「你應該不會在乎的」從字面上來說是最糟糕的事情,尤其是當「我決定試着學習數據本地化以及如何提高緩存的利用率時」。 – 2014-09-23 23:10:17
緩存行爲並不完全可預測且非常複雜。你可能想看看[每個程序員需要了解的內存](https://lwn.net/Articles/250967/),以瞭解它是如何工作的。 – Jason 2014-09-23 23:11:50