mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.36k stars 1.22k forks source link

Lazy string interpolation in ENSURE #2454

Closed alexbereznikov closed 3 weeks ago

alexbereznikov commented 3 months ago

String interpolation on hot paths leads to performance degradation. We need these messages only when error happened. With #2453 applied this gives 3x performance boost.

This partially fixes #2451

ltetak commented 3 months ago

Good catch. I guess it would be more standard to move the string interpolation deeper as other libraries do it. public static void ENSURE(bool conditional, string message, params object[] args)

alexbereznikov commented 3 months ago

@ltetak Sure I thought about that, but profiling says that most of the time was lost in BasePage.ToString() and I'm not quite sure how to avoid calculating it when passing via args

alexbereznikov commented 3 months ago

@ltetak Actually this .ToString() call is redundant as formatter will call it either way. So it should work in the way you proposed, pushed fixed code, please take a look

ltetak commented 3 months ago

@alexbereznikov I am not an owner of this repo. But this looks like a great performance improvement without any risks or side effects. cc: @mbdavid

JKamsker commented 3 weeks ago

LGTM! Thanks for your contribution!