libxml-raku / LibXML-raku

Raku bindings to the libxml2 native library
Artistic License 2.0
11 stars 5 forks source link

Extend use of class mapping #98

Closed vrurg closed 1 year ago

vrurg commented 1 year ago

Give out more control to user-specified classes by boxing with class-from-provided classes.

vrurg commented 1 year ago

With these changes I managed to cover 100% of boxing in my code with cache. The stats, the way I posted them in #94, got count 1 for each unique key! Consequently, both a local test and the real tool working on a real file are ~3 times faster!

Downsides: I'm not as confident with this PR as I wish it to be. The overall point is to first get a user class for a raw node and then box through the class. Unfortunately, I have some doubts about applicability of this approach to LibXML::Node::List and ::Set. Moreover, ::Set has an issue marked as TODO for now.

Otherwise it seems that implementing caching would be less painful than I anticipated. The plan is to:

  1. Have an attribute $!use-cache or $!caching on LibXML::Config. R/O for now.
  2. Add a new method named maybe-cached on the config. The method would try to use cached entries if $!use-cache is true. Otherwise it would dispatch over a raw node by mapping it into a user class (class-from but done using config-local structure, thus not paying the price of extra method call) and boxing if necessary.
  3. If no cache entry found, or the raw node is undefined, then maybe-cached would use callbacks to create new objects, if such callbacks are provided. Otherwise it would use box on the resolved user class.

Some minor details are skipped, they may depend on the outcomes of the implementation itself.