Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All] Optimize insertion time using where possible reserve() #1869

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "codec_def_encode_hevc.h"
#include "mos_utilities.h"

#define RESERVE(num) data.reserve(num)
#define NUM_ELEM_ARRAY(member) (sizeof(obj.member) / sizeof(obj.member[0]))
#define PUSH(member) data.push_back(obj.member)
#define PUSH64(member) \
Expand All @@ -37,21 +38,25 @@
data.push_back(p[1]); \
}
#define PUSH_ARRAY(member) \
RESERVE(NUM_ELEM_ARRAY(member)); \
for (size_t i0 = 0; i0 < NUM_ELEM_ARRAY(member); i0++) \
{ \
PUSH(member[i0]); \
}
#define PUSH_ARRAY2(member) \
RESERVE(NUM_ELEM_ARRAY(member)); \
for (size_t i1 = 0; i1 < NUM_ELEM_ARRAY(member); i1++) \
{ \
PUSH_ARRAY(member[i1]); \
}
#define PUSH_ARRAY3(member) \
RESERVE(NUM_ELEM_ARRAY(member)); \
for (size_t i2 = 0; i2 < NUM_ELEM_ARRAY(member); i2++) \
{ \
PUSH_ARRAY2(member[i2]); \
}
#define PUSH_SEQ(member, num) \
RESERVE(num); \
for (size_t i = 0; i < num; i++) \
{ \
PUSH(member[i]); \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ VPHAL_VEBOX_STATE_XE_HPM::VPHAL_VEBOX_STATE_XE_HPM(
veboxMaxPipeNum = gtSystemInfo->MaxVECS;
}

m_veCmdBuffers.reserve(veboxMaxPipeNum);
for (i = 0; i < veboxMaxPipeNum; i++)
{
PMOS_COMMAND_BUFFER pCmdBuffer = (PMOS_COMMAND_BUFFER)MOS_AllocAndZeroMemory(sizeof(MOS_COMMAND_BUFFER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ VPHAL_VEBOX_STATE_XE_XPM::VPHAL_VEBOX_STATE_XE_XPM(
veboxMaxPipeNum = gtSystemInfo->MaxVECS;
}

m_veCmdBuffers.reserve(veboxMaxPipeNum);
for (i = 0; i < veboxMaxPipeNum; i++)
{
PMOS_COMMAND_BUFFER pCmdBuffer = (PMOS_COMMAND_BUFFER)MOS_AllocAndZeroMemory(sizeof(MOS_COMMAND_BUFFER));
Expand Down
1 change: 1 addition & 0 deletions media_driver/agnostic/common/cm/cm_hal_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ struct CM_HAL_GENERIC
//! always return MOS_STATUS_SUCCESS
virtual MOS_STATUS AddSupportedCisaIDs(uint32_t *cisaGenIDs, int len = 1)
{
m_cisaGenIDs.reserve(len);
for (int i = 0; i < len; i++)
{
m_cisaGenIDs.push_back(cisaGenIDs[i]);
Expand Down
1 change: 1 addition & 0 deletions media_driver/agnostic/common/cm/cm_kernel_rt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,7 @@ int32_t CmKernelRT::SetArgsInternal( CM_KERNEL_INTERNAL_ARG_TYPE nArgType, uint3
{
size = numSamplers * sizeof(unsigned int);

sampler_index_array.reserve(numSamplers);
for (unsigned int i = 0; i < numSamplers; i++)
{
SamplerIndex* samplerIndex = (SamplerIndex*)value + i;
Expand Down
14 changes: 12 additions & 2 deletions media_driver/agnostic/common/cm/cm_visa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ ISAfile::ISAfile(const ISAfile& other) {
errorIndex = other.errorIndex;
header = new Header(other.version);
*header = *other.header;
kernel_data.reserve(other.kernel_data.size());
for (KernelBody *kb : other.kernel_data) {
KernelBody *kb2 = new KernelBody(other.version);
*kb2 = *kb;
kernel_data.push_back(kb2);
}
function_data.reserve(other.function_data.size());
for (FunctionBody *fb : other.function_data) {
FunctionBody *fb2 = new FunctionBody(other.version);
*fb2 = *fb;
Expand All @@ -79,8 +81,10 @@ ISAfile& ISAfile::operator= (const ISAfile& other) {
delete kb;
for (FunctionBody *fb : function_data)
delete fb;
kernel_data.reserve(other.kernel_data.size());
for (KernelBody *kb : other.kernel_data)
kernel_data.push_back(&*kb);
function_data.reserve(other.function_data.size());
for (FunctionBody *fb : other.function_data)
function_data.push_back(&*fb);
}
Expand Down Expand Up @@ -255,6 +259,7 @@ bool ISAfile::writeToFile(const char *filename, std::vector<uint8_t> &originalBu
setError("Error writing GEN binary into ISA file, bad offset from original file", 0);
return false;
}
buffer.reserve(g->getBinarySize());
for (uint32_t b = 0; b < g->getBinarySize(); b++) {
buffer.push_back(static_cast<char>(originalBuffer[offset + b]));
}
Expand All @@ -270,28 +275,33 @@ void ISAfile::addToBuffer(Field &field, std::vector<char> &buffer) {
switch (field.type) {
case Datatype::ONE: buffer.push_back(field.ui8[0]); break;
case Datatype::TWO: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]); break;
case Datatype::FOUR: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
case Datatype::FOUR: buffer.reserve(4);
buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
buffer.push_back(field.ui8[2]); buffer.push_back(field.ui8[3]); break;
case Datatype::EIGHT: buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
case Datatype::EIGHT: buffer.reserve(8);
buffer.push_back(field.ui8[0]); buffer.push_back(field.ui8[1]);
buffer.push_back(field.ui8[2]); buffer.push_back(field.ui8[3]);
buffer.push_back(field.ui8[4]); buffer.push_back(field.ui8[5]);
buffer.push_back(field.ui8[6]); buffer.push_back(field.ui8[7]); break;
case Datatype::VARCHAR:
{
buffer.reserve(field.size);
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.varchar[i]));
}
break;
}
case Datatype::VARCHAR_POOL:
{
buffer.reserve(field.size);
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.varchar[i]));
}
break;
}
case Datatype::GDATA:
{
buffer.reserve(field.size);
for (unsigned i = 0; i < field.size; i++) {
buffer.push_back(static_cast<char>(field.gdata[i]));
}
Expand Down
18 changes: 18 additions & 0 deletions media_driver/agnostic/common/cm/cm_visa.h
Original file line number Diff line number Diff line change
Expand Up @@ -2386,11 +2386,13 @@ namespace vISA {
//!
Function(const Function& other) {
fields = other.fields;
variable_reloc_symtab.reserve(other.variable_reloc_symtab.size());
for (RelocationInfo *r : other.variable_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
variable_reloc_symtab.push_back(s);
}
function_reloc_symtab.reserve(other.function_reloc_symtab.size());
for (RelocationInfo *r : other.function_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
Expand All @@ -2408,13 +2410,15 @@ namespace vISA {
fields = other.fields;
for (RelocationInfo *r : variable_reloc_symtab)
delete r;
variable_reloc_symtab.reserve(other.variable_reloc_symtab.size());
for (RelocationInfo *r : other.variable_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
variable_reloc_symtab.push_back(s);
}
for (RelocationInfo *r : function_reloc_symtab)
delete r;
function_reloc_symtab.reserve(other.function_reloc_symtab.size());
for (RelocationInfo *r : other.function_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
Expand Down Expand Up @@ -2763,6 +2767,7 @@ namespace vISA {
//!
GlobalVariable(const GlobalVariable& other) {
fields = other.fields;
attribute_info.reserve(other.attribute_info.size());
for (AttributeInfo *r : other.attribute_info) {
AttributeInfo *s = new AttributeInfo();
*s = *r;
Expand All @@ -2780,6 +2785,7 @@ namespace vISA {
fields = other.fields;
for (AttributeInfo *r : attribute_info)
delete r;
attribute_info.reserve(other.attribute_info.size());
for (AttributeInfo *r : other.attribute_info) {
AttributeInfo *s = new AttributeInfo();
*s = *r;
Expand Down Expand Up @@ -4623,16 +4629,19 @@ namespace vISA {
//!
Kernel(const Kernel& other) {
fields = other.fields;
variable_reloc_symtab.reserve(other.variable_reloc_symtab.size());
for (RelocationInfo *r : other.variable_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
variable_reloc_symtab.push_back(s);
}
function_reloc_symtab.reserve(other.function_reloc_symtab.size());
for (RelocationInfo *r : other.function_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
function_reloc_symtab.push_back(s);
}
gen_binary_info.reserve(other.gen_binary_info.size());
for (GenBinary *r : other.gen_binary_info) {
GenBinary *s = new GenBinary();
*s = *r;
Expand All @@ -4650,20 +4659,23 @@ namespace vISA {
fields = other.fields;
for (RelocationInfo *r : variable_reloc_symtab)
delete r;
variable_reloc_symtab.reserve(other.variable_reloc_symtab.size());
for (RelocationInfo *r : other.variable_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
variable_reloc_symtab.push_back(s);
}
for (RelocationInfo *r : function_reloc_symtab)
delete r;
function_reloc_symtab.reserve(other.function_reloc_symtab.size());
for (RelocationInfo *r : other.function_reloc_symtab) {
RelocationInfo *s = new RelocationInfo();
*s = *r;
function_reloc_symtab.push_back(s);
}
for (GenBinary *r : gen_binary_info)
delete r;
gen_binary_info.reserve(other.gen_binary_info.size());
for (GenBinary *r : other.gen_binary_info) {
GenBinary *s = new GenBinary();
*s = *r;
Expand Down Expand Up @@ -5071,16 +5083,19 @@ namespace vISA {
//!
Header(const Header& other) {
fields = other.fields;
kernel_info.reserve(other.kernel_info.size());
for (Kernel *r : other.kernel_info) {
Kernel *s = new Kernel();
*s = *r;
kernel_info.push_back(s);
}
file_scope_var_info.reserve(other.file_scope_var_info.size());
for (GlobalVariable *r : other.file_scope_var_info) {
GlobalVariable *s = new GlobalVariable();
*s = *r;
file_scope_var_info.push_back(s);
}
function_info.reserve(other.function_info.size());
for (Function *r : other.function_info) {
Function *s = new Function();
*s = *r;
Expand All @@ -5098,20 +5113,23 @@ namespace vISA {
fields = other.fields;
for (Kernel *r : kernel_info)
delete r;
kernel_info.reserve(other.kernel_info.size());
for (Kernel *r : other.kernel_info) {
Kernel *s = new Kernel();
*s = *r;
kernel_info.push_back(s);
}
for (GlobalVariable *r : file_scope_var_info)
delete r;
file_scope_var_info.reserve(other.file_scope_var_info.size());
for (GlobalVariable *r : other.file_scope_var_info) {
GlobalVariable *s = new GlobalVariable();
*s = *r;
file_scope_var_info.push_back(s);
}
for (Function *r : function_info)
delete r;
function_info.reserve(other.function_info.size());
for (Function *r : other.function_info) {
Function *s = new Function();
*s = *r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5732,9 +5732,10 @@ class QuadTreeNode

void CreateCUs()
{
uint32_t size = m_size / 2;
uint32_t level = m_level + 1;
const uint32_t size = m_size / 2;
const uint32_t level = m_level + 1;

m_childBlocks.reserve(4);
m_childBlocks.emplace_back(m_ctb, m_x, m_y, level, m_ctbLog2Size);
m_childBlocks.emplace_back(m_ctb, m_x + size, m_y, level, m_ctbLog2Size);
m_childBlocks.emplace_back(m_ctb, m_x, m_y + size, level, m_ctbLog2Size);
Expand Down
1 change: 1 addition & 0 deletions media_driver/agnostic/gen9/cm/cm_hal_g9.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct CM_HAL_G9_X:public CM_HAL_GENERIC
void OverwriteSteppingTable(const char **newTable, int len)
{
m_steppingTable.clear();
m_steppingTable.reserve(len);
for (int i = 0; i < len; i ++)
{
m_steppingTable.push_back(newTable[i]);
Expand Down
6 changes: 4 additions & 2 deletions media_driver/linux/common/ddi/media_libva_caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef MediaLibvaCapsFactory<MediaLibvaCaps, DDI_MEDIA_CONTEXT> CapsFactory;
#endif

#include "set"
#include "unordered_set"

#ifndef VA_ENCRYPTION_TYPE_NONE
#define VA_ENCRYPTION_TYPE_NONE 0x00000000
Expand Down Expand Up @@ -2345,14 +2346,15 @@ VAStatus MediaLibvaCaps::QueryConfigProfiles(
{
DDI_CHK_NULL(profileList, "Null pointer", VA_STATUS_ERROR_INVALID_PARAMETER);
DDI_CHK_NULL(numProfiles, "Null pointer", VA_STATUS_ERROR_INVALID_PARAMETER);
std::set<int32_t> profiles;
std::unordered_set<int32_t> profiles;
int32_t i;
profiles.reserve(m_profileEntryCount);
for (i = 0; i < m_profileEntryCount; i++)
{
profiles.insert((int32_t)m_profileEntryTbl[i].m_profile);
}

std::set<int32_t>::iterator it;
std::unordered_set<int32_t>::iterator it;
for (it = profiles.begin(), i = 0; it != profiles.end(); ++it, i++)
{
profileList[i] = (VAProfile)*it;
Expand Down
1 change: 1 addition & 0 deletions media_driver/linux/ult/ult_app/ddi_test_caps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int Test_QueryConfigProfiles(VADriverContextP ctx, vector<FeatureID> &queriedFea
}
else
{
queriedFeatureIDTable.reserve(num_entrypoint);
for (int j = 0; j < num_entrypoint; j++)
{
queriedFeatureIDTable.push_back({profile, entrypoints[j]});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ MOS_STATUS Av1DownSamplingFeatureXe2_Lpm_Base::GetRefFrameList(std::vector<uint3
}

refFrameList.clear();
refFrameList.reserve(refFrameIndexList.size());
for (uint32_t frameIdx : refFrameIndexList)
{
refFrameList.push_back(frameIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ MOS_STATUS Av1DownSamplingFeatureXe2_Hpm::GetRefFrameList(std::vector<uint32_t>
}

refFrameList.clear();
refFrameList.reserve(refFrameIndexList.size());
for (uint32_t frameIdx : refFrameIndexList)
{
refFrameList.push_back(frameIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ MOS_STATUS AvcDownSamplingFeature::GetRefFrameList(std::vector<uint32_t> &refFra
avcBasicFeature->m_refFrames.GetActiveReferenceList(*avcBasicFeature->m_avcPicParams);

refFrameList.clear();

refFrameList.reserve(activeRefList.size());
for (uint8_t frameIdx : activeRefList)
{
refFrameList.push_back(frameIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class RefrenceAssociatedBuffer
DECODE_ASSERT(m_availableBuffers.empty());
DECODE_ASSERT(m_activeBuffers.empty());

m_availableBuffers.reserve(initialAllocNum);
for (uint32_t i = 0; i < initialAllocNum; i++)
{
BufferType *buffer = m_bufferOp.Allocate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace decode
DECODE_FUNC_CALL();

m_activeReferenceList.clear();
m_activeReferenceList.reserve(CODECHAL_MAX_CUR_NUM_REF_FRAME_VP9);
for (auto i = 0; i < CODECHAL_MAX_CUR_NUM_REF_FRAME_VP9; i++)
{
m_activeReferenceList.push_back(picParams.RefFrameList[i].FrameIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ std::vector<PMOS_SURFACE> Av1ReferenceFrames::GetEncRefSurface() const

auto idxList = GetRefScalingIdx();
std::vector<PMOS_SURFACE> ret;

ret.reserve(idxList.size());
for (auto idx : idxList)
{
ret.push_back(m_basicFeature->m_trackedBuf->GetSurface(m_encRefBufType, idx));
Expand All @@ -552,7 +552,7 @@ std::vector<PMOS_SURFACE> Av1ReferenceFrames::GetEnc4xRefSurface() const
{
auto idxList = GetRefScalingIdx();
std::vector<PMOS_SURFACE> ret;

ret.reserve(idxList.size());
for (auto idx : idxList)
{
ret.push_back(m_basicFeature->m_trackedBuf->GetSurface(m_enc4xRefBufType, idx));
Expand All @@ -565,7 +565,7 @@ std::vector<PMOS_SURFACE> Av1ReferenceFrames::GetEnc8xRefSurface() const
{
auto idxList = GetRefScalingIdx();
std::vector<PMOS_SURFACE> ret;

ret.reserve(idxList.size());
for (auto idx : idxList)
{
ret.push_back(m_basicFeature->m_trackedBuf->GetSurface(m_enc8xRefBufType, idx));
Expand Down
Loading