tikz bounding box / cropping: too much space for curves

Here is my attempt to get an automatic method. Read this page to know how to split Bézier curves.

I define the new limit bb style with two arguments:

  1. the maximum distance between actual bounding box and perfect bounding box.
  2. the action (draw, fill...) applied to the path.

This new style splits automatically and recursively all Bézier curves to remove too distant control points.

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{calc,decorations.pathreplacing}
\tikzset{
  bezier/controls/.code args={(#1) and (#2)}{
    \def\mystartcontrol{#1}
    \def\mytargetcontrol{#2}
  },
  bezier/limit/.store in=\mylimit,
  bezier/limit=1cm,
  bezier/.code={
    \tikzset{bezier/.cd,#1}
    \tikzset{
      to path={
        let
        \p0=(\tikztostart),    \p1=(\mystartcontrol),
        \p2=(\mytargetcontrol), \p3=(\tikztotarget),
        \n0={veclen(\x1-\x0,\y1-\y0)},
        \n1={veclen(\x3-\x2,\y3-\y2)},
        \n2={\mylimit}
        in  \pgfextra{
          \pgfmathtruncatemacro\ok{max((\n0>\n2),(\n1>\n2))}
        }
        \ifnum\ok=1 %
        let
        \p{01}=($(\p0)!.5!(\p1)$), \p{12}=($(\p1)!.5!(\p2)$), \p{23}=($(\p2)!.5!(\p3)$),
        \p{0112}=($(\p{01})!.5!(\p{12})$), \p{1223}=($(\p{12})!.5!(\p{23})$),
        \p{01121223}=($(\p{0112})!.5!(\p{1223})$)
        in
        to[bezier={controls={(\p{01}) and (\p{0112})}}]
        (\p{01121223})
        to[bezier={controls={(\p{1223}) and (\p{23})}}]
        (\p3)
        \else
        [overlay=false] .. controls (\p1) and (\p2) ..  (\p3) [overlay=true]
        \fi
      },
    }%, <-- Comma here results in "Missing character: There is no , in font nullfont!"
  },
  limit bb/.style n args={2}{
    overlay,
    decorate,
    decoration={
      show path construction,
      moveto code={},
      lineto code={\path[#2] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);},
      curveto code={
        \path[#2]
        (\tikzinputsegmentfirst)
        to[bezier={limit=#1,controls={(\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)}}]
        (\tikzinputsegmentlast);
      },
      closepath code={\path[#2] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);},
    },
  },
  limit bb/.default={1mm}{draw},
}

\begin{document}
\begin{tikzpicture}
  \node[draw,circle] (A) at (0,0){A};
  \node[draw,circle] (B) at (3,3){B};
  \draw[limit bb={1mm}{draw=red},bend left=90,looseness=2] (A) to (B);
  \draw[limit bb={1mm}{draw=blue},bend right=90,looseness=2] (A) to (B);
  \draw[green] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}

By default, part of the bounding box comes from

\def\pgf@lt@moveto#1#2{%
  \pgf@protocolsizes{#1}{#2}%
  \pgfsyssoftpath@moveto{\the#1}{\the#2}%
}
\def\pgf@lt@lineto#1#2{%
  \pgf@protocolsizes{#1}{#2}%
  \pgfsyssoftpath@lineto{\the#1}{\the#2}%
}
\def\pgf@lt@curveto#1#2#3#4#5#6{%
  \pgf@protocolsizes{#1}{#2}%
  \pgf@protocolsizes{#3}{#4}%
  \pgf@protocolsizes{#5}{#6}%
  \pgfsyssoftpath@curveto{\the#1}{\the#2}{\the#3}{\the#4}{\the#5}{\the#6}%
}

That is the reason we saw that all control points are involved: control points are directly passed to bounding-box-calculation (\pgf@protocolsizes). To solve this, one can only do the math inside \pgf@lt@curveto. This topic is definitely a duplicate if you count programming languages other than TeX. For instance An algorithm to find bounding box of closed bezier curves? in stack overflow.

But in TeX it is hard to do math. But still it is possible to sacrifice some efficiency to get a fairly acceptable result. For example: since 3(1-t)t^2,3(1-t)^2t≤4/9, we know

xmax ≤ max(xA,xD)+4/9|xB-max(xA,xD)|+4/9|xC-max(xA,xD)|

so the right hand side could improve the calculation.