Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"If truth is beauty, how come no one has their hair done in the library?" -- Lily Tomlin


devel / comp.lang.mumps / Values, copies and string building

SubjectAuthor
* Values, copies and string buildingJens Lideström
`* Re: Values, copies and string buildingK.S. Bhaskar
 `- Re: Values, copies and string buildingJens Lideström

1
Values, copies and string building

<41d382ab-9409-4793-8bb7-ec31dea0fa37n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=453&group=comp.lang.mumps#453

  copy link   Newsgroups: comp.lang.mumps
X-Received: by 2002:a05:6214:5cc5:b0:4b3:e05a:bbb9 with SMTP id lk5-20020a0562145cc500b004b3e05abbb9mr7920718qvb.42.1666010391015;
Mon, 17 Oct 2022 05:39:51 -0700 (PDT)
X-Received: by 2002:a05:620a:22cc:b0:6ee:3e43:ac40 with SMTP id
o12-20020a05620a22cc00b006ee3e43ac40mr7585503qki.454.1666010390851; Mon, 17
Oct 2022 05:39:50 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.mumps
Date: Mon, 17 Oct 2022 05:39:50 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=81.232.46.228; posting-account=e6ZTGwoAAAD0RJ1tKSnsnYuh1JIHXrm6
NNTP-Posting-Host: 81.232.46.228
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <41d382ab-9409-4793-8bb7-ec31dea0fa37n@googlegroups.com>
Subject: Values, copies and string building
From: jens.lidestrom@vgregion.se (Jens Lideström)
Injection-Date: Mon, 17 Oct 2022 12:39:51 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2849
 by: Jens Lideström - Mon, 17 Oct 2022 12:39 UTC

I'm wondering the following about M programs in GTM:

When are values copied, and can separate variables sometimes point to the same value?

Are the values of the variables copied in all of the following examples?

```
set localVariable=otherLocalVariable
set localVariable(1)=otherLocalVariable(
set global=otherLocalVariable
set global(1)=otherLocalVariable
set $extract(localVariable,1,2)=otherLocalVariable
do routine(localVariable)
set localVariable=$$extrinsicFunction()
```

These are the operations I want to illustrate with the examples:

* SET
- Are the value of local variables copied when one local is set to another? On globals?
- On the top-level values and values of subscripts?

* On SET `$extract(x,1,2)="X"`? Will the old contents of x be discarded, and a new string allocated with an updated copy?

* On routine and extrinsic functions invocations, are the arguments copied? (Reference arguments are obviously not copied.)

I realise than when the values are small this is not important, because it will be fast to make copies. But if the values are large it seems like it might become a performance problem. It would be good to know a little about how this works.

One example where copies can be slow is when you build a string in a loop:
```
for i=1:1:10000 set text=$get(text)_i
```

It seems like in this example the content of the variable `text` will be copied in each iteration, making the loop runtime O(n^2) in the length of `text`.
Is there any way to avoid all the copies of `text` and build a string with linear execution time?

Are these kinds of things described in the GTM documentation? Or in other places?

Best regards,
Jens Lideström

Re: Values, copies and string building

<56afb3eb-99ec-4388-bdff-c5ba39ac50c7n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=455&group=comp.lang.mumps#455

  copy link   Newsgroups: comp.lang.mumps
X-Received: by 2002:ad4:5be6:0:b0:4b3:ff39:7ad4 with SMTP id k6-20020ad45be6000000b004b3ff397ad4mr2991202qvc.126.1666112832738;
Tue, 18 Oct 2022 10:07:12 -0700 (PDT)
X-Received: by 2002:a05:622a:58f:b0:394:d520:dbe1 with SMTP id
c15-20020a05622a058f00b00394d520dbe1mr3003720qtb.164.1666112832415; Tue, 18
Oct 2022 10:07:12 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.mumps
Date: Tue, 18 Oct 2022 10:07:12 -0700 (PDT)
In-Reply-To: <41d382ab-9409-4793-8bb7-ec31dea0fa37n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=71.162.243.192; posting-account=zTPg1AoAAABx_LtAQ3dW6FBnU1dwmSvl
NNTP-Posting-Host: 71.162.243.192
References: <41d382ab-9409-4793-8bb7-ec31dea0fa37n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <56afb3eb-99ec-4388-bdff-c5ba39ac50c7n@googlegroups.com>
Subject: Re: Values, copies and string building
From: ksbhaskar@gmail.com (K.S. Bhaskar)
Injection-Date: Tue, 18 Oct 2022 17:07:12 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 5258
 by: K.S. Bhaskar - Tue, 18 Oct 2022 17:07 UTC

On Monday, October 17, 2022 at 8:39:51 AM UTC-4, jens.li...@vgregion.se wrote:
> I'm wondering the following about M programs in GTM:
>
> When are values copied, and can separate variables sometimes point to the same value?

[KSB] Yes, sometimes separate variables can point to the same value. In some cases, it is explicit, and in some cases, it is implicit.

For explicitly connecting variables, look at alias variables (https://docs.yottadb.com/ProgrammersGuide/langext.html#alias-variable-extensions).

For the implicit cases, an explanation might help. Actual strings are stored in a garbage collected heap called the string pool. An "mval" is data structure that includes (among other fields) a pointer to a location in the string pool and an associated length. So, a statement such as SET X=$EXTRACT(Y,…) creates a new mval that points to a part of the stringpool that the mval for X points to.

Strings in the string pool are never modified, but they are garbage collected from time to time.
> Are the values of the variables copied in all of the following examples?
>
> ```
> set localVariable=otherLocalVariable
> set localVariable(1)=otherLocalVariable(

[KSB] In the above two cases, mvals are copied, not strings in the string pool.

> set global=otherLocalVariable
> set global(1)=otherLocalVariable

[KSB] Outside a transaction these two update an in-memory copy of the database block. When they occur inside transactions, the database blocks are updated when transactions are committed.

> set $extract(localVariable,1,2)=otherLocalVariable

[KSB] Yes, this does copy data in the string pool.

> do routine(localVariable)

[KSB] This copies an mval.

> set localVariable=$$extrinsicFunction()

[KSB] The mval of the value returned by the extrinsic function is assigned to the local variable.

> ```
>
> These are the operations I want to illustrate with the examples:
>
> * SET
> - Are the value of local variables copied when one local is set to another? On globals?
> - On the top-level values and values of subscripts?
>
> * On SET `$extract(x,1,2)="X"`? Will the old contents of x be discarded, and a new string allocated with an updated copy?
>
> * On routine and extrinsic functions invocations, are the arguments copied? (Reference arguments are obviously not copied.)
>
> I realise than when the values are small this is not important, because it will be fast to make copies. But if the values are large it seems like it might become a performance problem. It would be good to know a little about how this works.

[KSB] If the answers above did not answer these question, please ask again.

> One example where copies can be slow is when you build a string in a loop:
>
> ```
> for i=1:1:10000 set text=$get(text)_i
> ```
>
> It seems like in this example the content of the variable `text` will be copied in each iteration, making the loop runtime O(n^2) in the length of `text`.
>
> Is there any way to avoid all the copies of `text` and build a string with linear execution time?

[KSB] The concatenate operator has an optimization that if the string being concatenated to is the last string in the string pool such that the string can just be 'extended' then it does so without copying the entire string over so this should not be a performance issue other than of course doing one character at a time. The loop is far more expensive than the concatenate. In a loop such as this, the first set may copy the string but subsequent extensions shouldn't because the shortcut would be available as it would then be the last string in the string pool.

> Are these kinds of things described in the GTM documentation? Or in other places?

[KSB] Only in the source code.

Regards
– Bhaskar

>
> Best regards,
> Jens Lideström

Re: Values, copies and string building

<3b3b3f84-647e-4766-8402-ba389aa9b705n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=458&group=comp.lang.mumps#458

  copy link   Newsgroups: comp.lang.mumps
X-Received: by 2002:ac8:5854:0:b0:39c:dba4:6fa0 with SMTP id h20-20020ac85854000000b0039cdba46fa0mr3258229qth.175.1666117400633;
Tue, 18 Oct 2022 11:23:20 -0700 (PDT)
X-Received: by 2002:a0c:8ec1:0:b0:4b4:2508:c4ee with SMTP id
y1-20020a0c8ec1000000b004b42508c4eemr3381092qvb.45.1666117400455; Tue, 18 Oct
2022 11:23:20 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.mumps
Date: Tue, 18 Oct 2022 11:23:20 -0700 (PDT)
In-Reply-To: <56afb3eb-99ec-4388-bdff-c5ba39ac50c7n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=81.232.46.228; posting-account=e6ZTGwoAAAD0RJ1tKSnsnYuh1JIHXrm6
NNTP-Posting-Host: 81.232.46.228
References: <41d382ab-9409-4793-8bb7-ec31dea0fa37n@googlegroups.com> <56afb3eb-99ec-4388-bdff-c5ba39ac50c7n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3b3b3f84-647e-4766-8402-ba389aa9b705n@googlegroups.com>
Subject: Re: Values, copies and string building
From: jens.lidestrom@vgregion.se (Jens Lideström)
Injection-Date: Tue, 18 Oct 2022 18:23:20 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1295
 by: Jens Lideström - Tue, 18 Oct 2022 18:23 UTC

Thanks for the explanation, Bhaskar! This is super interesting information.

/Jens

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor