From 0e29f7974b59e4440cf02e1388fb9d8ab2b5c5fd Mon Sep 17 00:00:00 2001 From: Hein-Pieter van Braam Date: Wed, 25 Jul 2018 03:11:03 +0200 Subject: 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 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. --- core/undo_redo.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'core/undo_redo.cpp') diff --git a/core/undo_redo.cpp b/core/undo_redo.cpp index b9a2fdd0ac..3d90608dd7 100644 --- a/core/undo_redo.cpp +++ b/core/undo_redo.cpp @@ -39,7 +39,7 @@ void UndoRedo::_discard_redo() { for (int i = current_action + 1; i < actions.size(); i++) { - for (List::Element *E = actions[i].do_ops.front(); E; E = E->next()) { + for (List::Element *E = actions.write[i].do_ops.front(); E; E = E->next()) { if (E->get().type == Operation::TYPE_REFERENCE) { @@ -70,7 +70,7 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { if (p_mode == MERGE_ENDS) { // Clear all do ops from last action, and delete all object references - List::Element *E = actions[current_action + 1].do_ops.front(); + List::Element *E = actions.write[current_action + 1].do_ops.front(); while (E) { @@ -83,11 +83,11 @@ void UndoRedo::create_action(const String &p_name, MergeMode p_mode) { } E = E->next(); - actions[current_action + 1].do_ops.pop_front(); + actions.write[current_action + 1].do_ops.pop_front(); } } - actions[actions.size() - 1].last_tick = ticks; + actions.write[actions.size() - 1].last_tick = ticks; merge_mode = p_mode; @@ -122,7 +122,7 @@ void UndoRedo::add_do_method(Object *p_object, const String &p_method, VARIANT_A for (int i = 0; i < VARIANT_ARG_MAX; i++) { do_op.args[i] = *argptr[i]; } - actions[current_action + 1].do_ops.push_back(do_op); + actions.write[current_action + 1].do_ops.push_back(do_op); } void UndoRedo::add_undo_method(Object *p_object, const String &p_method, VARIANT_ARG_DECLARE) { @@ -147,7 +147,7 @@ void UndoRedo::add_undo_method(Object *p_object, const String &p_method, VARIANT for (int i = 0; i < VARIANT_ARG_MAX; i++) { undo_op.args[i] = *argptr[i]; } - actions[current_action + 1].undo_ops.push_back(undo_op); + actions.write[current_action + 1].undo_ops.push_back(undo_op); } void UndoRedo::add_do_property(Object *p_object, const String &p_property, const Variant &p_value) { @@ -162,7 +162,7 @@ void UndoRedo::add_do_property(Object *p_object, const String &p_property, const do_op.type = Operation::TYPE_PROPERTY; do_op.name = p_property; do_op.args[0] = p_value; - actions[current_action + 1].do_ops.push_back(do_op); + actions.write[current_action + 1].do_ops.push_back(do_op); } void UndoRedo::add_undo_property(Object *p_object, const String &p_property, const Variant &p_value) { @@ -182,7 +182,7 @@ void UndoRedo::add_undo_property(Object *p_object, const String &p_property, con undo_op.type = Operation::TYPE_PROPERTY; undo_op.name = p_property; undo_op.args[0] = p_value; - actions[current_action + 1].undo_ops.push_back(undo_op); + actions.write[current_action + 1].undo_ops.push_back(undo_op); } void UndoRedo::add_do_reference(Object *p_object) { @@ -195,7 +195,7 @@ void UndoRedo::add_do_reference(Object *p_object) { do_op.resref = Ref(Object::cast_to(p_object)); do_op.type = Operation::TYPE_REFERENCE; - actions[current_action + 1].do_ops.push_back(do_op); + actions.write[current_action + 1].do_ops.push_back(do_op); } void UndoRedo::add_undo_reference(Object *p_object) { @@ -213,7 +213,7 @@ void UndoRedo::add_undo_reference(Object *p_object) { undo_op.resref = Ref(Object::cast_to(p_object)); undo_op.type = Operation::TYPE_REFERENCE; - actions[current_action + 1].undo_ops.push_back(undo_op); + actions.write[current_action + 1].undo_ops.push_back(undo_op); } void UndoRedo::_pop_history_tail() { @@ -223,7 +223,7 @@ void UndoRedo::_pop_history_tail() { if (!actions.size()) return; - for (List::Element *E = actions[0].undo_ops.front(); E; E = E->next()) { + for (List::Element *E = actions.write[0].undo_ops.front(); E; E = E->next()) { if (E->get().type == Operation::TYPE_REFERENCE) { @@ -307,7 +307,7 @@ bool UndoRedo::redo() { return false; //nothing to redo current_action++; - _process_operation_list(actions[current_action].do_ops.front()); + _process_operation_list(actions.write[current_action].do_ops.front()); version++; return true; @@ -318,7 +318,7 @@ bool UndoRedo::undo() { ERR_FAIL_COND_V(action_level > 0, false); if (current_action < 0) return false; //nothing to redo - _process_operation_list(actions[current_action].undo_ops.front()); + _process_operation_list(actions.write[current_action].undo_ops.front()); current_action--; version--; -- cgit v1.2.3