Skip to content
cccskills
FreePublished on GitHub

py-code-insight-counters

Measure work performed by the Python type engine in tests with PyCodeInsightCounters and PyPerfProbe. Compare operation counts, allocations, AST loads, and timing across scenarios to track performance changes or defects, using stable work counts where elapsed time may fluctuate.

OriginalShow the original description

Count Python code insight work in tests with PyCodeInsightCounters.

JetBrains/intellij-community20,664 stars(GitHub 20,664 + cccskills 0)

Run score Not tested

See how to install

Good for

  • Checking whether a code change reduces type engine work
  • Pinning a performance regression in a test
  • Comparing counter growth across input sizes
  • Measuring highlighting and AST load scenarios

Files included (1)

  • SKILL.md4.9 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

<!-- Generated by community/.ai/render-guides.mjs; edit community/.agents/skills/py-code-insight-counters/SKILL.md -->

Python code insight counters

PyCodeInsightCounters (python-psi-impl, package com.jetbrains.python.codeInsight) counts the work of the native Python type engine. PyPerfProbe (community/python/testFramework, package com.intellij.python.community.testFramework.performance) measures a scenario with these counters, the wall time, the allocations and the AST loads.

Use them to show that a change does less work, to show how the work grows with the input, or to pin a performance defect in a test. A work count does not flake like a time.

Counters

CounterCountsHook
GET_TYPE_CALLSeach getType callTypeEvalContextImpl.getType
GET_TYPE_CACHE_HITSthe calls that the context cache answersTypeEvalContextImpl.getType
GET_TYPE_EVALUATIONSthe calls that evaluate a typeTypeEvalContextImpl.getType
CONTEXTS_CONSTRUCTEDeach type context, also the library and assumption contextsTypeEvalContextImpl constructor
CONTEXT_LOOKUP_MISSESthe context lookups that store a new contextTypeEvalContextCacheImpl
ASSUME_TYPE_CALLSthe narrowing assumptions that create a contextTypeEvalContextImpl.assumeType
MATCH_STEPSthe steps of the type matchPyTypeChecker.match
OVERLOAD_CANDIDATES_CHECKEDthe overload candidates that the argument types checkPyCallExpressionHelper.matchesByArgumentTypes
CFG_BUILDS, CFG_INSTRUCTIONSthe control flow builds and their instructionsPyControlFlowBuilder.buildControlFlow

A cache hit and an evaluation do not add up to the calls. The rest are library delegations and the requests that the recursion guard stops.

Count in a test

The counting is off by default. For the work of the calling thread only:

val counts = PyCodeInsightCounters.countOnCurrentThread { context.getType(expression) }
assertEquals(1L, counts.getValue(Counter.GET_TYPE_EVALUATIONS))

The highlighting passes run on other threads, so countOnCurrentThread does not see their work. For all threads, take two snapshots:

PyCodeInsightCounters.enable(testRootDisposable)
val before = PyCodeInsightCounters.snapshot()
myFixture.doHighlighting()
val delta = PyCodeInsightCounters.snapshot() - before
assertTrue(delta[Counter.CFG_BUILDS] >= 1)

The delta also contains the background work of the IDE in that time. Assert an exact count only for the calling thread. Otherwise assert a bound or a ratio.

Measure a scenario

  1. Implement PyPerfProbe.Editor over the test fixture. FixtureEditor in PyNativeEngineCountersPerformanceTest is an example.
  2. Create the probe with a disposable. The probe turns the counting on, turns off the RecursionManager test checks and sets the registry keys of PyPerfProbe.PINNED_REGISTRY to their IDE values.
  3. Call one of these:
    • measure(scenario) { action } runs 3 warm-up attempts and 10 attempts. Before each attempt, setup runs. The default setup is cold().
    • measureEditorFile(name, inspections) measures a cold highlighting, a highlighting after a one-character edit, and one inferAll pass.
    • measureScaled(scenario) { action } uses fewer attempts when the first run takes more than 10 seconds.
  4. Call report(result). It prints PYPERF lines: the time quantiles, the allocations, the AST loads of the first attempt, and each counter with its median, its range and the value of the first attempt.

Mark the test class with @PerformanceUnitTest. The usual test runs skip it. tests.cmd runs it when you give its fully qualified name.

The system property pyperf.out names a file that also gets the PYPERF lines. The system property pyperf.astload.stacks=true prints the stack of the first three AST loads of each file.

Read the result

  • cold() drops the PSI caches and the type contexts. The PSI, the stubs, the control flow and the soft caches stay. So the counts of one scenario change between attempts. For a cold highlighting of pandas_examples.py, the range of type.getType.evaluations is about 20 % of the median.
  • Compare medians, and read the range= value before you claim a difference.
  • For the complexity of an algorithm, measure the input at several sizes (n, 2n, 4n) and compare the growth of a counter. Do not compare single times.

Add a temporary counter

  1. Add an entry to PyCodeInsightCounters.Counter with a stable dotted id, for example type.foo.calls.
  2. At the hook, call PyCodeInsightCounters.inc(Counter.FOO_CALLS) or add(counter, value). When the counting is off, a call costs one volatile read. Put other work at the hook behind PyCodeInsightCounters.isEnabled.
  3. Keep a counter in the merge request only when a test asserts on it. Remove the others before the merge.

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

actions

FreeSummary

Implement or update IntelliJ `AnAction` classes and their `plugin.xml` registrations. Keep labels and descriptions in message bundles, configure optional icons, and launch suspending work through the event coroutine scope so synchronous action handling does not freeze the IDE.

  • Adding a new IntelliJ action
  • Updating an action's registration or icon
  • Moving action labels into a message bundle
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

Move IntelliJ JPS test modules to Bazel and diagnose failures that occur only under Bazel. The guide helps trace missing dependencies, plugins, test data, and runfiles, then verify the generated target and migrated tests.

  • Migrating a JPS test module to Bazel
  • Diagnosing a test failure that occurs only in Bazel
  • Checking Bazel dependencies and test data wiring
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

code-style

FreeSummary

Write or review Kotlin and Java code in line with IntelliJ project conventions. The guidance covers language choice, formatting, sealed types, threading annotations, and safe handling of control flow exceptions so changes fit the surrounding codebase.

  • Writing a new Kotlin file that follows IntelliJ conventions
  • Reviewing Java code and checking its style guidance
  • Checking formatting, sealed-type handling, or exception handling
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

commits

FreeSummary

Create, amend, or rename IntelliJ commits with messages that follow the project’s ticket and label formats. Keep each commit focused, explain the reason and decision in the body when needed, and pass the message safely through the shell or PowerShell.

  • Writing a message for a change linked to a YouTrack ticket
  • Preparing a commit message for tests, cleanup, or documentation
  • Amending or renaming an existing IntelliJ commit
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

Compare how Python type checkers interpret a code snippet or file. The script runs selected tools, including ty, pyrefly, basedpyright, mypy, and zuban, then produces a Markdown report with verdicts, timings, and each checker's diagnostic output.

  • Comparing type inference for a Python snippet
  • Checking whether a Python expression should be a type error
  • Reviewing differences among Python type checkers
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

Shape Compose Desktop UI code by keeping composables focused on rendering and placing application decisions in presenters or coordinators. Get guidance on state ownership, effects, reusable component APIs, lazy-list keys, animation, and performance for individual UI changes.

  • Separating screen rendering from application decisions
  • Choosing which UI state belongs in composition
  • Structuring effects and coroutine work
JetBrains/intellij-community20.7KUpdated Oct 26, 2026

See all skills by JetBrains

Report a problem with this skill