Panel speed / cache?

gaining performance does not work that way. you have to delay the creation of the pages collection to inside the call to lapse. so if there is no cache or it is expired the collection will be created anew.

<?php

class AutorizacionesPage extends Kirby\Cms\Page {

	public function historial(): Kirby\Cms\Pages
	{
	    
	    // speed up the time to fetch ids from an complex pages tree
		$data = Bnomei\Lapse::io(
		    'AutorizacionesHistorial',
		    function () {
			    $collectionForIds = pages('afiliados')
		            ->grandChildren()
		            ->filterBy('intendedTemplate', 'in', ['autorizacion', 'consulta']);

		             return [
		        	'historiales' => $collectionForIds, 
		    	     ];
	    	        }
			, 1 // expire in minutes. adjust to your needs.
		);

		// what exactly did this code save you?
		// probably not much.
		// your gain in performance mostly depends on how 
		// many files have be skipped when using the cache.

		// kirby is lazy loading data required when crawling
		// the file tree. it is NOT reading the content files that
		// do not match your template filter.
		// if you would do more complex stuff or modified checks
		// you would gain more.

		return $data['historiales'];
	}
}

i think lapse should be doing fine with serialization of the pages object but i have not tested that recently but i will.

if it does not work consider just storing the ids and rebuilding the collection yourself.

<?php

class AutorizacionesPage extends Kirby\Cms\Page {

	public function historial(): Kirby\Cms\Pages
	{
		$data = Bnomei\Lapse::io(
		    'AutorizacionesHistorial',
		    function () {
			    $collectionForIds = pages('afiliados')
		            ->grandChildren()
		            ->filterBy('intendedTemplate', 'in', ['autorizacion', 'consulta']);

		        return [
		        	'historiales' => $collectionForIds->keys(), // for pages: key == id 
		    	];
	    	}
			, 1 // expire in minutes. adjust to your needs.
		);
		
		// new pages collection
		$collection = new Pages(
			array_map(function($id) {
				return page($id); // get page foreach id
			}, $data['historiales'])
		);

		return $collection;
	}
}