The SVG component is very fast. I thought I would shave off some of its overhead by directly using XML commands in a loop to create squares. Below is the code that does that.

When using the SVG Component commands the loop is really fast. When the IfElse is False so XML is used, the loop is slower, and comes practically to a stop before completing when around 3000 squares are being created.

Any idea why using XML code would be slower?

		If (True)		
			$ref:=SVG_New_rect ($svg;$x;$y;$cSize;$cSize;0;0;"white";$obj.color)
			SVG_SET_ID ($ref;$name)
			$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\
		End if 

Running a small sample below generates SVG code that looks right.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><svg xmlns="" xmlns:xlink=""><metadata xmlns=""><generator>4D SVG v18R3.104 ©4D SAS, 2008-2020.</generator><about>4D SVG is the quintessential vector drawing tool for 4D developers.</about><generation date="2020-06-10" system="macOS 10.14.6 (18G5033)" time="PT12H4M39S"/></metadata><defs id="4D"/>
<rect fill="black" height="40" id="sq1" stroke="white" width="40" x="1" y="1"/>
<rect fill="black" height="40" id="sq2" stroke="white" width="40" x="2" y="2"/>
<rect fill="black" height="40" id="sq3" stroke="white" width="40" x="3" y="3"/>

---4D Code for the above SVG---
$ref:=SVG_New_rect ($svg;$x;$y;$cSize;$cSize;0;0;"white";"black")
SVG_SET_ID ($ref;"sq1")

$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\

$ref:=DOM Create XML element($svg;"rect";"x";$x;"y";$y;"width";$cSize;\

$txt:=SVG_Export_to_XML ($svg)
OBJECT Get pointer(Object named;"pic")->:=SVG_Export_to_picture ($svg)
SVG_CLEAR ($svg)

Thanks - Keith

without knowing why, I’d try to replace $obj.color with a $locale first. If it doesn’t change anything, I’d suspect some trick in the component to replace DOM Create XML element, because what you describe sounds like an increasingly difficult course. As a partner, I seem to remember the source code is available: no?
(I’m unable to remember where 4d components sources are, BTW)


DOM Create XML element uses xpath. Maybe that is the slow part…

Can you try using this instead and let us know ?

$ref:=DOM Append XML child node($svg;XML ELEMENT;"rect")
1 Like
1 Like

I took the code to create a rect from the SVG source, and stopped investigating as soon as I saw “…:=DOM Create XML element($svg;“rect”;“x”…”. I can’t find the word “child” in the source of the “SVG_New_rect.4dm” text. It does create only the first four attributes on that line of code. The rest are added later using “DOM SET XML ATTRIBUTE”.

I was going to substitute a local for the object, but the attribute is used only once in the loop, and can change with each interation.

The substitute code appending the child node works, and is noticably faster. I would not have guessed this alternate approach.

Thanks for the help.


1 Like