KEMBAR78
Inline some of the most called functions in SCI and SCUMM by mikrosk · Pull Request #4852 · scummvm/scummvm · GitHub
Skip to content

Conversation

@mikrosk
Copy link
Contributor

@mikrosk mikrosk commented Mar 29, 2023

When profiling a few games, we have spotted some functions which repeatedly made it to the top ten most called / cpu eating functions (on a 32 MHz hardware, so that explains why those numbers are so high...)

From the SCI camp we tested Gabriel Knight, Larry 2, Larry 7 and Sierra's Christmas Card 1988. The most CPU intensive functions (shown on Larry 2 intro but it is similar for the others):

8.30% of all spent CPU cycles: Sci::reg_t::getOffset() const
6.08% of all spent CPU cycles: Sci::getSciVersion()
1.80% of all spent CPU cycles: Sci::reg_t::setOffset(unsigned int)
1.25% of all spent CPU cycles: Sci::Script::offsetIsObject(unsigned int) const

After applying this PR, the total run_vm() cost went from 0.00940s / call, down to 0.00672s / call, which is 28.5% improvement!

During a run of Full Throttle, testGfxUsageBit() calls account for 6.2% of all function calls (!) and ~2% of used cycles.

The getSciVersion commit is perhaps the most interesting for review purposes; I was thinking about doing what the FIXME suggests but that wouldn't be such a win for performance as it could be inlined, sure, but also it would be replaced with another indirect jump caused by g_sci->getSciVersion() so I've taken the most efficient path even if it doesn't improve the code.

@AndywinXp
Copy link
Contributor

That's great! Nice job! The SCUMM parts looks good to go!

@sluicebox
Copy link
Member

sluicebox commented Mar 29, 2023

Overall this looks good to me. For fun, I compared msvc release builds, the SCI changes add 4k. I appreciate the profiling details, my first response to the PR title was "...but we're speed throttled?", makes much more sense given the machines you're targeting. =)

What's the benefit of moving declarations into a new version.h? Also, it would need the copyright header. Either way, I think getSciVersionDesc would be better declared in resource.h. (That is, I think it already should have been.) It's not a part of this, it's just for debug/logging, and it's in resource.cpp. (Or maybe what I'm asking is, should we eventually just make a version.cpp?)

Don't worry about the FIXME on making the SCI version global not a global; that would be a huge undertaking and it's not really a problem. Might as well get some speed out of it. =)

A while ago I reversed a ScummVM msvc exe to see what a fork that we didn't have source for had done. Everywhere I looked I saw getSciVersion inlined along with its assert. "Wow, we call this A LOT!"

Also, commit messages need SCI and SCUMM: prefixes

@sluicebox sluicebox requested a review from bluegr March 29, 2023 23:05
@mikrosk mikrosk force-pushed the scumm_sci_inlines branch from 53fa3cc to 2091c48 Compare March 30, 2023 20:37
@mikrosk
Copy link
Contributor Author

mikrosk commented Mar 30, 2023

Pushed new changes.

What's the benefit of moving declarations into a new version.h? Also, it would need the copyright header. Either way, I think getSciVersionDesc would be better declared in resource.h.

Initially I thought it is a good idea to have all version-related functions together. However you are right, the versions are more related to resource handling than some generic version checking so I've moved the two functions into resource.h.

As for version.h / version.cpp I had been considering it but that would mean that g_sciVersion would need to be exposed for resource.cpp while current implementation in version.h only limits the declaration into one specific function which has one specific purpose (being inlined :) ). And of course, it has also way less header dependencies (in comparison to sci.h or resource.h). IIRC, the initial location (sci.h) created some circular dependency when I inlined getSciVersion into sci.h so I had to move it somewhere.

I've also fixed the cosmetic requests.

@sluicebox
Copy link
Member

@mikrosk Thanks for the explanation and updates; this looks great to me. Summoning"SCI Elder" @bluegr for final sign off! =)

@sev-
Copy link
Member

sev- commented Apr 29, 2023

Thank you, makes sense, and I like the minimalism of the changes.

@sev- sev- merged commit 864bc30 into scummvm:master Apr 29, 2023
@mikrosk mikrosk deleted the scumm_sci_inlines branch April 29, 2023 10:37
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.

4 participants