Bitbake overrides optimisation in the datastore
I mentioned recently that the override "cache" data in the bitbake data store has some potential to be optimised. The data I'm referring to is "self.overridedata" in data_smart.py. To explain what it does, some same data helps:
RDEPENDS = "A"
RDEPENDS:${PN} = "B"
RDEPENDS:${PN}:class-target = "C"
RDEPENDS:${PN}:qemumips:class-native = "D"
Some code might call:
getVar("RDEPENDS")
-or-getVar("RDEPENDS:${PN}")
and this data cache exists so that bitbake doesn't have to iterate the datastore for all keys, checking for variables starting with the expression.
For the above, currently bitbake will store data against each of the combinations, the code is in _setvar_update_overrides(). Taking, "RDEPENDS:${PN}:qemumips:class-native", it would place a cache entry against:
RDEPENDS:${PN}:qemumips:class-native
RDEPENDS:${PN}:qemumips
RDEPENDS:${PN}
RDEPENDS
since any of those being requested by getVar needs to return a value. It would cache the needed overrides against these, they're stored as strings. Effectively this means d.getVar("RDEPENDS") can be redirected to d.getVar("RDEPENDS:${PN}:qemumips:class-native").
To work out what d.getVar("RDEPENDS") should return it goes through a few steps:
a) see what possibilities are "active"
This means for RDEPENDS:${PN}:qemumips:class-native it would check for set('${PN}', 'qemumips', 'class-native') is a subset of set(current OVERRIDES). The actual code:
if flag == "_content" and not parsing and overridedata is not None:
match = False
active = {}
self.need_overrides()
for (r, o) in overridedata:
# FIXME What about double overrides both with "_" in the name? if o in self.overridesset:
active[o] = r
elif ":" in o:
if set(o.split(":")).issubset(self.overridesset):
active[o] = r
b) for the active possibilities, work through in OVERRIDES order, chopping off the right hand part each step. The actual code:
mod = True
while mod:
mod = False
for o in self.overrides:
for a in active.copy():
if a.endswith(":" + o):
t = active[a]
del active[a] active[a.replace(":" + o, "")] = t mod = True
elif a == o:
match = active[a]
del active[a]
The code in question was designed for "_" as the override character, which may or may not have indicated an override. We now can know what is and isn't an override so I think we can improve things.
So far, my feeling is that we can optimise things by:
a) Only caching the entries that would apply to the initial part of the variable, i.e. have one cache entry for RDEPENDS rather than all the combinations.
b) Have a pre split version of the strings so we can avoid the split/join operations.
Challenges:
* sets don't hold order and part of our code is order sensitive
* we do want sets for comparison with the currently active overrides
* sets/lists can't be used in the current dictionary overridesdata structure as they're mutable (could use a tuple)
So at some points we need strings, sometimes lists, sometimes sets and we need to "chop off" sections from the left/right sides of the expression.
I'm open to ideas on how we could/should be handling things.
Cheers,
Richard
Quoted 97 lines · Richard Purdie
I mentioned recently that the override "cache" data in the bitbake data store has some potential to be optimised. The data I'm referring to is "self.overridedata" in
data_smart.py. To explain what it does, some same data helps:RDEPENDS = "A"
RDEPENDS:${PN} = "B"
RDEPENDS:${PN}:class-target = "C"
RDEPENDS:${PN}:qemumips:class-native = "D"Some code might call:
getVar("RDEPENDS")
-or-getVar("RDEPENDS:${PN}")and this data cache exists so that bitbake doesn't have to iterate the datastore for all keys, checking for variables starting with the expression.
For the above, currently bitbake will store data against each of the combinations, the code is in
_setvar_update_overrides(). Taking, "RDEPENDS:${PN}:qemumips:class-native", it would place a cache entry against:RDEPENDS:${PN}:qemumips:class-native
RDEPENDS:${PN}:qemumips
RDEPENDS:${PN}
RDEPENDSsince any of those being requested by getVar needs to return a value. It would cache the needed overrides against these, they're stored as strings. Effectively this means
d.getVar("RDEPENDS")can be redirected tod.getVar("RDEPENDS:${PN}:qemumips:class-native").To work out what
d.getVar("RDEPENDS")should return it goes through a few steps:a) see what possibilities are "active"
This means for RDEPENDS:${PN}:qemumips:class-native it would check for
set('${PN}', 'qemumips', 'class-native')is a subset of set(current OVERRIDES). The actual code:if flag == "
_content" and not parsing and overridedata is not None:
match = False
active = {}
self.need_overrides()
for (r, o) in overridedata:
# FIXME What about double overrides both with "_" in the name? if o in self.overridesset:
active[o] = r
elif ":" in o:
if set(o.split(":")).issubset(self.overridesset):
active[o] = rb) for the active possibilities, work through in OVERRIDES order, chopping off the right hand part each step. The actual code:
mod = True
while mod:
mod = False
for o in self.overrides:
for a inactive.copy():
ifa.endswith(":" + o):
t = active[a]
del active[a] active[a.replace(":" + o, "")] = t mod = True
elif a == o:
match = active[a]
del active[a]The code in question was designed for "_" as the override character, which may or may not have indicated an override. We now can know what is and isn't an override so I think we can improve things.
So far, my feeling is that we can optimise things by:
a) Only caching the entries that would apply to the initial part of the variable, i.e. have one cache entry for RDEPENDS rather than all the combinations.
b) Have a pre split version of the strings so we can avoid the split/join operations.
Challenges:
* sets don't hold order and part of our code is order sensitive
* we do want sets for comparison with the currently active overrides
* sets/lists can't be used in the current dictionary overridesdata structure as they're mutable (could use a tuple)
Quoted 5 lines
So at some points we need strings, sometimes lists, sometimes sets and we need to "chop off" sections from the left/right sides of the expression.
I'm open to ideas on how we could/should be handling things.
Ok.... I think I understand this now (maybe...). My gut instinct is to use a tree based approach here; basically use the requested variable name (e.g. "RDEPENDS:${PN}") to find the starting node in the tree, then recurse down the branches, efficiently culling out ones that don't apply and accounting for priority as we go.
I've written a little demonstration script in
https://git.yoctoproject.org/poky-contrib/log/?h=jpew/override-cache
to sort of give the idea; maybe look it over (especially the test cases at the bottom!) to see if it makes sense.
Quoted 4 lines
Cheers,
Richard
Hi Joshua, all,
Quoted 6 lines
Ok.... I think I understand this now (maybe...). My gut instinct is to use a tree based approach here; basically use the requested variable name (e.g. "RDEPENDS:${PN}") to find the starting node in the tree, then recurse down the branches, efficiently culling out ones that don't apply and accounting for priority as we go.
Quoted 11 lines
I've written a little demonstration script in
https://git.yoc/
toproject.org%2Fpoky-contrib%2Flog%2F%3Fh%3Djpew%2Foverride- cache&data=05%7C01%7Cchris.laplante%40agilent.com%7C093557944d2849 8e646008daff2b6637%7Ca9c0bc098b46420693512ba12fb4a5c0%7C0%7C0%7 C638102859137208260%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C% 7C&sdata=dpmfZo0rf5r4bjuxiKTvi34jAqXXoMT%2FErOPGM8S%2Bn8%3D&res erved=0
to sort of give the idea; maybe look it over (especially the test cases at the bottom!) to see if it makes sense.
I find this idea very compelling.
Has anyone put any work into implementing the datastore itself as some kind of graph/tree? It "feels" like it should be possible.
I think it would be most similar to the task of implementing a spreadsheet formula evaluation engine. Spreadsheets deal with expanding arbitrarily complex formulas. The expansions can involve non-deterministic procedures (such as getting the current date/time). Seems highly applicable to DataSmart.
My testbed would probably be bytebraise (https://github.com/agilent/bytebraise). But as it says on the tin, the goal of that project is just so that I can better understand bitbake internals by reimplementing them, then ultimately feeding any improvements to upstream bitbake.
Thanks
Chris
Quoted 10 lines
I find this idea very compelling.
Has anyone put any work into implementing the datastore itself as some kind of graph/tree? It "feels" like it should be possible.
I think it would be most similar to the task of implementing a spreadsheet formula evaluation engine. Spreadsheets deal with expanding arbitrarily complex formulas. The expansions can involve non-deterministic procedures (such as getting the current date/time). Seems highly applicable to DataSmart.
Quoted 5 lines
My testbed would probably be bytebraise
(https://github.com/agilent/bytebraise). But as it says on the tin, the goal of that project is just so that I can better understand bitbake internals by reimplementing them, then ultimately feeding any improvements to upstream bitbake.
For posterity: I discussed this with a colleague yesterday and our conclusion was, 'no', it is not feasible.
In ByteBraise itself I am planning on removing my own implementation of DataSmart and just using the Python implementation in BitBake via RustPython.
Thanks,
Chris
Quoted 24 lines · On Tue, 2023-09-19 at 15:04 +0000, [email protected] wrote:
Quoted 15 lines
I find this idea very compelling.
Has anyone put any work into implementing the datastore itself as some kind of
graph/tree? It "feels" like it should be possible.I think it would be most similar to the task of implementing a spreadsheet
formula evaluation engine. Spreadsheets deal with expanding arbitrarily
complex formulas. The expansions can involve non-deterministic procedures
(such as getting the current date/time). Seems highly applicable to DataSmart.Quoted 8 lines
My testbed would probably be bytebraise
(https://github.com/agilent/bytebraise). But as it says on the tin, the goal of
that project is just so that I can better understand bitbake internals by
reimplementing them, then ultimately feeding any improvements to upstream
bitbake.
Quoted 6 lines
For posterity: I discussed this with a colleague yesterday and our conclusion was, 'no', it is not feasible.
In ByteBraise itself I am planning on removing my own implementation of DataSmart and just using the Python implementation in BitBake via RustPython.
Thanks for the report back! Given my experiences with that code I suspected this might have some challenges but it could have been interesting :)
I would love to get back to the optimisation work but sadly other issues have been more pressing.
Cheers,
Richard