diff --git a/mercurial/statprof.py b/mercurial/statprof.py
index 068307b638f45e6bdab09b3030edb2cac2935b64_bWVyY3VyaWFsL3N0YXRwcm9mLnB5..8b0a3ff5ed1278894f76e8c8f0a8777bdc7ccb4b_bWVyY3VyaWFsL3N0YXRwcm9mLnB5 100644
--- a/mercurial/statprof.py
+++ b/mercurial/statprof.py
@@ -768,5 +768,11 @@
                 filename,
                 function,
             )
-            codepattern = b'%' + (b'%d' % (55 - len(liststring))) + b's %d:  %s'
+            # 4 to account for the word 'line'
+            spacing_len = max(4, 55 - len(liststring))
+            prefix = b''
+            if spacing_len == 4:
+                prefix = b', '
+
+            codepattern = b'%s%s %d: %s%s'
             codestring = codepattern % (
@@ -772,3 +778,4 @@
             codestring = codepattern % (
-                b'line',
+                prefix,
+                b'line'.rjust(spacing_len),
                 site.lineno,
@@ -774,4 +781,5 @@
                 site.lineno,
+                b''.ljust(max(0, 4 - len(str(site.lineno)))),
                 site.getsource(30),
             )
 
diff --git a/tests/test-profile.t b/tests/test-profile.t
index 068307b638f45e6bdab09b3030edb2cac2935b64_dGVzdHMvdGVzdC1wcm9maWxlLnQ=..8b0a3ff5ed1278894f76e8c8f0a8777bdc7ccb4b_dGVzdHMvdGVzdC1wcm9maWxlLnQ= 100644
--- a/tests/test-profile.t
+++ b/tests/test-profile.t
@@ -64,9 +64,9 @@
 
 Install an extension that can sleep and guarantee a profiler has time to run
 
-  $ cat >> sleepext.py << EOF
+  $ cat >> sleepext_with_a_long_filename.py << EOF
   > import time
   > from mercurial import registrar
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > @command(b'sleep', [], b'hg sleep')
@@ -68,11 +68,11 @@
   > import time
   > from mercurial import registrar
   > cmdtable = {}
   > command = registrar.command(cmdtable)
   > @command(b'sleep', [], b'hg sleep')
-  > def sleep(ui, *args, **kwargs):
+  > def sleep_for_at_least_one_stat_cycle(ui, *args, **kwargs):
   >     time.sleep(0.1)
   > EOF
 
   $ cat >> $HGRCPATH << EOF
   > [extensions]
@@ -74,9 +74,9 @@
   >     time.sleep(0.1)
   > EOF
 
   $ cat >> $HGRCPATH << EOF
   > [extensions]
-  > sleep = `pwd`/sleepext.py
+  > sleep = `pwd`/sleepext_with_a_long_filename.py
   > EOF
 
 statistical profiler works
@@ -90,7 +90,7 @@
   $ grep -v _path_stat ../out | head -n 3
     %   cumulative      self          
    time    seconds   seconds  name    
-  * sleepext.py:*:sleep (glob)
+  * sleepext_with_a_long_filename.py:*:sleep_for_at_least_one_stat_cycle (glob)
   $ cat ../out | statprofran
 
   $ hg --profile --config profiling.statformat=bymethod sleep 2>../out || cat ../out
@@ -100,8 +100,8 @@
 
   $ hg --profile --config profiling.statformat=hotpath sleep 2>../out || cat ../out
   $ cat ../out | statprofran
-  $ grep sleepext.py ../out
-  .* [0-9.]+%  [0-9.]+s  sleepext.py:\s*sleep           line 7:  time\.sleep.* (re)
+  $ grep sleepext_with_a_long_filename.py ../out
+  .* [0-9.]+%  [0-9.]+s  sleepext_with_a_long_filename.py:\s*sleep_for_at_least_one_stat_cycle, line 7:    time\.sleep.* (re)
 
   $ hg --profile --config profiling.statformat=json sleep 2>../out || cat ../out
   $ cat ../out