Minimizing the amount of blocks generated using perlin noise

I’m making a voxel based mining game in which in the system for my game, caves occasionally generate depending on certain conditions. This works with the algorithm that I’m using, however the amount of time it takes to generate a cave is too much, as well as the amount of blocks spawning from the cave is also too much, this is because these caves generate unnecessary corners and blocks that aren’t seen until the player actually mines to them. How could I eliminate these unnecessary blocks entirely? I’ve been trying for a bit but I can’t figure it out.

This is what the code looks like.

local b = {}
local s, incr, length = 0, 0, 1
local noise = false
b[pos]=true
while length>0 do
	--print(b, length)
	for i,v in pairs(b) do
		incr+=1
		for j=1,6 do
			noise = math.noise((i+dir[j]).X/60, (i+dir[j]).Y/60, (i+dir[j]).Z/60) >= 0.4
			if not blocks[i+dir[j]] and not b[i+dir[j]] then
				if noise then
					b[i+dir[j]]=true
					length+=1
				end
				s=0
				for k=1,6 do
					if math.noise((i+dir[j]+dir[k]).X/60, (i+dir[j]+dir[k]).Y/60, (i+dir[j]+dir[k]).Z/60) > 0.4 then
						s+=1
					end
				end
				if s==6 then
					blocks[i+dir[j]]=true
				elseif s>0 then
					blocks[i+dir[j]]=true
					task.spawn(function()
						choose(i+dir[j], plr, id, true)
					end)
				end
			end
		end
		--print(b, i)
		b[i]=nil
		length-=1
		if incr%100==0 then
			task.wait()
		end
	end
end