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

Reserve space for LocalVector if size is known #100269

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3691,6 +3691,7 @@ void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *
glDepthFunc(GL_GREATER);

TightLocalVector<GLenum> draw_buffers;
draw_buffers.reserve(4);
draw_buffers.push_back(GL_COLOR_ATTACHMENT0);
draw_buffers.push_back(GL_COLOR_ATTACHMENT1);
draw_buffers.push_back(GL_COLOR_ATTACHMENT2);
Expand Down
1 change: 1 addition & 0 deletions drivers/gles3/shader_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ bool ShaderGLES3::_load_from_cache(Version *p_version) {
ERR_FAIL_COND_V_MSG(cache_variant_count != variant_count, false, "shader cache variant count mismatch, expected " + itos(variant_count) + " got " + itos(cache_variant_count)); //should not happen but check

LocalVector<OAHashMap<uint64_t, Version::Specialization>> variants;
variants.reserve(cache_variant_count);
for (int i = 0; i < cache_variant_count; i++) {
uint32_t cache_specialization_count = f->get_32();
OAHashMap<uint64_t, Version::Specialization> variant;
Expand Down
2 changes: 2 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2848,6 +2848,7 @@ Error DisplayServerWindows::dialog_show(String p_title, String p_description, Ve
Char16String title = p_title.utf16();
Char16String message = p_description.utf16();
LocalVector<Char16String> buttons;
buttons.reserve(p_buttons.size());
for (String s : p_buttons) {
buttons.push_back(s.utf16());
}
Expand Down Expand Up @@ -6662,6 +6663,7 @@ void DisplayServerWindows::register_windows_driver() {

DisplayServerWindows::~DisplayServerWindows() {
LocalVector<List<FileDialogData *>::Element *> to_remove;
to_remove.reserve(file_dialogs.size());
for (List<FileDialogData *>::Element *E = file_dialogs.front(); E; E = E->next()) {
FileDialogData *fd = E->get();
if (fd->listener_thread.is_started()) {
Expand Down
3 changes: 3 additions & 0 deletions scene/3d/lightmap_gi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa

LocalVector<Vector3> new_probe_positions;
HashMap<Vector3i, bool> positions_used;
new_probe_positions.reserve(8);
for (uint32_t i = 0; i < 8; i++) { //insert bounding endpoints
Vector3i pos;
if (i & 1) {
Expand Down Expand Up @@ -1259,6 +1260,8 @@ LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_pa
LocalVector<Plane> bsp_planes;
LocalVector<int32_t> bsp_simplex_indices;
PackedInt32Array tetrahedrons;
bsp_simplices.reserve(solved_simplices.size());
bsp_simplex_indices.reserve(solved_simplices.size());

for (int i = 0; i < solved_simplices.size(); i++) {
//Prepare a special representation of the simplex, which uses a BSP Tree
Expand Down
2 changes: 2 additions & 0 deletions scene/3d/retarget_modifier_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ void RetargetModifier3D::_retarget_global_pose() {
}

LocalVector<Transform3D> source_poses;
source_poses.reserve(source_bone_ids.size());
if (influence < 1.0) {
for (int source_bone_id : source_bone_ids) {
source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_global_rest(source_bone_id).interpolate_with(source_skeleton->get_bone_global_pose(source_bone_id), influence));
Expand Down Expand Up @@ -308,6 +309,7 @@ void RetargetModifier3D::_retarget_pose() {
}

LocalVector<Transform3D> source_poses;
source_poses.reserve(source_bone_ids.size());
if (influence < 1.0) {
for (int source_bone_id : source_bone_ids) {
source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_rest(source_bone_id).interpolate_with(source_skeleton->get_bone_pose(source_bone_id), influence));
Expand Down
2 changes: 2 additions & 0 deletions scene/3d/skeleton_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,11 +1163,13 @@ void Skeleton3D::_process_modifiers() {
real_t influence = mod->get_influence();
if (influence < 1.0) {
LocalVector<Transform3D> old_poses;
old_poses.reserve(get_bone_count());
for (int i = 0; i < get_bone_count(); i++) {
old_poses.push_back(get_bone_pose(i));
}
mod->process_modification();
LocalVector<Transform3D> new_poses;
new_poses.reserve(get_bone_count());
for (int i = 0; i < get_bone_count(); i++) {
new_poses.push_back(get_bone_pose(i));
}
Expand Down
1 change: 1 addition & 0 deletions scene/property_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const Nod
}

if (inheritance_states.size() > 0) {
r_states_stack.reserve(r_states_stack.size() + inheritance_states.size());
for (int i = inheritance_states.size() - 1; i >= 0; --i) {
r_states_stack.push_back(inheritance_states[i]);
}
Expand Down
1 change: 1 addition & 0 deletions scene/resources/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,7 @@ Error ArrayMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, flo

//create surfacetools for each surface..
LocalVector<Ref<SurfaceTool>> surfaces_tools;
surfaces_tools.reserve(lightmap_surfaces.size());

for (int i = 0; i < lightmap_surfaces.size(); i++) {
Ref<SurfaceTool> st;
Expand Down
1 change: 1 addition & 0 deletions scene/resources/surface_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const
vertex_array.push_back(v);
}

index_array.reserve(index_array.size() + nindices.size());
for (const int &index : nindices) {
int dst_index = index + vfrom;
index_array.push_back(dst_index);
Expand Down
8 changes: 8 additions & 0 deletions servers/rendering/rendering_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,7 @@ RDD::RenderPassID RenderingDevice::_render_pass_create(RenderingDeviceDriver *p_

LocalVector<RDD::Attachment> attachments;
LocalVector<int> attachment_remap;
attachment_remap.reserve(p_attachments.size());

for (int i = 0; i < p_attachments.size(); i++) {
if (p_attachments[i].usage_flags == AttachmentFormat::UNUSED_ATTACHMENT) {
Expand Down Expand Up @@ -2229,6 +2230,9 @@ RDD::RenderPassID RenderingDevice::_render_pass_create(RenderingDeviceDriver *p_
for (int i = 0; i < p_passes.size(); i++) {
const FramebufferPass *pass = &p_passes[i];
RDD::Subpass &subpass = subpasses[i];
subpass.color_references.reserve(pass->color_attachments.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the subpass lists are empty at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the whole subpasses is freshly defined just 7 lines above.

subpass.input_references.reserve(pass->input_attachments.size());
subpass.resolve_references.reserve(pass->resolve_attachments.size());

TextureSamples texture_samples = TEXTURE_SAMPLES_1;
bool is_multisample_first = true;
Expand Down Expand Up @@ -2415,6 +2419,8 @@ RenderingDevice::FramebufferFormatID RenderingDevice::framebuffer_format_create_
Vector<TextureSamples> samples;
LocalVector<RDD::AttachmentLoadOp> load_ops;
LocalVector<RDD::AttachmentStoreOp> store_ops;
load_ops.reserve(p_attachments.size());
store_ops.reserve(p_attachments.size());
for (int64_t i = 0; i < p_attachments.size(); i++) {
load_ops.push_back(RDD::ATTACHMENT_LOAD_OP_CLEAR);
store_ops.push_back(RDD::ATTACHMENT_STORE_OP_STORE);
Expand Down Expand Up @@ -2554,6 +2560,7 @@ RID RenderingDevice::framebuffer_create_multipass(const Vector<RID> &p_texture_a
Vector<AttachmentFormat> attachments;
LocalVector<RDD::TextureID> textures;
LocalVector<RDG::ResourceTracker *> trackers;
trackers.reserve(p_texture_attachments.size());
attachments.resize(p_texture_attachments.size());
Size2i size;
bool size_set = false;
Expand Down Expand Up @@ -3181,6 +3188,7 @@ RID RenderingDevice::uniform_set_create(const Collection &p_uniforms, RID p_shad
RDD::BoundUniform &driver_uniform = driver_uniforms[i];
driver_uniform.type = uniform.uniform_type;
driver_uniform.binding = uniform.binding;
driver_uniform.ids.reserve(uniform.get_id_count());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the ids are empty at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole driver_uniforms is defined just above this loop and resized, so all these items are just default constructed, unless you do some weird stuff in constructor it should be empty.


// Mark immutable samplers to be skipped when creating uniform set.
driver_uniform.immutable_sampler = uniform.immutable_sampler;
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/shader_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ String ShaderPreprocessor::vector_to_string(const LocalVector<char32_t> &p_v, in

String ShaderPreprocessor::tokens_to_string(const LocalVector<Token> &p_tokens) {
LocalVector<char32_t> result;
result.reserve(p_tokens.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be better served with an in-place String construction; though I'm not sure whether the token.text can be trusted to be correct UTF-32.

for (const Token &token : p_tokens) {
result.push_back(token.text);
}
Expand Down