I'm sure there was an article in one of the comics recently about use heat/cold to impart a choke on the barrel.
There is no simple formula for the barrel length to give maximum muzzle energy for a given setup. The length required will depend on the pellet design and weight, the dimensions and specification of the piston and spring, the stroke length, the transfer port size etc. There are computer based time stepping simulations which will give a guess but I would not trust them to be sufficiently accurate to start chopping up barrels based on their predictions.
All I would say is the amount of energy lost by having too long a barrel does not seem to be very large. Simulations I have carried out suggest an optimum barrel length of around 16 inches which is not surprising as the simulation was set up to give that value based on measured data. If you have a short stroke with a strong spring the length can come down to 13 inches. This is not to say you cannot get the energy with different barrel lengths and stroke lengths, it is just that you will not be at the most efficient combination in terms of muzzle energy.
It's just a guess but I'd expect it will differ between guns due to transfer port size & flow.
It will be the relation between the volume of the bore per inch & the swept volume of the cylinder but the flow through the port will surely govern the rate of pressure build up behind the pellet and therefore the rate of acceleration.
The optimum length being the point where the pellet reaches the required velocity, and that in turn will depend on the mass & friction of the pellet.
Presumably a factory barrel length has to be a compromise between a heavy pellet not reaching max velocity & a light pellet being so fast the ME exceeds the limit.