tesseract
tesseract copied to clipboard
Fix possible UB when accessing empty vector's data
Make sure that the empty vector's data is never used, at()
will throw an exception in that case.
This may allow compilers to better optimize code.
I'd expect that code using at()
is slower, so I am not sure whether this is really an improvement. Did you test code size and performance?
I did not perform large scale tests so can't be sure if this is faster in all cases.
In my test case (single long line) this looks to be faster. Init() is not called that many times (about 30) and there should be virtually no exception related performance hit cause nothing is thrown.
On the other hand it may be beneficial to do some testing on non-x86 platforms but I do not have those available atm.
Edit: actually this at()
call is just after resize()
which can also throw (on all platforms that tesseract supports I assume?) so the fact that there might be an exception should be a non-issue and it protects from UB.
Probably std::vector::data() will help in this particular case.