8000 FIX: Update hash tables of affected profiler instances when rewriting code objects by TTsangSC · Pull Request #351 · pyutils/line_profiler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

FIX: Update hash tables of affected profiler instances when rewriting code objects #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

8000 TTsangSC
Copy link
Collaborator
@TTsangSC TTsangSC commented Jun 10, 2025

Closes #350. Includes #349, which by extension closes #348. New changes are from 7338799 onwards.

Code changes

  • line_profiler/_line_profiler.pyx::LineProfiler
    • .add_function()
      • Now synchronizing the hash tables of instances profiling the same function object whenever it replaces its code object
      • No longer creating separate .dupes_map items for padded bytecodes
    • ._all_paddings, ._all_instances_by_funcs
      New private class attributes for use by .add_function()

Test changes

  • tests/test_line_profiler.py
    • test_{{class,static,bound,partial}method,partial,[cached_]property}_decorator(), test_profile_generated_code()
      Minor refactoring with a utility function for more readable output
    • test_multiple_profilers_identical_bytecode()
      New test checking that the order in which profilers are used to decorate functions shouldn't affect the output (up to overhead)
    • test_aggregate_profiling_data_between_code_versions()
      New test checking that existing profiling data should persist and be aggregated with new data after another profiler replaced the code object of a profiled function

Performance impact

Test suite run 5 times with the command pytest 'not (test_duplicate or test_aggregate or identical_bytecode)', i.e. excluding the tests added by this PR (and #349); impact on performance should be minimal.

Setup\Code (mean, best (s)) main multi-desync-fix (this PR)
Python 3.8.20, pytest 8.3.5 7.605, 7.518 7.523, 7.426
Python 3.13.3, pytest 8.4.0 9.835, 9.589 9.777, 9.662

TTsangSC added 9 commits June 8, 2025 02:43
line_profiler/_line_profiler.pyx::LineProfiler.get_stats()
    Updated/simplified implementation to aggregate profiling data
    between code objects with duplicate hashes
line_profiler/_line_profiler.pyx::LineProfiler
    _all_instances_by_bcodes
        New private class attribute for keeping track of:
        - Instances profiling the same bytecode
        - Count for how the the bytecode should be padded
    dupes_map
        Now containing the up-to-date (instead of pre-padding) code objs
    add_function()
        Refactored implementation to:
        - Pad bytecodes according to `._all_instances_by_bcodes` instead
          of the instance's `.dupes_map`
        - Use `.dupes_map` to identify profiler instances profiling the
          same functions and update their hash tables
        - Curate `.dupes_map` on said instances to stay up-to-date
line_profiler/_line_profiler.pyx::LineProfiler
    add_function()
        - Now identifying update targets based on function-object
          identity instead of code-object identity (because different
          function objects can share the same code object)
        - Simplified implementation
    _all_instances_by_bcodes
        Superseded by other private class attributes:
        - `._all_paddings`:
          mapping from bytecodes to padding length
        - `._all_instances_by_funcs`:
          mapping from function identity to `WeakSet`s of instances
tests/test_line_profiler.py
    test_multiple_profilers_identical_bytecode()
        New test for applying multiple profilers to identical functions
        in arbitrary order (one subtest currently failing)
line_profiler/_line_profiler.pyx::LineProfiler.add_function()
    Now stripping padded code objects and re-padding when appropriate,
    so that no two profiled code objects can end up with the same
    bytecode

tests/test_line_profiler.py
    test_multiple_profilers_identical_bytecode()
        - Updated comments and docstring
        - Added check against duplicate bytecodes
tests/test_line_profiler.py
    get_prof_stats()
        New helper function for formatting and retrieving
        `LineProfiler.print_stats()` output
    test_*_decorator()
    test_profile_generated_code()
    test_multiple_profilers_identical_bytecode()
        Refactored to use `get_prof_stats()`
    test_aggregate_profiling_data_between_code_versions()
        New test checking that profiling data gathered before and after
        a function's code object is rewritten are correctly aggregated
@TTsangSC TTsangSC changed the title FIX: FIX: Update hash tables of affected profiler instances when rewriting code objects Jun 10, 2025
Copy link
codecov bot commented Jun 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 65.33%. Comparing base (6889534) to head (781c01e).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #351   +/-   ##
=======================================
  Coverage   65.33%   65.33%           
=======================================
  Files          13       13           
  Lines        1073     1073           
  Branches      234      234           
=======================================
  Hits          701      701           
  Misses        310      310           
  Partials       62       62           

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a344928...781c01e. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: dupe-code resolution on multiple profilers results in loss of profiling data Timing entries not combined between duplicate code objects
1 participant
0