draft/shapestring font-cache, possible to improve?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

draft/shapestring font-cache, possible to improve?

Post by heda »

have lately done some simplistic animations with draft/lines and draft/shapestrings.

first thing one notices is that once any animation includes shapestrings the speed drops a factor 10-100, it is impossible not to notice the drop in speed, and it does not feel proportional to the expected needed computations.

another thing has been that it seemingly randomly bugs out with occ-errors on not creating face

have struggled with this quite a bit, with up until now just lived with it

the only real workaround so far has been to just run it an pray that one do not really notice it in the animation, which happened from time to time, but more often than not the errors just added up to a completely useless animation, as when the rendered string is just complete garbage of non-ascii chars

never really could wrap my head around the errors,
for one gui-updates were troublesome, especially with delays included,
this is regardless that the code is littered with .recompute and .updateGui in desperate attempts to circumvent untimely updates.
that kind of hinted to event que not being flushed, so started to litter the code with
QCoreApplication.processEvents as well, subjectively this seemed to help a bit, but not fixing it.

installed a different version (from 0.20.0 libpack win to 0.20.2 conda win // libpack - the .zip would not unzip...),
and voila, errors where gone :-), for a while before they one again appeared :-(

noticed that the stream of errors looked a bit different,
some error types appeared to not be there any more, the error left was

Code: Select all

/Mod/Draft/draftobjects/shapestring.py, line 67, in execute
    CharList = Part.makeWireString(obj.String,obj.FontFile,obj.Size,obj.Tracking)
<class 'Part.OCCError'>: Font file not found ../tmp/FCT1C08.tmp
took the time to dig into it a bit, since it is weird,
why would it look for a font-file with file-ending .tmp,
especially when the object property has a full path to a real (existing and reachable) .ttf file?
yepp, the font-path is in object, which one would assume avoids any heuristics in finding fonts, while error speaks differently

briefly glanced at the shapestring py code, but could not spot any passages that creates cache-files,
so suppose some c voodoo is needed either in fc-part or occt somewhere
or the cache writing is qt or os driven, that would be a curved ball...

to me it is a mystery where this cache is created...
to complete the story,
the cache-location had accumulated some 40 Gb of duplicated font files
font file is 1.6Mb, so there were close to 30k copies there
that seems a bit unnecessary to do...
if it would not have been for size and timestamp of tmp-file,
probably never would have made the connection that it was copied font files.

yepp, have been running the animation a couple of times, but not 30k times,
one run is maybe 3k steps, but i have for sure ran it more than 10 times,
i.e. nothing obvious sticking out why it would 30k cache copies
as one can see it is also in the root of a temp-folder,
would be nicer if it was in a folder identifying it as a fc-cache...

if a shapestring creation/edit randomly results in a copy of font-file to disk,
that can be a suspect to why animation speed drops, or it drives speed variations


anyhow, for now have hard-coded the font-path in shapestring.py
and it appears to get rid of the errors, while at the same time making it slower...
in the last error free run, it did not put massive amounts of copies in the tmp-location, only one.

clearly this is an illusive story, but the font-copies were real,
could of course have been written by another application, but it is a bit too much of a coincidence imho
so somewhere there is a cache writing triggered under some conditions,
that it is looking in the cache instead of provided path ought to be some peculiar logic somewhere...

an attempt to mvp that may or may not trigger the issue,
it sums up the operations done though.
have not been able to get the errors with this code (yet?),
at least it gives an idea of the slowdown in speed.
maybe one stumbles into an error with longer looping

or the whole thing is a mind ghost and never happened...

since most work happens in c, unclear (to me) why code below hogs down the computer to the extent it does...

Code: Select all

from string import ascii_uppercase
from random import choices

import Draft

def center(doc, obj):
    doc.recompute()
    bb = obj.Shape.BoundBox
    obj.Placement.Base.x = - bb.XLength / 2
    doc.recompute()
    Gui.updateGui()


## '' works on some boxes, others not, probably pref-settings...
## full path will always work, one supposes
font = ''

doc = App.newDocument('stringtest')
obj = Draft.makeShapeString('abc' * 6, font)
center(doc, obj)
obji = Draft.makeShapeString('1', font)
doc.recompute()
obji.Placement.Base.y = obj.Size * 4
center(doc, obji)

Gui.Selection.clearSelection()
Gui.ActiveDocument.ActiveView.fitAll()

for c in ascii_uppercase * 2:
    obji.String = f'{ord(c) - 64}'
    center(doc, obji)
    obj.String = ''.join(choices(ascii_uppercase, k=15))
    center(doc, obj)
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: draft/shapestring font-cache, possible to improve?

Post by wandererfan »

heda wrote: Sun Jan 22, 2023 6:02 am have lately done some simplistic animations with draft/lines and draft/shapestrings.
Are you on Windows? I see some suspect code in Part/App/AppPartPy.cpp, but it is only compiled for Windows.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: draft/shapestring font-cache, possible to improve?

Post by heda »

yepp, this was on win
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: draft/shapestring font-cache, possible to improve?

Post by wandererfan »

heda wrote: Mon Jan 23, 2023 9:24 pm yepp, this was on win
I don't use windows very often. Where do I look for these files?
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: draft/shapestring font-cache, possible to improve?

Post by heda »

c:\user\username\AppData\Local\Temp

if remembering right, plenty of fc-folders there,
but also in other AppData folders, have never (or attempted to) understood the naming/purpose of local/roaming etc...
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: draft/shapestring font-cache, possible to improve?

Post by wandererfan »

heda wrote: Tue Jan 24, 2023 9:04 pm
issue #8271

I can't seem to create the FCT* files and the code that is supposed to delete them does run. There must be something I'm doing differently.

In any case, I have a fix that eliminates the temporary file copy in testing. Works on Win, need to move it to linux and test that it still works there.
GeneFC
Veteran
Posts: 5373
Joined: Sat Mar 19, 2016 3:36 pm
Location: Punta Gorda, FL

Re: draft/shapestring font-cache, possible to improve?

Post by GeneFC »

heda wrote: Tue Jan 24, 2023 9:04 pm have never (or attempted to) understood the naming/purpose of local/roaming etc...
For a single computer not on a domain there is essentially no difference in these appdata locations. If one is connected to a domain, for example inside a company, then the "roaming" folder tree is synced on the server while the "local" folders are not synced.

That allows a user to log in on a different computer that is also connected to the domain and find all the "roaming" stuff readily accessible.

Gene
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: draft/shapestring font-cache, possible to improve?

Post by wandererfan »

git commit c318bc08cb eliminates the creation of the temporary files. Let me know how it works for you.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: draft/shapestring font-cache, possible to improve?

Post by heda »

@wandererfan
awesome to chase that down, will try in the coming weeks, suppose next dev version will have the commit included

@GeneFC thanks, suspected it was something like that (admittedly sometimes feel the less i need to know about win, the happier i am...)

on the slowness thing...
had a homer moment there, duh, for no particular reason i did the animation in shapestrings - and had to suffer with the slowness.
have to blame a momentary lapse of reason...
recently realized that one can also use draft/text, turned out it was much faster, so i'm using that now.
no means of setting an outline then, but for this case it is perfectly fine - the annoying slowness is gone with the wind.

btw

Code: Select all

App.getUserCachePath()
is close to where the font-cache was placed, at least the reported path has fc in it...
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: draft/shapestring font-cache, possible to improve?

Post by heda »

ran animation with FreeCAD-0.21dev-31790-Installer.exe
animation ran as it should,
did not get these random errors in failing to create shapestrings,
also did not find any cached font-files :-)

the change works as expected,
best part is that the subjective speedup was 2x-3x,
@wandererfan well done!!!

now one starts to wonder if there are other places where there is some hard to find cache going on...
if nothing else, maybe someone brave will start monitoring if fc triggers writing to disk when there really is no apparent reason for it...
Post Reply