History

Use QHash for _timerFunctionMap instead of std::unordered_map

Ada committed 4 months ago

Commit 42bbbaf1 parent 461e32f2 files · +18 −14Browse files

Showing the changes to libraries/script-engine/src/ScriptManager.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ScriptManager.cpp+17−13
@@ -1171,8 +1171,8 @@ void ScriptManager::run() { · 1170 unmodified lines
1171// NOTE: This is private because it must be called on the same thread that created the timers, which is why1171// NOTE: This is private because it must be called on the same thread that created the timers, which is why
1172// we want to only call it in our own run "shutdown" processing.1172// we want to only call it in our own run "shutdown" processing.
1173void ScriptManager::stopAllTimers() {1173void ScriptManager::stopAllTimers() {
1174 for (const auto& [handle, value] : _timerFunctionMap) {1174 for (const auto& pair : _timerFunctionMap) {
1175 QTimer* timer = std::get<0>(value);1175 QTimer* timer = std::get<0>(pair);
1176 1176
1177 timer->stop();1177 timer->stop();
1178 delete timer;1178 delete timer;
@@ -1183,10 +1183,15 @@ void ScriptManager::stopAllTimers() { · 4 unmodified lines
1183}1183}
1184 1184
1185void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) {1185void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) {
1186 std::vector<int> toDelete;1186 std::vector<int> toDelete = {};
1187 1187
1188 for (const auto& [handle, value] : _timerFunctionMap) {1188 for (
1189 const auto& [timer, callbackData] = value;1189 auto i = _timerFunctionMap.constKeyValueBegin();
1190 i != _timerFunctionMap.constKeyValueEnd();
1191 ++i
1192 ) {
1193 const auto& handle = i->first;
1194 const auto& [timer, callbackData] = i->second;
1190 1195
1191 if (callbackData.definingEntityIdentifier != entityID) { continue; }1196 if (callbackData.definingEntityIdentifier != entityID) { continue; }
1192 1197
@@ -1197,7 +1202,7 @@ void ScriptManager::stopAllTimersForEntityScript(const EntityItemID& entityID) { · 4 unmodified lines
1197 }1202 }
1198 1203
1199 for (auto handle : toDelete) {1204 for (auto handle : toDelete) {
1200 _timerFunctionMap.erase(handle);1205 _timerFunctionMap.remove(handle);
1201 }1206 }
1202 1207
1203 // the timer map is potentially shared across multiple entity scripts,1208 // the timer map is potentially shared across multiple entity scripts,
@@ -1252,11 +1257,11 @@ void ScriptManager::timerFired(int handle) { · 48 unmodified lines
1252 return;1257 return;
1253 }1258 }
1254 1259
1255 auto [timer, data] = _timerFunctionMap.at(handle);1260 auto [timer, data] = _timerFunctionMap.value(handle);
1256 1261
1257 if (!timer->isActive()) {1262 if (!timer->isActive()) {
1258 // this timer is done, we can kill it1263 // this timer is done, we can kill it
1259 _timerFunctionMap.erase(handle);1264 _timerFunctionMap.remove(handle);
1260 delete timer;1265 delete timer;
1261 }1266 }
1262 1267
@@ -1278,8 +1283,7 @@ void ScriptManager::timerFired(int handle) { · 15 unmodified lines
1278}1283}
1279 1284
1280int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int intervalMS, bool isSingleShot) {1285int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int intervalMS, bool isSingleShot) {
1281 int handle = _timerHandleCounter;1286 int handle = _timerHandleCounter++;
1282 _timerHandleCounter += 1;
1283 1287
1284 // create the timer, add it to the map, and start it1288 // create the timer, add it to the map, and start it
1285 QTimer* newTimer = new QTimer(this);1289 QTimer* newTimer = new QTimer(this);
@@ -1297,7 +1301,7 @@ int ScriptManager::setupTimerWithInterval(const ScriptValue& function, int inter · 11 unmodified lines
1297 connect(this, &ScriptManager::scriptEnding, newTimer, &QTimer::stop);1301 connect(this, &ScriptManager::scriptEnding, newTimer, &QTimer::stop);
1298 1302
1299 CallbackData timerData = { function, currentEntityIdentifier, currentSandboxURL };1303 CallbackData timerData = { function, currentEntityIdentifier, currentSandboxURL };
1300 _timerFunctionMap.insert({ handle, { newTimer, timerData } });1304 _timerFunctionMap.insert(handle, { newTimer, timerData });
1301 1305
1302 if (intervalMS < 0) {1306 if (intervalMS < 0) {
1303 int lineNumber = -1;1307 int lineNumber = -1;
@@ -1350,9 +1354,9 @@ int ScriptManager::setTimeout(const ScriptValue& function, int timeoutMS) { · 46 unmodified lines
1350 1354
1351void ScriptManager::stopTimer(int handle) {1355void ScriptManager::stopTimer(int handle) {
1352 if (_timerFunctionMap.contains(handle)) {1356 if (_timerFunctionMap.contains(handle)) {
1353 auto [timer, callbackData] = _timerFunctionMap.at(handle);1357 auto [timer, callbackData] = _timerFunctionMap.value(handle);
1354 timer->stop();1358 timer->stop();
1355 _timerFunctionMap.erase(handle);1359 _timerFunctionMap.remove(handle);
1356 delete timer;1360 delete timer;
1357 } else {1361 } else {
1358 qCDebug(scriptengine) << "stopTimer -- not in _timerFunctionMap" << handle;1362 qCDebug(scriptengine) << "stopTimer -- not in _timerFunctionMap" << handle;