<commit_before><?php
namespace falkirks\minereset\task;

use falkirks\minereset\MineReset;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;


class ResetTask extends AsyncTask{
    /** @var  string */
    private $name;
    /** @var string $chunks */
    private $chunks;
    /** @var Vector3 $a */
    private $a;
    /** @var Vector3 $b */
    private $b;
    /** @var string $ratioData */
    private $ratioData;
    /** @var int $levelId */
    private $levelId;
    /** @var Chunk $chunkClass */
    private $chunkClass;

    public function __construct(string $name, array $chunks, Vector3 $a, Vector3 $b, array $data, $levelId, $chunkClass){
        $this->name = $name;
        $this->chunks = serialize($chunks);
        $this->a = $a;
        $this->b = $b;
        $this->ratioData = serialize($data);
        $this->levelId = $levelId;
        $this->chunkClass = $chunkClass;
    }
    /**
     * Actions to execute when run
     *
     * @return void
     */
    public function onRun(){
        $chunkClass = $this->chunkClass;
        /** @var Chunk[] $chunks */
        $chunks = unserialize($this->chunks);
        foreach($chunks as $hash => $binary){
            $chunks[$hash] = $chunkClass::fastDeserialize($binary);
        }
        $sum = [];
        $id = array_keys(unserialize($this->ratioData));

        foreach($id as $i => $blockId){
            $blockId = explode(":", $id[$i]);
            if(!isset($blockId[1])){
                $blockId[1] = 0;
            }
            $id[$i] = $blockId;
        }
        $m = array_values(unserialize($this->ratioData));
        $sum[0] = $m[0];
        for ($l = 1, $mCount = count($m); $l < $mCount; $l++)
            $sum[$l] = $sum[$l - 1] + $m[$l];

        $sumCount = count($sum);

        $totalBlocks = ($this->b->x - $this->a->x + 1)*($this->b->y - $this->a->y + 1)*($this->b->z - $this->a->z + 1);
        $interval = $totalBlocks / 8; //TODO determine the interval programmatically
        $lastUpdate = 0;
        $currentBlocks = 0;

        $currentChunkX = $this->a->x >> 4;
        $currentChunkZ = $this->a->z >> 4;

        $currentChunkY = $this->a->y >> 4;

        $currentChunk = null;
        $currentSubChunk = null;

        for ($x = $this->a->getX(), $x2 = $this->b->getX(); $x <= $x2; $x++) {
            $chunkX = $x >> 4;
            for ($z = $this->a->getZ(), $z2 = $this->b->getZ(); $z <= $z2; $z++) {
                $chunkZ = $z >> 4;
                if($currentChunk === null or $chunkX !== $currentChunkX or $chunkZ !== $currentChunkZ){
                    $currentChunkX = $chunkX;
                    $currentChunkZ = $chunkZ;

                    $hash = Level::chunkHash($chunkX, $chunkZ);
                    $currentChunk = $chunks[$hash];
                    if($currentChunk === null){
                        continue;
                    }
                }

                for ($y = $this->a->getY(), $y2 = $this->b->getY(); $y <= $y2; $y++) {
                    $chunkY = $y >> 4;
<commit_msg>Fixed minor performance loss due to accessing serialized vectors repeatedly pthreads serializes objects assigned as members of Threaded objects, so every time you access it you're creating a new object and incurring the overhead of unserializing it again for reading. Store these in local variables to avoid the serialization penalty.<commit_after><?php
namespace falkirks\minereset\task;

use falkirks\minereset\MineReset;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;


class ResetTask extends AsyncTask{
    /** @var  string */
    private $name;
    /** @var string $chunks */
    private $chunks;
    /** @var Vector3 $a */
    private $a;
    /** @var Vector3 $b */
    private $b;
    /** @var string $ratioData */
    private $ratioData;
    /** @var int $levelId */
    private $levelId;
    /** @var Chunk $chunkClass */
    private $chunkClass;

    public function __construct(string $name, array $chunks, Vector3 $a, Vector3 $b, array $data, $levelId, $chunkClass){
        $this->name = $name;
        $this->chunks = serialize($chunks);
        $this->a = $a;
        $this->b = $b;
        $this->ratioData = serialize($data);
        $this->levelId = $levelId;
        $this->chunkClass = $chunkClass;
    }
    /**
     * Actions to execute when run
     *
     * @return void
     */
    public function onRun(){
        $chunkClass = $this->chunkClass;
        /** @var Chunk[] $chunks */
        $chunks = unserialize($this->chunks);
        foreach($chunks as $hash => $binary){
            $chunks[$hash] = $chunkClass::fastDeserialize($binary);
        }
        $sum = [];
        $id = array_keys(unserialize($this->ratioData));

        foreach($id as $i => $blockId){
            $blockId = explode(":", $id[$i]);
            if(!isset($blockId[1])){
                $blockId[1] = 0;
            }
            $id[$i] = $blockId;
        }
        $m = array_values(unserialize($this->ratioData));
        $sum[0] = $m[0];
        for ($l = 1, $mCount = count($m); $l < $mCount; $l++)
            $sum[$l] = $sum[$l - 1] + $m[$l];

        $sumCount = count($sum);

        //Get these as local variables so they don't keep getting serialized/unserialized every single access
        $posA = $this->a;
        $posB = $this->b;

        $totalBlocks = ($posB->x - $posA->x + 1)*($posB->y - $posA->y + 1)*($posB->z - $posA->z + 1);
        $interval = $totalBlocks / 8; //TODO determine the interval programmatically
        $lastUpdate = 0;
        $currentBlocks = 0;

        $currentChunkX = $posA->x >> 4;
        $currentChunkZ = $posA->z >> 4;

        $currentChunkY = $posA->y >> 4;

        $currentChunk = null;
        $currentSubChunk = null;

        for ($x = $posA->getX(), $x2 = $posB->getX(); $x <= $x2; $x++) {
            $chunkX = $x >> 4;
            for ($z = $posA->getZ(), $z2 = $posB->getZ(); $z <= $z2; $z++) {
                $chunkZ = $z >> 4;
                if($currentChunk === null or $chunkX !== $currentChunkX or $chunkZ !== $currentChunkZ){
                    $currentChunkX = $chunkX;
                    $currentChunkZ = $chunkZ;

                    $hash = Level::chunkHash($chunkX, $chunkZ);
                    $currentChunk = $chunks[$hash];
                    if($currentChunk === null){
                        continue;
                    }
                }

                for ($y = $posA->getY(), $y2 = $posB->getY(); $y <= $y2; $y++) {
                    $chunkY = $y >> 4;

{'fraction_non_alphanumeric': 0.17327547508603922, 'fraction_numerical': 0.008978003890468352, 'mean_word_length': 1.9536014140521432, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3820558', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2106, 'n_words': 513}