From f763563834be83ffae397e047191a86fc59703b3 Mon Sep 17 00:00:00 2001 From: Damien Nicolet Date: Fri, 13 Dec 2019 01:02:14 +0100 Subject: [PATCH] Memory optimisation --- apps/code/editor_controller.cpp | 18 +++++++++++++++++- apps/code/editor_controller.h | 4 +++- ion/include/ion/storage.h | 2 +- poincare/include/poincare/tree_pool.h | 2 +- poincare/src/trigonometry_cheat_table.cpp | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index aba355959..c799d8ff8 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -18,6 +18,22 @@ EditorController::EditorController(MenuController * menuController, App * python m_editorView.setTextAreaDelegates(this, this); } +#ifdef MAX_SCRIPTSIZE +void EditorController::setScript(Script script) { + m_script = script; + Script::Data scriptData = m_script.value(); + size_t s=scriptData.size,ms=sizeof(m_areaBuffer); + if (s>ms) + s=ms; + size_t availableScriptSize = s + Ion::Storage::sharedStorage()->availableSize(); + if (availableScriptSize>ms) + availableScriptSize=ms; + // assert(sizeof(m_areaBuffer) >= availableScriptSize); + // We cannot use strlcpy as the first char reprensenting the importation status can be 0. + memcpy(m_areaBuffer, (const char *)scriptData.buffer, s); + m_editorView.setText(m_areaBuffer+1, availableScriptSize-1); // 1 char is taken by the importation status flag +} +#else void EditorController::setScript(Script script) { m_script = script; Script::Data scriptData = m_script.value(); @@ -27,7 +43,7 @@ void EditorController::setScript(Script script) { memcpy(m_areaBuffer, (const char *)scriptData.buffer, scriptData.size); m_editorView.setText(m_areaBuffer+1, availableScriptSize-1); // 1 char is taken by the importation status flag } - +#endif // TODO: this should be done in textAreaDidFinishEditing maybe?? bool EditorController::handleEvent(Ion::Events::Event event) { if (event == Ion::Events::OK || event == Ion::Events::Back || event == Ion::Events::Home) { diff --git a/apps/code/editor_controller.h b/apps/code/editor_controller.h index 284bf1af3..d5b948d4e 100644 --- a/apps/code/editor_controller.h +++ b/apps/code/editor_controller.h @@ -1,6 +1,8 @@ #ifndef CODE_EDITOR_CONTROLLER_H #define CODE_EDITOR_CONTROLLER_H +#define MAX_SCRIPTSIZE 12288 + #include #include "script.h" #include "editor_view.h" @@ -39,7 +41,7 @@ private: /* m_areaBuffer first character is dedicated to the importation status. * Thereby, we avoid wasteful copy while adding the Script to the storage * (in order to add the importation status char before the areaBuffer). */ - char m_areaBuffer[Ion::Storage::k_storageSize]; // this could be slightly optimize + char m_areaBuffer[Ion::Storage::k_storageSize>MAX_SCRIPTSIZE?MAX_SCRIPTSIZE:Ion::Storage::k_storageSize]; // this could be slightly optimize Script m_script; MenuController * m_menuController; }; diff --git a/ion/include/ion/storage.h b/ion/include/ion/storage.h index 41c7001de..5138c1b76 100644 --- a/ion/include/ion/storage.h +++ b/ion/include/ion/storage.h @@ -16,7 +16,7 @@ class StorageDelegate; class Storage { public: typedef uint16_t record_size_t; - constexpr static size_t k_storageSize = 16384; + constexpr static size_t k_storageSize = 20480; static Storage * sharedStorage(); constexpr static char k_dotChar = '.'; diff --git a/poincare/include/poincare/tree_pool.h b/poincare/include/poincare/tree_pool.h index a124964e3..8e6a3d6f4 100644 --- a/poincare/include/poincare/tree_pool.h +++ b/poincare/include/poincare/tree_pool.h @@ -49,7 +49,7 @@ public: int numberOfNodes() const; private: - constexpr static int BufferSize = 32768; + constexpr static int BufferSize = 16384; constexpr static int MaxNumberOfNodes = BufferSize/sizeof(TreeNode); static TreePool * SharedStaticPool; diff --git a/poincare/src/trigonometry_cheat_table.cpp b/poincare/src/trigonometry_cheat_table.cpp index eac4f3a4b..9eae9bc33 100644 --- a/poincare/src/trigonometry_cheat_table.cpp +++ b/poincare/src/trigonometry_cheat_table.cpp @@ -82,7 +82,7 @@ Expression TrigonometryCheatTable::simplify(const Expression e, ExpressionNode:: * For instance, when simplfy a Cosine, we always compute the value for an angle * in the top right trigonometric quadrant. */ const TrigonometryCheatTable * TrigonometryCheatTable::Table() { - static Row sTableRows[] = { + const Row sTableRows[] = { Row(Row::Pair("-90", -90.0f), Row::Pair("π*(-2)^(-1)", -1.5707963267948966f), Row::Pair("-100", -100.0f),