texinfo-commits
[Top][All Lists]
Advanced

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

branch master updated: * tp/Texinfo/XS/main/document_types.h (DOCUMENT):


From: Patrice Dumas
Subject: branch master updated: * tp/Texinfo/XS/main/document_types.h (DOCUMENT): add a modified_information flags field to DOCUMENT to keep track of which information is passed to Perl.
Date: Fri, 08 Mar 2024 14:09:23 -0500

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 0b144db1d7 * tp/Texinfo/XS/main/document_types.h (DOCUMENT): add a 
modified_information flags field to DOCUMENT to keep track of which information 
is passed to Perl.
0b144db1d7 is described below

commit 0b144db1d7fd445e6f40331a31ccacec9a2ed94d
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Fri Mar 8 20:09:22 2024 +0100

    * tp/Texinfo/XS/main/document_types.h (DOCUMENT): add a
    modified_information flags field to DOCUMENT to keep track of which
    information is passed to Perl.
    
    * tp/Texinfo/XS/main/DocumentXS.xs (indices_sort_strings),
    tp/Texinfo/XS/main/document.c (document_indices_sort_strings): build
    Perl indices_sort_strings information only once based on a DOCUMENT
    modified_information flag, and cache the Perl indices_sort_strings
    information in the Perl document hash, as is done in Perl overriden
    code.
---
 ChangeLog                           | 13 +++++++++
 tp/Texinfo/XS/main/DocumentXS.xs    | 58 +++++++++++++++++++++++++++++--------
 tp/Texinfo/XS/main/document.c       |  3 +-
 tp/Texinfo/XS/main/document_types.h |  5 ++++
 4 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 40c0d99e04..9ef5d4ddf9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-03-08  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/XS/main/document_types.h (DOCUMENT): add a
+       modified_information flags field to DOCUMENT to keep track of which
+       information is passed to Perl.
+
+       * tp/Texinfo/XS/main/DocumentXS.xs (indices_sort_strings),
+       tp/Texinfo/XS/main/document.c (document_indices_sort_strings): build
+       Perl indices_sort_strings information only once based on a DOCUMENT
+       modified_information flag, and cache the Perl indices_sort_strings
+       information in the Perl document hash, as is done in Perl overriden
+       code.
+
 2024-03-07  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/main/DocumentXS.xs,
diff --git a/tp/Texinfo/XS/main/DocumentXS.xs b/tp/Texinfo/XS/main/DocumentXS.xs
index 5f29518e51..d6eccc8db4 100644
--- a/tp/Texinfo/XS/main/DocumentXS.xs
+++ b/tp/Texinfo/XS/main/DocumentXS.xs
@@ -170,7 +170,7 @@ set_document_global_info (SV *document_in, char *key, SV 
*value_sv)
             else if (!strcmp (key, "input_perl_encoding"))
               {
                 /* should not be needed, but in case global information
-                   is reused, it will be ok */
+                   is reused, it will avoid memory leaks */
                 non_perl_free (document->global_info->input_perl_encoding);
                 document->global_info->input_perl_encoding
                    = non_perl_strdup ((char *)SvPVbyte_nolen(value_sv));
@@ -183,39 +183,73 @@ set_document_global_info (SV *document_in, char *key, SV 
*value_sv)
               }
           }
 
-# main_configuration, prefer_reference_element
+# customization_information, prefer_reference_element
 SV *
 indices_sort_strings (SV *document_in, ...)
     PROTOTYPE: $$;$
     PREINIT:
         DOCUMENT *document = 0;
         const INDICES_SORT_STRINGS *indices_sort_strings = 0;
-        SV **indices_information_sv;
         HV *document_hv;
         int prefer_reference_element = 0;
+        SV *result_sv = 0;
+        const char *sort_strings_key = "index_entries_sort_strings";
      CODE:
         document = get_sv_document_document (document_in,
                                              "indices_sort_strings");
         if (items > 2 && SvOK(ST(2)))
           prefer_reference_element = SvIV (ST(2));
+
         if (document)
           indices_sort_strings
            = document_indices_sort_strings (document, document->error_messages,
                                   document->options, prefer_reference_element);
 
         document_hv = (HV *) SvRV (document_in);
-        indices_information_sv
-          = hv_fetch (document_hv, "indices", strlen ("indices"), 0);
 
-        if (indices_sort_strings && indices_information_sv)
+        if (indices_sort_strings)
           {
-            HV *indices_information_hv = (HV *) SvRV (*indices_information_sv);
-            HV *indices_sort_strings_hv
-              = build_indices_sort_strings (indices_sort_strings,
-                                            indices_information_hv);
-
-            RETVAL = newRV_inc ((SV *) indices_sort_strings_hv);
+            /* build Perl data only if needed and cache the built Perl
+               data in the same hash as done in overriden Perl code */
+            if (document->modified_information & F_DOCM_indices_sort_strings)
+              {
+    /* TODO maybe would be better to call $document->indices_information()
+       or build_indices_information function to pass to Perl if needed */
+                SV **indices_information_sv
+                  = hv_fetch (document_hv, "indices", strlen ("indices"), 0);
+                if (indices_information_sv)
+                  {
+                    HV *indices_information_hv
+                        = (HV *) SvRV (*indices_information_sv);
+                    HV *indices_sort_strings_hv
+                     = build_indices_sort_strings (indices_sort_strings,
+                                                   indices_information_hv);
+
+                    result_sv = newRV_inc ((SV *) indices_sort_strings_hv);
+                    SvREFCNT_inc (result_sv);
+                    hv_store (document_hv, sort_strings_key,
+                              strlen (sort_strings_key), result_sv, 0);
+                    document->modified_information
+                                &= ~F_DOCM_indices_sort_strings;
+                  }
+                /* warn if not found? */
+              }
+            else
+              { /* retrieve previously stored result */
+                SV **index_entries_sort_strings_sv
+                  = hv_fetch (document_hv, sort_strings_key,
+                              strlen (sort_strings_key), 0);
+                if (index_entries_sort_strings_sv
+                    && SvOK (*index_entries_sort_strings_sv))
+                  {
+                    result_sv = *index_entries_sort_strings_sv;
+                    SvREFCNT_inc (result_sv);
+                  }
+                /* error out if not found?  Or rebuild? */
+              }
           }
+        if (result_sv)
+          RETVAL = result_sv;
         else
           RETVAL = newSV (0);
     OUTPUT:
diff --git a/tp/Texinfo/XS/main/document.c b/tp/Texinfo/XS/main/document.c
index 2e1679c9d9..239f1c0d17 100644
--- a/tp/Texinfo/XS/main/document.c
+++ b/tp/Texinfo/XS/main/document.c
@@ -173,8 +173,9 @@ document_indices_sort_strings (DOCUMENT *document,
        = setup_index_entries_sort_strings (error_messages, options,
                                merged_indices, document->index_names,
                                prefer_reference_element);
-    }
 
+      document->modified_information |= F_DOCM_indices_sort_strings;
+    }
   return document->indices_sort_strings;
 }
 
diff --git a/tp/Texinfo/XS/main/document_types.h 
b/tp/Texinfo/XS/main/document_types.h
index ffba2aef84..ef24acaab9 100644
--- a/tp/Texinfo/XS/main/document_types.h
+++ b/tp/Texinfo/XS/main/document_types.h
@@ -32,6 +32,8 @@ struct OPTIONS;
 /* same with convert_to_text.h */
 struct TEXT_OPTIONS;
 
+#define F_DOCM_indices_sort_strings      0x0001
+
 enum error_type { MSG_error, MSG_warning,
                   MSG_document_error, MSG_document_warning };
 
@@ -180,6 +182,9 @@ typedef struct DOCUMENT {
     INDICES_SORT_STRINGS *indices_sort_strings;
     COLLATIONS_INDICES_SORTED_BY_INDEX *sorted_indices_by_index;
     COLLATIONS_INDICES_SORTED_BY_LETTER *sorted_indices_by_letter;
+
+    /* flags for modified information not already passed to Perl */
+    unsigned long modified_information;
 } DOCUMENT;
 
 /* not in document, but used in parser */



reply via email to

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