Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Improve Tcl_GetNumberFromObj with abstract list interfaces

Closed / Out of Date10545687da9962482e3a9014b4cf9cbcca07e647Created 1 year agoUpdated 1 year ago
Descriptionplain2025-09-04 19:10:50
Tcl_GetNumberFromObj() has already been written to make use of
the extended Tcl_ObjType abstract list routines to efficiently
reject non-singleton lists as non-numbers.

I have a patch to re-order this processing, which is possible because
all of Tcl's numeric types have implemented the abstract list routines
so rejecting early is efficient enough to come first without worry
about slowing down the accept results noticeably.

Some comments are added to help later readers understand the logic.
Referenced by1
dgpplain2025-09-04 19:12:51
Branch bug-10545687da ready for review and merge.
jan.nijtmanswiki2025-09-05 13:01:35

Actually, I don't like this change.

What is done here is speed-up the error-case, slowing down the most-common OK case, where the Tcl_Obj is already a double, int or bignum.

It's only a few compare operations, not much at all, but I don't see much benefit on this change. Speed is more important for the OK case than for the error-case (which is already slow anyway), therefore I prefer the code as it was.

Sorry!

Adding comments, so people understand the logic is always good. Maybe we should comment here why checking for double/int/bignum is done first.

jan.nijtmanswiki2025-09-05 13:05:21

Another reason the lengthProc() shouldn't be called first: What if some extension implements an objtype which is implemented as a linked list. Then the lengthProc() of this type might be very costly. That's why lengthProc() should be called last, after all other (cheap) type checks.

dgpplain2025-09-05 13:44:19
I think if you reflect a little longer you will see that your
second comment doesn't match how these facilities are used.

I will look again to see what adaptations can take account of
your first comment.
griffinplain2025-09-05 13:52:40
Not sure if making a choice based on potential bad actors implementing poorly designed abstract lists is wise. 

Then there's the coder that chooses to use GetNumber without having some reasonable assumption that it is a number. 

I think avoiding string creation whenever possible is as important as avoiding shimmering.
What is needed is some test results that characterize the different approaches.
jan.nijtmanswiki2025-09-05 15:13:17

Note that there are 8 functions following the same concept: Tcl_GetNumberFromObj(), Tcl_GetLongFromObj(), Tcl_GetWideIntFromObj(), Tcl_GetWideUIntFromObj(), TclGetWideBitsFromObj(), GetBignumFromObj(), Tcl_GetDoubleFromObj() and Tcl_GetBoolFromObj().

All of them do this to prevent generating the string representation in as many situations as possible.

dgpplain2025-09-05 19:58:46
Revised code committed for further review.

Now that you point out the other places dealing with similar tasks,
I'll see whether there's a refactor opportunity.
jan.nijtmanswiki2025-09-06 10:15:54

Even better: why not move the lengthProc protection into TclParseNumber(). Then all callers of TclParseNumber() can benefit from it

jan.nijtmanswiki2025-09-06 10:32:24

Note that [this] ticket is where this all originate from

jan.nijtmanswiki2025-09-07 10:15:53

[this] is (starting) the refactoring I had in mind. Not finished yet (the same can be done in some more functions), but you can already review this part.

dgpplain2025-09-08 14:20:36
This has been overtaken by work on another ticket.  Closing.
Log in (anonymous is fine) to add a comment.