If you lift the match statement out of the UDF you might be able to improve performance.  Actually, we probably should look at this optimization in other places as well.