Skip to content

TempEntity performance #223

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

Closed
jsza opened this issue Nov 5, 2017 · 5 comments
Closed

TempEntity performance #223

jsza opened this issue Nov 5, 2017 · 5 comments

Comments

@jsza
Copy link
Contributor

jsza commented Nov 5, 2017

I am upgrading from a very old version of SP (> 1 year) and the new TempEntities class is slower than exposing ITempEntsSystem.

import time

from commands.say import SayCommand
from effects.base import TempEntity
from engines.precache import Model
from mathlib import Vector
from players.entity import Player


@SayCommand('test')
def on_test(command, index, teamonly):
    player = Player(index)
    start = player.origin
    end = player.origin + Vector(0, 0, 128)
    material = Model('materials/sprites/laser.vmt')

    s = time.time()
    # emulate lines for 3 boxes
    for x in range(36):
        TempEntity('BeamPoints', alpha=255, red=255, green=255, blue=255,
                   life_time=1.0, start_width=4, end_width=4, frame_rate=255,
                   model=material, halo=material)
        beam.create()

    print('Loop took: ', time.time() - s)

Loop took: 0.01820659637451172

I've had success with this workaround, maybe we can do this for the other wrapper functions in the effects package?

    beam = TempEntity('BeamPoints', alpha=255, red=255, green=255, blue=255,
                      life_time=1.0, start_width=4, end_width=4, frame_rate=255,
                      model=material, halo=material)
    for x in range(36):
        beam.start_point = start
        beam.end_point = end
        beam.create()

0.004525899887084961

@jsza
Copy link
Contributor Author

jsza commented Nov 5, 2017

Uhm, disregard this. I went to make the changes to effects.beam and they were already there! Mistakes were made 😛

@jsza jsza closed this as completed Nov 5, 2017
@jsza
Copy link
Contributor Author

jsza commented Nov 5, 2017

And never mind again. I got confused between effects.beam and effects.box... I'll have a PR up soon.

@jordanbriere
Copy link
Contributor

jordanbriere commented Nov 5, 2017

Yes, always a better idea to create TempEntity/UserMessage instances once (into global scope, when possible) and update values that were not known at load time (such as origins, etc.). I can indeed see how looping dozens of times into the effects helpers was costly. Thank you!

I just checked your PR, seems like you forgot to remove line 269 and 278.

@jsza
Copy link
Contributor Author

jsza commented Nov 5, 2017

Thank you, updated.

@jordanbriere
Copy link
Contributor

Merged, thank you! To add more info; the reason why ITempEntsSystem was faster in the effect wrappers is because it internally does the same thing as your proposed optimization: update values of a global CBaseTempEntity instance. The TempEntity class is copying that global instance from the engine on initialization (mainly for its dispatch table) because it allows plugins to have their own instances that doesn't get edited/affected by others. So settings values that are known at load time is definitely the way to go so you avoid having to set them later on and save calls which optimize.

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

No branches or pull requests

2 participants