DO/CASE limits?

This forum is meant for questions and discussions about the X# language and tools
Post Reply
stefan.ungemach
Posts: 71
Joined: Thu Jul 15, 2021 10:46 am
Location: Germany

DO/CASE limits?

Post by stefan.ungemach »

Is there a known limit to the number of CASEs in a DO/CASE construct? I have a simple construct like

DO CASE
CASE symField == #A4HHST
symClass := #HHST
(...)
ENDCASE
where the compiler crashes if I have more than 668 CASE conditions (reproducable). Since this is a strange number I'm not sure about some violated boundaries. Is there a known limit, and is there possibly a better way to deal with the need of processing several thousand of these translations?
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

DO/CASE limits?

Post by Chris »

Hi Stefan,

There is (was) no such known limit, but I just managed to reproduce the problem here, although in my case I needed to use 1500 case conditions!

This is a compiler bug, but of course indeed this is not the most efficient thing to do this. If all your CASE statements are like those in your code snippet, I would use a hastable/dictionary, which would make the code perform extremely fast and make it more readable I think. Something like:

Code: Select all

USING System.Collections.Generic

FUNCTION CreateTable() AS Dictionary<SYMBOL,SYMBOL>
	LOCAL oTable AS Dictionary<SYMBOL,SYMBOL>
	oTable := Dictionary<SYMBOL,SYMBOL>{1000}
	oTable:Add(#A4HHST, #HHST)
	oTable:Add(#ANOTHER, #RESULT)
	oTable:Add(#FOO, #BAR)
	// ....
RETURN oTable

FUNCTION GetClassFromField(symField AS SYMBOL) AS SYMBOL
STATIC LOCAL oTable := CreateTable() AS Dictionary<SYMBOL,SYMBOL>
IF oTable:ContainsKey(symField)
	RETURN oTable[symField]
END IF
RETURN null_SYMBOL


FUNCTION Start() AS VOID
LOCAL symField,symClass AS SYMBOL

symField := #A4HHST
symClass := GetClassFromField(symField)
? symClass

symField := #FOO
symClass := GetClassFromField(symField)
? symClass
Just make sure that the hashtable is created only once! (hence the STATIC local in my sample).

Another option would be to use SWITCH instead of DO CASE, but I think the hashtable is a more elegant solution.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
stefan.ungemach
Posts: 71
Joined: Thu Jul 15, 2021 10:46 am
Location: Germany

DO/CASE limits?

Post by stefan.ungemach »

Thank you Chris,

the hashtable is a much smarter solution - especially because we're talking of generated code here which leads to several thousand elements. I have several use cases for this :)
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

DO/CASE limits?

Post by Chris »

You're welcome!

Just for completeness, using a SWITCH instead of a DO CASE, will also generate code that internally uses a hash table, so it will also be very fast. But not so readable/elegant, when you have so many cases.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

DO/CASE limits?

Post by robert »

Chris,
Chris post=25416 userid=313 wrote:Hi Stefan,

There is (was) no such known limit, but I just managed to reproduce the problem here, although in my case I needed to use 1500 case conditions!
Do you convert your test code into a Github issue?
And maybe I should fix it by displaying an error message about "bad code quality"?

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
leon-ts
Posts: 435
Joined: Fri Feb 03, 2017 1:43 pm

DO/CASE limits?

Post by leon-ts »

Hi guys,
I would also suggest using the TryGetValue method in this particular case to immediately check and get the value if it is in the dictionary:

Code: Select all

IF oTable:TryGetValue(symField, OUT symFoundClass AS SYMBOL)
	RETURN symFoundClass
END IF
Best regards,
Leonid
Best regards,
Leonid
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

DO/CASE limits?

Post by Chris »

Robert, done! :)

Leonid, good point!

And note to myself, a SWITCH command does not allow a SYMBOL for its expression

.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
stefan.ungemach
Posts: 71
Joined: Thu Jul 15, 2021 10:46 am
Location: Germany

DO/CASE limits?

Post by stefan.ungemach »

Thanks again to all. I have chosen Leonid's approach.
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

DO/CASE limits?

Post by robert »

Chris,
Chris post=25421 userid=313 wrote:Robert, done! :)
And note to myself, a SWITCH command does not allow a SYMBOL for its expression
.
In that case use Symbol2String() on the symbol and compare with (uppercase) literal strings,.
The compiler will compare hashcodes, so that is relatively fast.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply