[Android Software/Hacking General [Developers Only]] mikeoswego:
I got the script I was working on down to 2 seconds from the 90 that I had before looping through the packages.xml file.

I am just copying the folder with it's libs from /data because the playstore automatically just places the correct lib file, the other lib files in the apk don't need to be extracted. You are not checking version number of the app installed by the playstore against the version in system (and if you update the version in system from recovery, etc., the version for the old updated-packages entry is never updated so you have to either have aapt to look up the version in /system or have a list created ahead of time statically to work against.

Here is what I have now. The static list of apps that I'm going to process (in your case it would be the gapps list installed, you don't want to process updated CM apps it they start being updated by the playstore. The list I precompile and place in /system/etc/applist is in the form of com.android.whatever"345678, appID"appVersion I used the " as the delimiter just because at the time. I'll change that to a tab but wanted to share what I've done. You are not updating pacakges.xml and maybe I don't need to either since on reboot, the version numbers are updated for the entries I've modifed in packages.xml.

When I'm editing the packages.xml, I blank out the lines for the app installed by the playstore, sed the updated-package lines to just package for the apps we are working with. On the rom I'm working with, this completes in 2 seconds with 200 apps installed and 5 of them updated; it is done before the boot animation stats. Well it's late so I'll just post it:

#!/system/bin/sh
# Hack to clean up apps in data after image upgrade
#
reboot=no
i=0
old_IFS=$IFS # save the field separator
IFS=$'\n' # new field separator, the end of line
while read line # Read a line
do
pkgxmlarray[i]=$line # Put it into the array
i=$(($i + 1))
done < /data/system/packages.xml
IFS=$old_IFS # restore default field separator
pkgxmlline=$i
counter=0
updatedcount=0
updatedpkg=$(printf "%s\n" "${pkgxmlarray[@]}" | grep -n "\<updated-package name" | cut -d \: -f1)
updatedpkgcount=$(printf "%s\n" "${pkgxmlarray[@]}" | grep -c "\<updated-package name")
i=0
while read line # Read a line
do
sysverarray[i]=$line # Put it into the array
i=$(($i + 1))
done < /system/etc/applist
sysline=$i
let tmpline=updatedpkgcount+1
counter=1
while [ $counter -lt $tmpline ]; do
arraypos=$(echo $updatedpkg | cut -d " " -f$counter)
let arraypos=arraypos-1
appname=$(echo "${pkgxmlarray[$arraypos]}" | cut -d \" -f2)
datapkgline=$(printf "%s\n" "${pkgxmlarray[@]}" | grep -n "<package name=\"$appname\"" | cut -d \: -f1)
let datapkgline=datapkgline-1
updatedpkgline=$(printf "%s\n" "${pkgxmlarray[@]}" | grep -n "<updated-package name=\"$appname\"" | cut -d \: -f1)
let updatedpkgline=updatedpkgline-1
datainfo=$(echo ${pkgxmlarray[$datapkgline]})
position=empty
poscounter=1
until [ "$position" == " version=" ]; do
position=$(echo "$datainfo" | cut -d \" -f $poscounter)
if [ "$position" == " version=" ]; then
let poscounter=poscounter+1
dataversion=$(echo "$datainfo" | cut -d \" -f $poscounter)
fi
let poscounter=poscounter+2
done
sysversion=$(printf "%s\n" "${sysverarray[@]}" | grep "$appname\""| cut -d \" -f2)
if [ "$sysversion" == "" ]; then
sysversion=0
fi
if [ $sysversion -ge $dataversion ] ; then
position=empty
poscounter=1
until [ "$position" == " codePath=" ]; do
position=$(echo "$datainfo" | cut -d \" -f $poscounter)
if [ "$position" == " codePath=" ]; then
let poscounter=poscounter+1
datapath=$(echo "$datainfo" | cut -d \" -f $poscounter)
fi
let poscounter=poscounter+2
done
rm -rf $datapath
reboot=yes
packline=no
endpackline=no
foundpack=no
tmpcounter=$datapkgline
done=no
until [ "$done" == "yes" ]; do
packline=$(echo ${pkgxmlarray[$tmpcounter]})
pkgxmlarray[$tmpcounter]=""
let tmpcounter=tmpcounter+1
until [ "$done" == "yes" ]; do
endpackline=$(echo ${pkgxmlarray[$tmpcounter]} | grep "</package")
if [ "$endpackline" == "" ]; then
pkgxmlarray[$tmpcounter]=""
let tmpcounter=tmpcounter+1
else
pkgxmlarray[$tmpcounter]=""
done=yes
fi
done
done
packline=no
endpackline=no
foundpack=no
tmpcounter=$updatedpkgline
done=no
until [ "$done" == "yes" ]; do
packline=$(echo ${pkgxmlarray[$tmpcounter]})
pkgxmlarray[$tmpcounter]=$(echo ${pkgxmlarray[$tmpcounter]} | sed 's/updated-package/package/')
let tmpcounter=tmpcounter+1
until [ "$done" == "yes" ]; do
endpackline=$(echo ${pkgxmlarray[$tmpcounter]} | grep "</updated-package")
if [ "$endpackline" == "" ]; then
let tmpcounter=tmpcounter+1
else
pkgxmlarray[$tmpcounter]==$(echo ${pkgxmlarray[$tmpcounter]} | sed 's/updated-package/package/')
done=yes
fi
done
done
fi
let counter=counter+1
done
if [ "$reboot" == "yes" ]; then
printf "%s\n" "${pkgxmlarray[@]}" | grep -v '^$' > /data/system/packages.xml
#	reboot
fi
#
# End clean up hack