Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.awk Subject: Re: substr() - copying or not copying, that is here the question. Date: Sun, 1 Jun 2025 00:01:11 -0000 (UTC) Organization: A noiseless patient Spider Lines: 31 Message-ID: <20250531165750.71@kylheku.com> References: <101f9oo$18edp$1@dont-email.me> <683b5389$0$683$14726298@news.sunsite.dk> Injection-Date: Sun, 01 Jun 2025 02:01:12 +0200 (CEST) Injection-Info: dont-email.me; posting-host="0e1641686119009e1c5a19483c56cb9e"; logging-data="1635245"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GzjIpv1fddjck10f5/A5ZrqFkZotogbE=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:k+Zse91XgV9/rkSE60DcPj4ETKk= Xref: csiph.com comp.lang.awk:9980 On 2025-05-31, Mack The Knife wrote: > In article <101f9oo$18edp$1@dont-email.me>, > Janis Papanagnou wrote: >>In the context p=index(substr(t,s),r) >>it would not be necessary to copy the substr(t,s), >>the index() function could operate on the original >>using some access "descriptor" (say, a pointer and >>a length) in read-only mode. >> >>Will (GNU) Awk do a copy of the data value or does >>it use a read-only descriptor access to the already >>existing substring of variable "t"? >> >>Currently I'm playing with some huge data and copies >>of MB sized data is costly (if it's repeatedly done >>with various substr() subscripts). > > substr() makes a copy. This is clear in the code. > > It's almost impossible to do this via read-only descriptor. It's entirely possible, with just reference counted memory management. Under the hood, you have two kinds of string types: primary strings, and strings which are views displaced into other strings. The displacing strings own a reference count on the target string, and indicate the offset and range. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca