Fixed High CPU Usage. setPosition now uses prepared statements.

This commit is contained in:
Michael Yoo 2013-07-15 21:44:47 +09:00
parent 08000257fb
commit 6d4465eb67
2 changed files with 17 additions and 2 deletions

View File

@ -128,6 +128,7 @@ class PocketMinecraftServer{
public function startDatabase(){
$this->preparedSQL = new stdClass();
$this->preparedSQL->entity = new stdClass();
$this->database = new SQLite3(":memory:");
$this->query("PRAGMA journal_mode = OFF;");
$this->query("PRAGMA encoding = \"UTF-8\";");
@ -143,6 +144,8 @@ class PocketMinecraftServer{
$this->preparedSQL->selectHandlers = $this->database->prepare("SELECT DISTINCT ID FROM handlers WHERE name = :name ORDER BY priority DESC;");
$this->preparedSQL->selectActions = $this->database->prepare("SELECT ID,code,repeat FROM actions WHERE last <= (:time - interval);");
$this->preparedSQL->updateAction = $this->database->prepare("UPDATE actions SET last = :time WHERE ID = :id;");
$this->preparedSQL->entity->setPosition = $this->database->prepare("UPDATE entities SET x = :x, y = :y, z = :z, pitch = :pitch, yaw = :yaw WHERE EID = :eid ;");
$this->preparedSQL->entity->setLevel = $this->database->prepare("UPDATE entities SET level = :level WHERE EID = :eid ;");
}
public function query($sql, $fetch = false){

View File

@ -752,7 +752,11 @@ class Entity extends Position{
public function setPosition(Vector3 $pos, $yaw = false, $pitch = false){
if($pos instanceof Position){
$this->level = $pos->level;
$this->server->query("UPDATE entities SET level = '".$this->level->getName()."' WHERE EID = ".$this->eid.";");
$this->server->preparedSQL->entity->setLevel->reset();
$this->server->preparedSQL->entity->setLevel->clear();
$this->server->preparedSQL->entity->setLevel->bindValue(":level", $this->level->getName(), SQLITE3_TEXT);
$this->server->preparedSQL->entity->setLevel->bindValue(":eid", $this->eid, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setLevel->execute();
}
$this->x = $pos->x;
$this->y = $pos->y;
@ -763,7 +767,15 @@ class Entity extends Position{
if($pitch !== false){
$this->pitch = $pitch;
}
$this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
$this->server->preparedSQL->entity->setPosition->reset();
$this->server->preparedSQL->entity->setPosition->clear();
$this->server->preparedSQL->entity->setPosition->bindValue(":x", $this->x, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->bindValue(":y", $this->y, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->bindValue(":z", $this->z, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->bindValue(":pitch", $this->pitch, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->bindValue(":yaw", $this->yaw, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_TEXT);
$this->server->preparedSQL->entity->setPosition->execute();
}
public function inBlock(Vector3 $block, $radius = 0.8){