Skip to content

Standard blits have inconsistent performance #2388

@itzpr3d4t0r

Description

@itzpr3d4t0r

Our current implementation of blit for what is likely the most standard way of blitting isn't right. It has inconsistent performance across various sizes with whole orders of magniture difference between even a single pixel difference.
This was previously tackled by @MyreMylar in an upstream draft PR but was never replicated. I believe this is an important topic that needs to be addressed as it impacts many people.

I believe that a custom pygame blitcopy function will need to be created for this.

This is the plot i made to illustrate this issue, note how the different performance levels differ wildly:
image

This can be easily seen with the following program:

import pygame
from matplotlib import pyplot as plt
from timeit import timeit

pygame.init()

max_size = 1000

base = pygame.Surface((max_size, max_size))

for s in range(max_size + 1):
    top = pygame.Surface((s, s))
    G = globals()
    im_data = timeit("base.blit(top, (0, 0))", globals=G, number=100)
    plt.scatter(s, im_data)
plt.show()

As for some actual values here is a slightly modified program and the results:

import pygame
from matplotlib import pyplot as plt
from timeit import timeit

pygame.init()

max_size = 100

base = pygame.Surface((max_size, max_size))

data = {}

for s in range(max_size + 1):
    top = pygame.Surface((s, s))
    G = globals()
    im_data = timeit("base.blit(top, (0, 0))", globals=G, number=100)
    data[f"({s}, {s})"] = im_data
    plt.scatter(s, im_data)
    
for test, datapt in data.items():
    print(f"{test}: {datapt}")
plt.show()
(0, 0): 3.8099999983387534e-05
(1, 1): 4.179999996267725e-05
(2, 2): 3.599999990910874e-05
(3, 3): 3.609999998843705e-05
(4, 4): 3.710000009959913e-05
(5, 5): 3.5499999967214535e-05
(6, 6): 3.790000005210459e-05
(7, 7): 3.7400000110210385e-05
(8, 8): 3.7699999893447966e-05
(9, 9): 3.8300000142044155e-05
(10, 10): 4.190000004200556e-05
(11, 11): 3.969999988839845e-05
(12, 12): 4.1999999893960194e-05
(13, 13): 4.149999995206599e-05
(14, 14): 4.330000001573353e-05
(15, 15): 4.260000014255638e-05
(16, 16): 4.55999997939216e-05
(17, 17): 4.4299999899521936e-05
(18, 18): 4.2200000052616815e-05
(19, 19): 4.4299999899521936e-05
(20, 20): 0.00038229999995564867
(21, 21): 4.280000007383933e-05
(22, 22): 5.000000010113581e-05
(23, 23): 4.479999984141614e-05
(24, 24): 0.0004620000001978042
(25, 25): 4.46999999894615e-05
(26, 26): 4.739999985758914e-05
(27, 27): 4.5199999931355705e-05
(28, 28): 0.0005215999999563792
(29, 29): 5.050000004303001e-05
(30, 30): 5.320000013853132e-05
(31, 31): 4.860000012740784e-05
(32, 32): 6.110000003900495e-05
(33, 33): 4.939999985253962e-05
(34, 34): 5.400000009103678e-05
(35, 35): 4.99000000218075e-05
(36, 36): 0.0006670999998732441
(37, 37): 5.1500000154192094e-05
(38, 38): 5.8700000181488576e-05
(39, 39): 5.4399999953602673e-05
(40, 40): 0.000742599999966842
(41, 41): 5.51000000541535e-05
(42, 42): 6.050000001778244e-05
(43, 43): 5.719999990105862e-05
(44, 44): 0.0007877999998981977
(45, 45): 5.82000000122207e-05
(46, 46): 7.029999983387825e-05
(47, 47): 5.7399999832341564e-05
(48, 48): 8.590000015828991e-05
(49, 49): 5.989999999655993e-05
(50, 50): 7.370000002993038e-05
(51, 51): 6.200000007083872e-05
(52, 52): 0.0009717000000364351
(53, 53): 6.639999992330559e-05
(54, 54): 7.679999998799758e-05
(55, 55): 6.520000010823424e-05
(56, 56): 0.0011038000000098691
(57, 57): 6.95999999607011e-05
(58, 58): 8.659999980409339e-05
(59, 59): 7.510000000365835e-05
(60, 60): 0.0011160999999901833
(61, 61): 7.310000000870787e-05
(62, 62): 8.679999996275001e-05
(63, 63): 7.359999995060207e-05
(64, 64): 0.00011519999998199637
(65, 65): 7.590000018353749e-05
(66, 66): 0.00010200000019722211
(67, 67): 7.879999998294807e-05
(68, 68): 0.0012280000000828295
(69, 69): 8.720000005268957e-05
(70, 70): 0.00010070000007544877
(71, 71): 8.240000011028314e-05
(72, 72): 0.0013069000001451059
(73, 73): 8.689999981470464e-05
(74, 74): 0.00010479999991730438
(75, 75): 8.439999987785995e-05
(76, 76): 0.0013922000000547996
(77, 77): 9.540000019114814e-05
(78, 78): 0.00011259999996582337
(79, 79): 9.559999989505741e-05
(80, 80): 0.00015129999997043342
(81, 81): 9.579999982634035e-05
(82, 82): 0.00012399999991430377
(83, 83): 9.790000012799283e-05
(84, 84): 0.001565199999959077
(85, 85): 0.00010160000010728254
(86, 86): 0.00013229999990471697
(87, 87): 0.00010229999998045969
(88, 88): 0.0016287999999349267
(89, 89): 0.00010729999985414906
(90, 90): 0.00013890000013816461
(91, 91): 0.00016670000013618846
(92, 92): 0.001699000000144224
(93, 93): 0.0001111999999920954
(94, 94): 0.00014520000013362733
(95, 95): 0.00012260000016794947
(96, 96): 0.0001953999999386724
(97, 97): 0.000120499999866297
(98, 98): 0.00016819999996187107
(99, 99): 0.00011850000009872019
(100, 100): 0.0020268999999188964

image

This is up to 2k width/height btw:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformanceRelated to the speed or resource usage of the projectbugNot working as intendedenhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions