As a debugging tool you could try Data::Structure::Util, which at the very least will ensure you're not accidentally creating circular references.
Otherwise I would continue down the path of input validation and improving your algorithms. Fiddling with XS is unlikely to help you here and sounds hackish.