diff options
author | Hein-Pieter van Braam <hp@tmm.cx> | 2018-07-25 03:11:03 +0200 |
---|---|---|
committer | Hein-Pieter van Braam <hp@tmm.cx> | 2018-07-26 00:54:16 +0200 |
commit | 0e29f7974b59e4440cf02e1388fb9d8ab2b5c5fd (patch) | |
tree | 18b7ff35f1eeee39031a16e9c1d834ebf03d44cf /scene/2d | |
parent | 9423f23ffb80c946dec380f73f3f313ec44d0d18 (diff) |
Reduce unnecessary COW on Vector by make writing explicit
This commit makes operator[] on Vector const and adds a write proxy to it. From
now on writes to Vectors need to happen through the .write proxy. So for
instance:
Vector<int> vec;
vec.push_back(10);
std::cout << vec[0] << std::endl;
vec.write[0] = 20;
Failing to use the .write proxy will cause a compilation error.
In addition COWable datatypes can now embed a CowData pointer to their data.
This means that String, CharString, and VMap no longer use or derive from
Vector.
_ALWAYS_INLINE_ and _FORCE_INLINE_ are now equivalent for debug and non-debug
builds. This is a lot faster for Vector in the editor and while running tests.
The reason why this difference used to exist is because force-inlined methods
used to give a bad debugging experience. After extensive testing with modern
compilers this is no longer the case.
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/animated_sprite.cpp | 2 | ||||
-rw-r--r-- | scene/2d/animated_sprite.h | 2 | ||||
-rw-r--r-- | scene/2d/collision_object_2d.cpp | 2 | ||||
-rw-r--r-- | scene/2d/collision_polygon_2d.cpp | 4 | ||||
-rw-r--r-- | scene/2d/line_2d.cpp | 2 | ||||
-rw-r--r-- | scene/2d/navigation2d.cpp | 40 | ||||
-rw-r--r-- | scene/2d/navigation_polygon.cpp | 10 | ||||
-rw-r--r-- | scene/2d/physics_body_2d.cpp | 8 | ||||
-rw-r--r-- | scene/2d/polygon_2d.cpp | 24 | ||||
-rw-r--r-- | scene/2d/skeleton_2d.cpp | 12 | ||||
-rw-r--r-- | scene/2d/tile_map.cpp | 4 |
11 files changed, 55 insertions, 55 deletions
diff --git a/scene/2d/animated_sprite.cpp b/scene/2d/animated_sprite.cpp index b56eedabc7..85e7f8df92 100644 --- a/scene/2d/animated_sprite.cpp +++ b/scene/2d/animated_sprite.cpp @@ -235,7 +235,7 @@ void SpriteFrames::_set_frames(const Array &p_frames) { E->get().frames.resize(p_frames.size()); for (int i = 0; i < E->get().frames.size(); i++) - E->get().frames[i] = p_frames[i]; + E->get().frames.write[i] = p_frames[i]; } Array SpriteFrames::_get_frames() const { diff --git a/scene/2d/animated_sprite.h b/scene/2d/animated_sprite.h index f6586aff36..cc49465403 100644 --- a/scene/2d/animated_sprite.h +++ b/scene/2d/animated_sprite.h @@ -113,7 +113,7 @@ public: ERR_FAIL_COND(p_idx < 0); if (p_idx >= E->get().frames.size()) return; - E->get().frames[p_idx] = p_frame; + E->get().frames.write[p_idx] = p_frame; } void remove_frame(const StringName &p_anim, int p_idx); void clear(const StringName &p_anim); diff --git a/scene/2d/collision_object_2d.cpp b/scene/2d/collision_object_2d.cpp index cabd7fddc2..1e2184bd41 100644 --- a/scene/2d/collision_object_2d.cpp +++ b/scene/2d/collision_object_2d.cpp @@ -259,7 +259,7 @@ void CollisionObject2D::shape_owner_remove_shape(uint32_t p_owner, int p_shape) for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) { for (int i = 0; i < E->get().shapes.size(); i++) { if (E->get().shapes[i].index > index_to_remove) { - E->get().shapes[i].index -= 1; + E->get().shapes.write[i].index -= 1; } } } diff --git a/scene/2d/collision_polygon_2d.cpp b/scene/2d/collision_polygon_2d.cpp index 9d2a83fda7..9f19f56e75 100644 --- a/scene/2d/collision_polygon_2d.cpp +++ b/scene/2d/collision_polygon_2d.cpp @@ -102,11 +102,11 @@ Vector<Vector<Vector2> > CollisionPolygon2D::_decompose_in_convex() { TriangulatorPoly &tp = I->get(); - decomp[idx].resize(tp.GetNumPoints()); + decomp.write[idx].resize(tp.GetNumPoints()); for (int i = 0; i < tp.GetNumPoints(); i++) { - decomp[idx][i] = tp.GetPoint(i); + decomp.write[idx].write[i] = tp.GetPoint(i); } idx++; diff --git a/scene/2d/line_2d.cpp b/scene/2d/line_2d.cpp index e9e895b5bb..ad9775c0b7 100644 --- a/scene/2d/line_2d.cpp +++ b/scene/2d/line_2d.cpp @@ -239,7 +239,7 @@ void Line2D::_draw() { { PoolVector<Vector2>::Read points_read = _points.read(); for (int i = 0; i < len; ++i) { - points[i] = points_read[i]; + points.write[i] = points_read[i]; } } diff --git a/scene/2d/navigation2d.cpp b/scene/2d/navigation2d.cpp index 16e0386952..e3b048fd74 100644 --- a/scene/2d/navigation2d.cpp +++ b/scene/2d/navigation2d.cpp @@ -74,7 +74,7 @@ void Navigation2D::_navpoly_link(int p_id) { Vector2 ep = nm.xform.xform(r[idx]); center += ep; e.point = _get_point(ep); - p.edges[j] = e; + p.edges.write[j] = e; int idxn = indices[(j + 1) % plen]; if (idxn < 0 || idxn >= len) { @@ -119,17 +119,17 @@ void Navigation2D::_navpoly_link(int p_id) { ConnectionPending pending; pending.polygon = &p; pending.edge = j; - p.edges[j].P = C->get().pending.push_back(pending); + p.edges.write[j].P = C->get().pending.push_back(pending); continue; //print_line(String()+_get_vertex(ek.a)+" -> "+_get_vertex(ek.b)); } C->get().B = &p; C->get().B_edge = j; - C->get().A->edges[C->get().A_edge].C = &p; - C->get().A->edges[C->get().A_edge].C_edge = j; - p.edges[j].C = C->get().A; - p.edges[j].C_edge = C->get().A_edge; + C->get().A->edges.write[C->get().A_edge].C = &p; + C->get().A->edges.write[C->get().A_edge].C_edge = j; + p.edges.write[j].C = C->get().A; + p.edges.write[j].C_edge = C->get().A_edge; //connection successful. } } @@ -167,10 +167,10 @@ void Navigation2D::_navpoly_unlink(int p_id) { } else if (C->get().B) { //disconnect - C->get().B->edges[C->get().B_edge].C = NULL; - C->get().B->edges[C->get().B_edge].C_edge = -1; - C->get().A->edges[C->get().A_edge].C = NULL; - C->get().A->edges[C->get().A_edge].C_edge = -1; + C->get().B->edges.write[C->get().B_edge].C = NULL; + C->get().B->edges.write[C->get().B_edge].C_edge = -1; + C->get().A->edges.write[C->get().A_edge].C = NULL; + C->get().A->edges.write[C->get().A_edge].C_edge = -1; if (C->get().A == &E->get()) { @@ -187,11 +187,11 @@ void Navigation2D::_navpoly_unlink(int p_id) { C->get().B = cp.polygon; C->get().B_edge = cp.edge; - C->get().A->edges[C->get().A_edge].C = cp.polygon; - C->get().A->edges[C->get().A_edge].C_edge = cp.edge; - cp.polygon->edges[cp.edge].C = C->get().A; - cp.polygon->edges[cp.edge].C_edge = C->get().A_edge; - cp.polygon->edges[cp.edge].P = NULL; + C->get().A->edges.write[C->get().A_edge].C = cp.polygon; + C->get().A->edges.write[C->get().A_edge].C_edge = cp.edge; + cp.polygon->edges.write[cp.edge].C = C->get().A; + cp.polygon->edges.write[cp.edge].C_edge = C->get().A_edge; + cp.polygon->edges.write[cp.edge].P = NULL; } } else { @@ -339,8 +339,8 @@ Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vect Vector<Vector2> path; path.resize(2); - path[0] = begin_point; - path[1] = end_point; + path.write[0] = begin_point; + path.write[1] = end_point; //print_line("Direct Path"); return path; } @@ -408,7 +408,7 @@ Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vect for (int i = 0; i < es; i++) { - Polygon::Edge &e = p->edges[i]; + Polygon::Edge &e = p->edges.write[i]; if (!e.C) continue; @@ -586,7 +586,7 @@ Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vect if (!path.size() || path[path.size() - 1].distance_squared_to(begin_point) > CMP_EPSILON) { path.push_back(begin_point); // Add the begin point } else { - path[path.size() - 1] = begin_point; // Replace first midpoint by the exact begin point + path.write[path.size() - 1] = begin_point; // Replace first midpoint by the exact begin point } path.invert(); @@ -594,7 +594,7 @@ Vector<Vector2> Navigation2D::get_simple_path(const Vector2 &p_start, const Vect if (path.size() <= 1 || path[path.size() - 1].distance_squared_to(end_point) > CMP_EPSILON) { path.push_back(end_point); // Add the end point } else { - path[path.size() - 1] = end_point; // Replace last midpoint by the exact end point + path.write[path.size() - 1] = end_point; // Replace last midpoint by the exact end point } return path; diff --git a/scene/2d/navigation_polygon.cpp b/scene/2d/navigation_polygon.cpp index 6e27bf1c1d..2d6679272a 100644 --- a/scene/2d/navigation_polygon.cpp +++ b/scene/2d/navigation_polygon.cpp @@ -91,7 +91,7 @@ void NavigationPolygon::_set_polygons(const Array &p_array) { polygons.resize(p_array.size()); for (int i = 0; i < p_array.size(); i++) { - polygons[i].indices = p_array[i]; + polygons.write[i].indices = p_array[i]; } } @@ -110,7 +110,7 @@ void NavigationPolygon::_set_outlines(const Array &p_array) { outlines.resize(p_array.size()); for (int i = 0; i < p_array.size(); i++) { - outlines[i] = p_array[i]; + outlines.write[i] = p_array[i]; } rect_cache_dirty = true; } @@ -166,7 +166,7 @@ int NavigationPolygon::get_outline_count() const { void NavigationPolygon::set_outline(int p_idx, const PoolVector<Vector2> &p_outline) { ERR_FAIL_INDEX(p_idx, outlines.size()); - outlines[p_idx] = p_outline; + outlines.write[p_idx] = p_outline; rect_cache_dirty = true; } @@ -432,8 +432,8 @@ void NavigationPolygonInstance::_notification(int p_what) { { PoolVector<Vector2>::Read vr = verts.read(); for (int i = 0; i < vsize; i++) { - vertices[i] = vr[i]; - colors[i] = color; + vertices.write[i] = vr[i]; + colors.write[i] = color; } } diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 7505aa4a94..9879f43877 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -1373,11 +1373,11 @@ Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) { } if (slide_colliders[p_bounce].is_null()) { - slide_colliders[p_bounce].instance(); - slide_colliders[p_bounce]->owner = this; + slide_colliders.write[p_bounce].instance(); + slide_colliders.write[p_bounce]->owner = this; } - slide_colliders[p_bounce]->collision = colliders[p_bounce]; + slide_colliders.write[p_bounce]->collision = colliders[p_bounce]; return slide_colliders[p_bounce]; } @@ -1475,7 +1475,7 @@ KinematicBody2D::~KinematicBody2D() { for (int i = 0; i < slide_colliders.size(); i++) { if (slide_colliders[i].is_valid()) { - slide_colliders[i]->owner = NULL; + slide_colliders.write[i]->owner = NULL; } } } diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index 81ed3c63c3..c9e5408f06 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -113,7 +113,7 @@ void Polygon2D::_notification(int p_what) { PoolVector<Vector2>::Read polyr = polygon.read(); for (int i = 0; i < len; i++) { - points[i] = polyr[i] + offset; + points.write[i] = polyr[i] + offset; } } @@ -153,18 +153,18 @@ void Polygon2D::_notification(int p_what) { SWAP(ep[1], ep[4]); SWAP(ep[2], ep[3]); SWAP(ep[5], ep[0]); - SWAP(ep[6], points[highest_idx]); + SWAP(ep[6], points.write[highest_idx]); } points.resize(points.size() + 7); for (int i = points.size() - 1; i >= highest_idx + 7; i--) { - points[i] = points[i - 7]; + points.write[i] = points[i - 7]; } for (int i = 0; i < 7; i++) { - points[highest_idx + i + 1] = ep[i]; + points.write[highest_idx + i + 1] = ep[i]; } len = points.size(); @@ -182,12 +182,12 @@ void Polygon2D::_notification(int p_what) { PoolVector<Vector2>::Read uvr = uv.read(); for (int i = 0; i < len; i++) { - uvs[i] = texmat.xform(uvr[i]) / tex_size; + uvs.write[i] = texmat.xform(uvr[i]) / tex_size; } } else { for (int i = 0; i < len; i++) { - uvs[i] = texmat.xform(points[i]) / tex_size; + uvs.write[i] = texmat.xform(points[i]) / tex_size; } } } @@ -262,10 +262,10 @@ void Polygon2D::_notification(int p_what) { { PoolVector<Color>::Read color_r = vertex_colors.read(); for (int i = 0; i < color_len && i < len; i++) { - colors[i] = color_r[i]; + colors.write[i] = color_r[i]; } for (int i = color_len; i < len; i++) { - colors[i] = color; + colors.write[i] = color; } } @@ -333,13 +333,13 @@ void Polygon2D::_notification(int p_what) { Vector<Vector2> vertices; vertices.resize(loop.size()); for (int j = 0; j < vertices.size(); j++) { - vertices[j] = points[loop[j]]; + vertices.write[j] = points[loop[j]]; } Vector<int> sub_indices = Geometry::triangulate_polygon(vertices); int from = indices.size(); indices.resize(from + sub_indices.size()); for (int j = 0; j < sub_indices.size(); j++) { - indices[from + j] = loop[sub_indices[j]]; + indices.write[from + j] = loop[sub_indices[j]]; } } @@ -538,12 +538,12 @@ void Polygon2D::clear_bones() { void Polygon2D::set_bone_weights(int p_index, const PoolVector<float> &p_weights) { ERR_FAIL_INDEX(p_index, bone_weights.size()); - bone_weights[p_index].weights = p_weights; + bone_weights.write[p_index].weights = p_weights; update(); } void Polygon2D::set_bone_path(int p_index, const NodePath &p_path) { ERR_FAIL_INDEX(p_index, bone_weights.size()); - bone_weights[p_index].path = p_path; + bone_weights.write[p_index].path = p_path; update(); } diff --git a/scene/2d/skeleton_2d.cpp b/scene/2d/skeleton_2d.cpp index 8ceffb3c27..e6e9bde20a 100644 --- a/scene/2d/skeleton_2d.cpp +++ b/scene/2d/skeleton_2d.cpp @@ -191,13 +191,13 @@ void Skeleton2D::_update_bone_setup() { bones.sort(); //sorty so they are always in the same order/index for (int i = 0; i < bones.size(); i++) { - bones[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose - bones[i].bone->skeleton_index = i; + bones.write[i].rest_inverse = bones[i].bone->get_skeleton_rest().affine_inverse(); //bind pose + bones.write[i].bone->skeleton_index = i; Bone2D *parent_bone = Object::cast_to<Bone2D>(bones[i].bone->get_parent()); if (parent_bone) { - bones[i].parent_index = parent_bone->skeleton_index; + bones.write[i].parent_index = parent_bone->skeleton_index; } else { - bones[i].parent_index = -1; + bones.write[i].parent_index = -1; } } @@ -230,9 +230,9 @@ void Skeleton2D::_update_transform() { ERR_CONTINUE(bones[i].parent_index >= i); if (bones[i].parent_index >= 0) { - bones[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform(); + bones.write[i].accum_transform = bones[bones[i].parent_index].accum_transform * bones[i].bone->get_transform(); } else { - bones[i].accum_transform = bones[i].bone->get_transform(); + bones.write[i].accum_transform = bones[i].bone->get_transform(); } } diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index 72c3ed7425..95fd4fff2c 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -529,8 +529,8 @@ void TileMap::update_dirty_quadrants() { { PoolVector<Vector2>::Read vr = navigation_polygon_vertices.read(); for (int i = 0; i < vsize; i++) { - vertices[i] = vr[i]; - colors[i] = debug_navigation_color; + vertices.write[i] = vr[i]; + colors.write[i] = debug_navigation_color; } } |