texinfo-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

branch master updated: * tp/Texinfo/XS/convert/build_html_perl_state.c (


From: Patrice Dumas
Subject: branch master updated: * tp/Texinfo/XS/convert/build_html_perl_state.c (html_pass_conversion_initialization), tp/Texinfo/XS/convert/convert_html.c (html_free_converter), tp/Texinfo/XS/main/api_to_perl.c (unregister_html_converter_perl_hv), tp/Texinfo/XS/main/converter_types.h (CONVERTER): register the converter_info_hv in pl_info_hv field in converter to get a stable caching from C in Perl data, even if the Perl data changes.
Date: Tue, 01 Oct 2024 12:35:36 -0400

This is an automated email from the git hooks/post-receive script.

pertusus pushed a commit to branch master
in repository texinfo.

The following commit(s) were added to refs/heads/master by this push:
     new 034679550b * tp/Texinfo/XS/convert/build_html_perl_state.c 
(html_pass_conversion_initialization), tp/Texinfo/XS/convert/convert_html.c 
(html_free_converter), tp/Texinfo/XS/main/api_to_perl.c 
(unregister_html_converter_perl_hv), tp/Texinfo/XS/main/converter_types.h 
(CONVERTER): register the converter_info_hv in pl_info_hv field in converter to 
get a stable caching from C in Perl data, even if the Perl data changes.
034679550b is described below

commit 034679550bffb4d8396c45a75574315302ec5002
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Thu Jul 25 16:43:30 2024 +0200

    * tp/Texinfo/XS/convert/build_html_perl_state.c
    (html_pass_conversion_initialization),
    tp/Texinfo/XS/convert/convert_html.c (html_free_converter),
    tp/Texinfo/XS/main/api_to_perl.c (unregister_html_converter_perl_hv),
    tp/Texinfo/XS/main/converter_types.h (CONVERTER): register the
    converter_info_hv in pl_info_hv field in converter to get a stable
    caching from C in Perl data, even if the Perl data changes.
    
    * tp/Texinfo/XS/main/api_to_perl.c (unregister_document_hv),
    tp/Texinfo/XS/main/build_perl_info.c (get_document, fill_document_hv),
    tp/Texinfo/XS/main/document_types.h (DOCUMENT): register the document
    HV in document to get a stable caching from C in Perl data, even if
    the Perl data changes.
    
    * tp/Texinfo/XS/convert/ConvertXS.xs
    (get_converter_indices_sorted_by_index)
    (get_converter_indices_sorted_by_letter),
    tp/Texinfo/XS/main/build_perl_info.c (BUILD_PERL_DOCUMENT_ITEM)
    (BUILD_PERL_DOCUMENT_LIST, document_global_information): use the
    C document hv field to cache Perl data instead of the passde document,
    in case it changed.  A changed document happens for DOcBook in tests
    as a copy of the document is made, with the same document descriptor
    and another object.
    
    * tp/Texinfo/XS/convert/build_html_perl_state.c
    (pass_sv_converter_info): use the Perl object in C to cache.
---
 ChangeLog                                     | 39 +++++++++++++++++++++----
 tp/Texinfo/XS/convert/ConvertXS.xs            | 14 +++++----
 tp/Texinfo/XS/convert/build_html_perl_state.c | 19 ++++++++----
 tp/Texinfo/XS/convert/convert_html.c          |  2 ++
 tp/Texinfo/XS/main/api_to_perl.c              | 26 +++++++++++++++++
 tp/Texinfo/XS/main/api_to_perl.h              |  3 ++
 tp/Texinfo/XS/main/build_perl_info.c          | 42 ++++++++++++++++++++-------
 tp/Texinfo/XS/main/converter_types.h          |  7 +++++
 tp/Texinfo/XS/main/document_types.h           |  8 +++++
 tp/t/protect_character_in_texinfo.t           |  2 ++
 10 files changed, 135 insertions(+), 27 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f873b1e310..a681b8e1c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,31 @@
-2024-09-30  Patrice Dumas  <pertusus@free.fr>
+2024-07-25  Patrice Dumas  <pertusus@free.fr>
 
-       * tp/Texinfo/XS/convert/ConvertXS.xs (html_command_description),
-       tp/Texinfo/XS/convert/call_html_perl_function.c
-       (call_formatting_function_format_single_footnote): check and reset
-       flags in build_html_formatting_state.
+       * tp/Texinfo/XS/convert/build_html_perl_state.c
+       (html_pass_conversion_initialization),
+       tp/Texinfo/XS/convert/convert_html.c (html_free_converter),
+       tp/Texinfo/XS/main/api_to_perl.c (unregister_html_converter_perl_hv),
+       tp/Texinfo/XS/main/converter_types.h (CONVERTER): register the
+       converter_info_hv in pl_info_hv field in converter to get a stable
+       caching from C in Perl data, even if the Perl data changes.
+
+       * tp/Texinfo/XS/main/api_to_perl.c (unregister_document_hv),
+       tp/Texinfo/XS/main/build_perl_info.c (get_document, fill_document_hv),
+       tp/Texinfo/XS/main/document_types.h (DOCUMENT): register the document
+       HV in document to get a stable caching from C in Perl data, even if
+       the Perl data changes.
+
+       * tp/Texinfo/XS/convert/ConvertXS.xs
+       (get_converter_indices_sorted_by_index)
+       (get_converter_indices_sorted_by_letter),
+       tp/Texinfo/XS/main/build_perl_info.c (BUILD_PERL_DOCUMENT_ITEM)
+       (BUILD_PERL_DOCUMENT_LIST, document_global_information): use the
+       C document hv field to cache Perl data instead of the passde document,
+       in case it changed.  A changed document happens for DOcBook in tests
+       as a copy of the document is made, with the same document descriptor
+       and another object.
+
+       * tp/Texinfo/XS/convert/build_html_perl_state.c
+       (pass_sv_converter_info): use the Perl object in C to cache.
 
 2024-07-24  Patrice Dumas  <pertusus@free.fr>
 
@@ -150,6 +172,13 @@
        do not call build_html_formatting_state, there is nothing set in C
        that needs to be passed.
 
+2024-09-30  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/XS/convert/ConvertXS.xs (html_command_description),
+       tp/Texinfo/XS/convert/call_html_perl_function.c
+       (call_formatting_function_format_single_footnote): check and reset
+       flags in build_html_formatting_state.
+
 2024-07-24  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/convert/build_html_perl_state.c
diff --git a/tp/Texinfo/XS/convert/ConvertXS.xs 
b/tp/Texinfo/XS/convert/ConvertXS.xs
index 644aa46711..b07fc11c72 100644
--- a/tp/Texinfo/XS/convert/ConvertXS.xs
+++ b/tp/Texinfo/XS/convert/ConvertXS.xs
@@ -302,9 +302,10 @@ get_converter_indices_sorted_by_index (SV *converter_sv)
         document_sv = hv_fetch (converter_hv, "document",
                                 strlen ("document"), 0);
         RETVAL = 0;
-        if (document_sv)
+        if (document_sv && self->document->hv)
           {
-            /* The sorted indices are cached in the same place as in Perl code.
+            /* The sorted indices are cached in the same place as in Perl code
+               gathered at document initialization.
                Either Perl code or XS code is used, so this is for consistency
                not really for interoperability */
             /* set to document "sorted_indices_by_index" */
@@ -316,7 +317,7 @@ get_converter_indices_sorted_by_index (SV *converter_sv)
                not found */
             if (language)
               {
-                HV *document_hv = (HV *) SvRV (*document_sv);
+                HV *document_hv = (HV *) self->document->hv;
                 SV *index_entries_by_index_sv
                  = get_language_document_hv_sorted_indices (document_hv,
                                     "sorted_indices_by_index", language,
@@ -375,9 +376,10 @@ get_converter_indices_sorted_by_letter (SV *converter_sv)
         document_sv = hv_fetch (converter_hv, "document",
                                 strlen ("document"), 0);
         RETVAL = 0;
-        if (document_sv)
+        if (document_sv && self->document->hv)
           {
-            /* The sorted indices are cached in the same place as in Perl code.
+            /* The sorted indices are cached in the same place as in Perl code
+               gathered at document initialization.
                Either Perl code or XS code is used, so this is for consistency
                not really for interoperability */
             /* set to document "sorted_indices_by_letter" */
@@ -389,7 +391,7 @@ get_converter_indices_sorted_by_letter (SV *converter_sv)
                not found */
             if (language)
               {
-                HV *document_hv = (HV *) SvRV (*document_sv);
+                HV *document_hv = (HV *) self->document->hv;
                 SV *index_entries_by_index_sv
                  = get_language_document_hv_sorted_indices (document_hv,
                                     "sorted_indices_by_letter", language,
diff --git a/tp/Texinfo/XS/convert/build_html_perl_state.c 
b/tp/Texinfo/XS/convert/build_html_perl_state.c
index 3b69a49262..15acfef271 100644
--- a/tp/Texinfo/XS/convert/build_html_perl_state.c
+++ b/tp/Texinfo/XS/convert/build_html_perl_state.c
@@ -238,14 +238,22 @@ html_pass_conversion_initialization (CONVERTER *converter,
   converter_hv = (HV *) SvRV (converter_sv);
 
   /* always set the document in the converter, as it is the only
-     way to find it back, it is not stored in C data */
-
+     way to find it back, it is not stored in C data.
+     TODO it is not foolproof, in case a document with the same descriptor
+     associate with another hash is passed.  There is/was a practical case,
+     in tests, a workaround for DocBook for tree modifications
+     with a copy of document hash.
+   */
   pass_document_to_converter_sv (converter, converter_sv, document_in);
 
   /* always set "converter_info" for calls to get_info in Perl. */
   converter_info_hv = newHV ();
 
   STORE("converter_info", newRV_noinc ((SV *)converter_info_hv));
+  /* store in C to be sure that the caching is in the same Perl object
+     even if the Perl data changes */
+  converter->pl_info_hv = converter_info_hv;
+  SvREFCNT_inc (converter_info_hv);
 
   if (converter)
     {
@@ -885,7 +893,6 @@ pass_sv_converter_info (const CONVERTER *converter,
                         const char *converter_info, SV *converter_sv)
 {
   HV *converter_hv;
-  SV **converter_info_sv;
   HV *converter_info_hv;
   SV **info_sv;
   SV *new_sv = 0;
@@ -893,9 +900,9 @@ pass_sv_converter_info (const CONVERTER *converter,
   dTHX;
 
   converter_hv = (HV *) SvRV (converter_sv);
-  converter_info_sv = hv_fetch (converter_hv, "converter_info",
-                                strlen ("converter_info"), 0);
-  converter_info_hv = (HV *) SvRV (*converter_info_sv);
+  /* do not find the cache in Perl data but in C to be more robust to
+     changes in Perl objects */
+  converter_info_hv = converter->pl_info_hv;
 
   /* The information is cached in the same place as in Perl code.
      Either Perl code or XS code is used, so this is for consistency
diff --git a/tp/Texinfo/XS/convert/convert_html.c 
b/tp/Texinfo/XS/convert/convert_html.c
index 5c57416482..b013c1a651 100644
--- a/tp/Texinfo/XS/convert/convert_html.c
+++ b/tp/Texinfo/XS/convert/convert_html.c
@@ -19165,6 +19165,8 @@ html_free_converter (CONVERTER *self)
   self->registered_ids = 0;
 #endif
 
+  unregister_html_converter_perl_hv (self);
+
   html_free_files_source_info (&self->files_source_info);
 
   free_strings_list (&self->check_htmlxref_already_warned);
diff --git a/tp/Texinfo/XS/main/api_to_perl.c b/tp/Texinfo/XS/main/api_to_perl.c
index 467ad4c6d8..6b41344680 100644
--- a/tp/Texinfo/XS/main/api_to_perl.c
+++ b/tp/Texinfo/XS/main/api_to_perl.c
@@ -33,6 +33,7 @@
 #include "tree_types.h"
 #include "option_types.h"
 #include "converter_types.h"
+#include "document_types.h"
 /* non_perl_* */
 #include "utils.h"
 
@@ -117,3 +118,28 @@ croak_message (char *message)
 {
   croak ("%s\n", message);
 }
+
+/* HTML specific */
+void
+unregister_html_converter_perl_hv (CONVERTER *converter)
+{
+  dTHX;
+
+  if (converter->pl_info_hv)
+    {
+      SvREFCNT_dec ((SV *)converter->pl_info_hv);
+      converter->pl_info_hv = 0;
+    }
+}
+
+void
+unregister_document_hv (DOCUMENT *document)
+{
+  dTHX;
+
+  if (document->hv)
+    {
+      SvREFCNT_dec ((SV *)document->hv);
+      document->hv = 0;
+    }
+}
diff --git a/tp/Texinfo/XS/main/api_to_perl.h b/tp/Texinfo/XS/main/api_to_perl.h
index bc904f8793..ce06896382 100644
--- a/tp/Texinfo/XS/main/api_to_perl.h
+++ b/tp/Texinfo/XS/main/api_to_perl.h
@@ -8,6 +8,9 @@
 void unregister_perl_tree_element (ELEMENT *e);
 void unregister_perl_button (BUTTON_SPECIFICATION *button);
 void register_perl_button (BUTTON_SPECIFICATION *button);
+void unregister_document_hv (DOCUMENT *document);
+/* HTML specific */
+void unregister_html_converter_perl_hv (CONVERTER *converter);
 
 void call_switch_to_global_locale (void);
 void call_sync_locale (void);
diff --git a/tp/Texinfo/XS/main/build_perl_info.c 
b/tp/Texinfo/XS/main/build_perl_info.c
index 31fa797692..f23e5b4780 100644
--- a/tp/Texinfo/XS/main/build_perl_info.c
+++ b/tp/Texinfo/XS/main/build_perl_info.c
@@ -1693,6 +1693,18 @@ get_document (size_t document_descriptor)
             strlen ("tree_document_descriptor"),
             newSViv (document_descriptor), 0);
 
+  if (!document->hv)
+    {
+      document->hv = (void *) hv;
+      SvREFCNT_inc ((SV *)hv);
+    }
+  else
+    {
+      if (document->options && document->options->DEBUG.o.integer > 0)
+        fprintf (stderr,
+          "get_document: %zu: already %p and new %p document hv\n",
+                 document_descriptor, document->hv, hv);
+    }
   hv_stash = gv_stashpv ("Texinfo::Document", GV_ADD);
   sv = newRV_noinc ((SV *) hv);
   sv_bless (sv, hv_stash);
@@ -1803,10 +1815,23 @@ fill_document_hv (HV *hv, size_t document_descriptor, 
int no_store)
                 strlen ("tree_document_descriptor"),
                 newSViv (document_descriptor), 0);
 
+      if (!document->hv)
+        {
+          document->hv = (void *) hv;
+          SvREFCNT_inc ((SV *)hv);
+        }
+      else if ((HV *)document->hv != hv)
+        {/* this happens if called through rebuild_tree as build_document
+            is called in that case, the document HV is not reused */
+          if (document->options && document->options->DEBUG.o.integer > 0)
+            fprintf (stderr,
+              "fill_document_hv: %zu: %p and new %p document hv differ\n",
+                     document_descriptor, document->hv, hv);
+        }
     }
 }
 
-/* Return Texinfo::Document perl object corresponding to the
+/* Return a new Texinfo::Document perl object corresponding to the
    C document structure corresponding to DOCUMENT_DESCRIPTOR.
    If NO_STORE is set, destroy the C document.
  */
@@ -1900,14 +1925,14 @@ funcname (SV *document_in) \
         {\
           HVAV *result_av_hv = buildname (document->fieldname);\
           result_sv = newRV_inc ((SV *) result_av_hv);\
-          hv_store (document_hv, key, strlen (key), result_sv, 0);\
+          hv_store (document->hv, key, strlen (key), result_sv, 0);\
           document->modified_information &= ~flagname;\
         }\
     }\
 \
   if (!result_sv)\
     {\
-      SV **sv_reference = hv_fetch (document_hv, key, strlen (key), 0);\
+      SV **sv_reference = hv_fetch (document->hv, key, strlen (key), 0);\
       if (sv_reference && SvOK (*sv_reference))\
         result_sv = *sv_reference;\
     }\
@@ -1954,14 +1979,14 @@ funcname (SV *document_in) \
         {\
           HVAV *result_av_hv = buildname (&document->fieldname);\
           result_sv = newRV_inc ((SV *) result_av_hv);\
-          hv_store (document_hv, key, strlen (key), result_sv, 0);\
+          hv_store (document->hv, key, strlen (key), result_sv, 0);\
           document->modified_information &= ~flagname;\
         }\
     }\
 \
   if (!result_sv)\
     {\
-      SV **sv_reference = hv_fetch (document_hv, key, strlen (key), 0);\
+      SV **sv_reference = hv_fetch (document->hv, key, strlen (key), 0);\
       if (sv_reference && SvOK (*sv_reference))\
         result_sv = *sv_reference;\
     }\
@@ -1997,14 +2022,11 @@ 
BUILD_PERL_DOCUMENT_LIST(document_global_commands_information,global_commands,"c
 SV *
 document_global_information (SV *document_in)
 {
-  HV *document_hv;
   SV *result_sv = 0;
   const char *key = "global_info";
 
   dTHX;
 
-  document_hv = (HV *) SvRV (document_in);
-
   DOCUMENT *document = get_sv_document_document (document_in,
                                      "document_global_information");
   if (document)
@@ -2014,14 +2036,14 @@ document_global_information (SV *document_in)
           HV *result_hv = build_global_info (&document->global_info,
                                              &document->global_commands);
           result_sv = newRV_inc ((SV *) result_hv);
-          hv_store (document_hv, key, strlen (key), result_sv, 0);
+          hv_store (document->hv, key, strlen (key), result_sv, 0);
           document->modified_information &= ~F_DOCM_global_info;
         }
     }
 
   if (!result_sv)
     {
-      SV **sv_reference = hv_fetch (document_hv, key, strlen (key), 0);
+      SV **sv_reference = hv_fetch (document->hv, key, strlen (key), 0);
       if (sv_reference && SvOK (*sv_reference))
         result_sv = *sv_reference;
     }
diff --git a/tp/Texinfo/XS/main/converter_types.h 
b/tp/Texinfo/XS/main/converter_types.h
index a52260299f..7f7a41e14b 100644
--- a/tp/Texinfo/XS/main/converter_types.h
+++ b/tp/Texinfo/XS/main/converter_types.h
@@ -900,6 +900,13 @@ typedef struct CONVERTER {
     FILE_ASSOCIATED_INFO_LIST html_files_information;
     /* state common with perl converter, not transmitted to perl */
     int use_unicode_text;
+
+    /* used to cache information passed to Perl, corresponding to
+       document and converter_info.  Actually reference to the corresponding
+       Perl objects as setup at initialization, kept here to be sure that
+       they do not change even if later on another object is passed.
+     */
+    void *pl_info_hv; /* converter->{'converter_info'} */
 } CONVERTER;
 
 typedef struct TRANSLATED_SUI_ASSOCIATION {
diff --git a/tp/Texinfo/XS/main/document_types.h 
b/tp/Texinfo/XS/main/document_types.h
index 89982490f6..4deee2c936 100644
--- a/tp/Texinfo/XS/main/document_types.h
+++ b/tp/Texinfo/XS/main/document_types.h
@@ -226,6 +226,14 @@ typedef struct DOCUMENT {
 
     /* flags for modified information not already passed to Perl */
     unsigned long modified_information;
+
+    /* reference to Perl document.  Should not be used to find the document
+       but to have a place where caching can happen reliably even if the
+       caller changes the hash associated to a descriptor.  There is/was a
+       practical case, in tests, a workaround for DocBook for tree
+       modifications with a copy of document hash.
+     */
+    void *hv;
 } DOCUMENT;
 
 /* following not in document, but used in parser */
diff --git a/tp/t/protect_character_in_texinfo.t 
b/tp/t/protect_character_in_texinfo.t
index 397496e14d..6673936034 100644
--- a/tp/t/protect_character_in_texinfo.t
+++ b/tp/t/protect_character_in_texinfo.t
@@ -59,6 +59,8 @@ sub run_test($$$$)
   # rebuild tree
   $tree_as_text = $document->tree();
 
+  # rebuild_tree calls build_document, therefore a new document is used instead
+  # of the one set during parsing.
   if ($XS_structuring) {
     $tree_as_line = Texinfo::Document::rebuild_tree($tree_as_line);
   }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]